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

Method ServerStreamingEcho

examples/features/metadata/server/main.go:79–114  ·  view source on GitHub ↗
(in *pb.EchoRequest, stream pb.Echo_ServerStreamingEchoServer)

Source from the content-addressed store, hash-verified

77}
78
79func (s *server) ServerStreamingEcho(in *pb.EchoRequest, stream pb.Echo_ServerStreamingEchoServer) error {
80 fmt.Printf("--- ServerStreamingEcho ---\n")
81 // Create trailer in defer to record function return time.
82 defer func() {
83 trailer := metadata.Pairs("timestamp", time.Now().Format(timestampFormat))
84 stream.SetTrailer(trailer)
85 }()
86
87 // Read metadata from client.
88 md, ok := metadata.FromIncomingContext(stream.Context())
89 if !ok {
90 return status.Errorf(codes.DataLoss, "ServerStreamingEcho: failed to get metadata")
91 }
92 if t, ok := md["timestamp"]; ok {
93 fmt.Printf("timestamp from metadata:\n")
94 for i, e := range t {
95 fmt.Printf(" %d. %s\n", i, e)
96 }
97 }
98
99 // Create and send header.
100 header := metadata.New(map[string]string{"location": "MTV", "timestamp": time.Now().Format(timestampFormat)})
101 stream.SendHeader(header)
102
103 fmt.Printf("request received: %v\n", in)
104
105 // Read requests and send responses.
106 for i := 0; i < streamingCount; i++ {
107 fmt.Printf("echo message %v\n", in.Message)
108 err := stream.Send(&pb.EchoResponse{Message: in.Message})
109 if err != nil {
110 return err
111 }
112 }
113 return nil
114}
115
116func (s *server) ClientStreamingEcho(stream pb.Echo_ClientStreamingEchoServer) error {
117 fmt.Printf("--- ClientStreamingEcho ---\n")

Callers

nothing calls this directly

Calls 10

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

Tested by

no test coverage detected