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

Method ClientStreamingEcho

examples/features/metadata/server/main.go:116–154  ·  view source on GitHub ↗
(stream pb.Echo_ClientStreamingEchoServer)

Source from the content-addressed store, hash-verified

114}
115
116func (s *server) ClientStreamingEcho(stream pb.Echo_ClientStreamingEchoServer) error {
117 fmt.Printf("--- ClientStreamingEcho ---\n")
118 // Create trailer in defer to record function return time.
119 defer func() {
120 trailer := metadata.Pairs("timestamp", time.Now().Format(timestampFormat))
121 stream.SetTrailer(trailer)
122 }()
123
124 // Read metadata from client.
125 md, ok := metadata.FromIncomingContext(stream.Context())
126 if !ok {
127 return status.Errorf(codes.DataLoss, "ClientStreamingEcho: failed to get metadata")
128 }
129 if t, ok := md["timestamp"]; ok {
130 fmt.Printf("timestamp from metadata:\n")
131 for i, e := range t {
132 fmt.Printf(" %d. %s\n", i, e)
133 }
134 }
135
136 // Create and send header.
137 header := metadata.New(map[string]string{"location": "MTV", "timestamp": time.Now().Format(timestampFormat)})
138 stream.SendHeader(header)
139
140 // Read requests and send responses.
141 var message string
142 for {
143 in, err := stream.Recv()
144 if err == io.EOF {
145 fmt.Printf("echo last received message\n")
146 return stream.SendAndClose(&pb.EchoResponse{Message: message})
147 }
148 message = in.Message
149 fmt.Printf("request received: %v, building echo\n", in)
150 if err != nil {
151 return err
152 }
153 }
154}
155
156func (s *server) BidirectionalStreamingEcho(stream pb.Echo_BidirectionalStreamingEchoServer) error {
157 fmt.Printf("--- BidirectionalStreamingEcho ---\n")

Callers

nothing calls this directly

Calls 11

PairsFunction · 0.92
FromIncomingContextFunction · 0.92
ErrorfFunction · 0.92
NewFunction · 0.92
NowMethod · 0.80
PrintfMethod · 0.65
SetTrailerMethod · 0.65
ContextMethod · 0.65
SendHeaderMethod · 0.65
RecvMethod · 0.65
SendAndCloseMethod · 0.65

Tested by

no test coverage detected