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

Function waitForDistribution

examples/features/dualstack/client/main.go:134–189  ·  view source on GitHub ↗

waitForDistribution makes RPC's on the greeter client until 3 RPC's follow the same 1:1:1 address ratio for the peer. Returns an error if fails to do so before context timeout.

(ctx context.Context, client hwpb.GreeterClient)

Source from the content-addressed store, hash-verified

132// the same 1:1:1 address ratio for the peer. Returns an error if fails to do so
133// before context timeout.
134func waitForDistribution(ctx context.Context, client hwpb.GreeterClient) error {
135 wantPeers := []string{
136 // Server 1 is listening on both IPv4 and IPv6 loopback addresses.
137 // Since the IPv6 address comes first in the resolver list, it will be
138 // given higher priority.
139 fmt.Sprintf("[::1]:%d", port1),
140 // Server 2 is listening only on the IPv4 loopback address.
141 fmt.Sprintf("127.0.0.1:%d", port2),
142 // Server 3 is listening only on the IPv6 loopback address.
143 fmt.Sprintf("[::1]:%d", port3),
144 }
145 const iterationsToVerify = 3
146 const backendCount = 3
147 requestCounter := 0
148
149 for ctx.Err() == nil {
150 result := make(map[string]int)
151 badRatioSeen := false
152 for i := 1; i <= iterationsToVerify && !badRatioSeen; i++ {
153 for j := 0; j < backendCount; j++ {
154 var peer peer.Peer
155 resp, err := client.SayHello(ctx, &hwpb.HelloRequest{
156 Name: fmt.Sprintf("request:%d", requestCounter),
157 }, grpc.Peer(&peer))
158 requestCounter++
159 if err != nil {
160 return fmt.Errorf("RPC failed: %v", err)
161 }
162 log.Print("Greeting:", resp.GetMessage())
163
164 peerAddr := peer.Addr.String()
165 if !slices.Contains(wantPeers, peerAddr) {
166 return fmt.Errorf("peer address was not one of %q, got: %v", strings.Join(wantPeers, ", "), peerAddr)
167 }
168 result[peerAddr]++
169 time.Sleep(time.Millisecond)
170 }
171
172 // Verify the results of this iteration.
173 for _, count := range result {
174 if count == i {
175 continue
176 }
177 badRatioSeen = true
178 break
179 }
180 if !badRatioSeen {
181 log.Print("Got iteration with 1:1:1 distribution between addresses.")
182 }
183 }
184 if !badRatioSeen {
185 return nil
186 }
187 }
188 return fmt.Errorf("timeout waiting for 1:1:1 distribution between addresses %v", wantPeers)
189}

Callers 1

mainFunction · 0.70

Calls 8

PeerFunction · 0.92
ErrMethod · 0.80
JoinMethod · 0.80
SayHelloMethod · 0.65
ErrorfMethod · 0.65
PrintMethod · 0.65
StringMethod · 0.65
GetMessageMethod · 0.45

Tested by

no test coverage detected