MCPcopy
hub / github.com/grpc/grpc-go / processServerList

Method processServerList

balancer/grpclb/grpclb_remote_balancer.go:59–102  ·  view source on GitHub ↗

processServerList updates balancer's internal state, create/remove SubConns and regenerates picker using the received serverList.

(l *lbpb.ServerList)

Source from the content-addressed store, hash-verified

57// processServerList updates balancer's internal state, create/remove SubConns
58// and regenerates picker using the received serverList.
59func (lb *lbBalancer) processServerList(l *lbpb.ServerList) {
60 if lb.logger.V(2) {
61 lb.logger.Infof("Processing server list: %#v", l)
62 }
63 lb.mu.Lock()
64 defer lb.mu.Unlock()
65
66 // Set serverListReceived to true so fallback will not take effect if it has
67 // not hit timeout.
68 lb.serverListReceived = true
69
70 // If the new server list == old server list, do nothing.
71 if serverListEqual(lb.fullServerList, l.Servers) {
72 if lb.logger.V(2) {
73 lb.logger.Infof("Ignoring new server list as it is the same as the previous one")
74 }
75 return
76 }
77 lb.fullServerList = l.Servers
78
79 var backendAddrs []resolver.Address
80 for i, s := range l.Servers {
81 if s.Drop {
82 continue
83 }
84
85 md := metadata.Pairs(lbTokenKey, s.LoadBalanceToken)
86 var ipStr string
87 if ip, ok := netip.AddrFromSlice(s.IpAddress); ok {
88 ipStr = ip.String()
89 } else {
90 ipStr = fmt.Sprintf("? %x", s.IpAddress)
91 }
92 addr := imetadata.Set(resolver.Address{Addr: net.JoinHostPort(ipStr, fmt.Sprintf("%d", s.Port))}, md)
93 if lb.logger.V(2) {
94 lb.logger.Infof("Server list entry:|%d|, ipStr:|%s|, port:|%d|, load balancer token:|%v|", i, ipStr, s.Port, s.LoadBalanceToken)
95 }
96 backendAddrs = append(backendAddrs, addr)
97 }
98
99 // Call refreshSubConns to create/remove SubConns. If we are in fallback,
100 // this is also exiting fallback.
101 lb.refreshSubConns(backendAddrs, false, lb.usePickFirst)
102}
103
104// refreshSubConns creates/removes SubConns with backendAddrs, and refreshes
105// balancer state and picker.

Callers 1

readServerListMethod · 0.80

Calls 9

refreshSubConnsMethod · 0.95
PairsFunction · 0.92
serverListEqualFunction · 0.85
VMethod · 0.65
InfofMethod · 0.65
StringMethod · 0.65
SetMethod · 0.65
LockMethod · 0.45
UnlockMethod · 0.45

Tested by

no test coverage detected