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

Method BidirectionalStreamingEcho

examples/features/metadata/server/main.go:156–195  ·  view source on GitHub ↗
(stream pb.Echo_BidirectionalStreamingEchoServer)

Source from the content-addressed store, hash-verified

154}
155
156func (s *server) BidirectionalStreamingEcho(stream pb.Echo_BidirectionalStreamingEchoServer) error {
157 fmt.Printf("--- BidirectionalStreamingEcho ---\n")
158 // Create trailer in defer to record function return time.
159 defer func() {
160 trailer := metadata.Pairs("timestamp", time.Now().Format(timestampFormat))
161 stream.SetTrailer(trailer)
162 }()
163
164 // Read metadata from client.
165 md, ok := metadata.FromIncomingContext(stream.Context())
166 if !ok {
167 return status.Errorf(codes.DataLoss, "BidirectionalStreamingEcho: failed to get metadata")
168 }
169
170 if t, ok := md["timestamp"]; ok {
171 fmt.Printf("timestamp from metadata:\n")
172 for i, e := range t {
173 fmt.Printf(" %d. %s\n", i, e)
174 }
175 }
176
177 // Create and send header.
178 header := metadata.New(map[string]string{"location": "MTV", "timestamp": time.Now().Format(timestampFormat)})
179 stream.SendHeader(header)
180
181 // Read requests and send responses.
182 for {
183 in, err := stream.Recv()
184 if err == io.EOF {
185 return nil
186 }
187 if err != nil {
188 return err
189 }
190 fmt.Printf("request received %v, sending echo\n", in)
191 if err := stream.Send(&pb.EchoResponse{Message: in.Message}); err != nil {
192 return err
193 }
194 }
195}
196
197func main() {
198 flag.Parse()

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
SendMethod · 0.65

Tested by

no test coverage detected