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

Function main

examples/features/flow_control/client/main.go:40–103  ·  view source on GitHub ↗

8KB

()

Source from the content-addressed store, hash-verified

38var payload = string(make([]byte, 8*1024)) // 8KB
39
40func main() {
41 flag.Parse()
42 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
43 defer cancel()
44
45 conn, err := grpc.NewClient(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
46 if err != nil {
47 log.Fatalf("did not connect: %v", err)
48 }
49 defer conn.Close()
50
51 c := pb.NewEchoClient(conn)
52
53 stream, err := c.BidirectionalStreamingEcho(ctx)
54 if err != nil {
55 log.Fatalf("Error creating stream: %v", err)
56 }
57 log.Printf("New stream began.")
58
59 // First we will send data on the stream until we cannot send any more. We
60 // detect this by not seeing a message sent 1s after the last sent message.
61 stopSending := grpcsync.NewEvent()
62 sentOne := make(chan struct{})
63 go func() {
64 i := 0
65 for !stopSending.HasFired() {
66 i++
67 if err := stream.Send(&pb.EchoRequest{Message: payload}); err != nil {
68 log.Fatalf("Error sending data: %v", err)
69 }
70 sentOne <- struct{}{}
71 }
72 log.Printf("Sent %v messages.", i)
73 stream.CloseSend()
74 }()
75
76 for !stopSending.HasFired() {
77 after := time.NewTimer(time.Second)
78 select {
79 case <-sentOne:
80 after.Stop()
81 case <-after.C:
82 log.Printf("Sending is blocked.")
83 stopSending.Fire()
84 <-sentOne
85 }
86 }
87
88 // Next, we wait 2 seconds before reading from the stream, to give the
89 // server an opportunity to block while sending its responses.
90 time.Sleep(2 * time.Second)
91
92 // Finally, read all the data sent by the server to allow it to unblock.
93 for i := 0; true; i++ {
94 if _, err := stream.Recv(); err != nil {
95 log.Printf("Read %v messages.", i)
96 if err == io.EOF {
97 log.Printf("Stream ended successfully.")

Callers

nothing calls this directly

Calls 15

HasFiredMethod · 0.95
StopMethod · 0.95
FireMethod · 0.95
NewClientFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
NewEventFunction · 0.92
NewTimerMethod · 0.80
ParseMethod · 0.65
FatalfMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected