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

Method UnaryEcho

examples/features/metadata/server/main.go:50–77  ·  view source on GitHub ↗
(ctx context.Context, in *pb.EchoRequest)

Source from the content-addressed store, hash-verified

48}
49
50func (s *server) UnaryEcho(ctx context.Context, in *pb.EchoRequest) (*pb.EchoResponse, error) {
51 fmt.Printf("--- UnaryEcho ---\n")
52 // Create trailer in defer to record function return time.
53 defer func() {
54 trailer := metadata.Pairs("timestamp", time.Now().Format(timestampFormat))
55 grpc.SetTrailer(ctx, trailer)
56 }()
57
58 // Read metadata from client.
59 md, ok := metadata.FromIncomingContext(ctx)
60 if !ok {
61 return nil, status.Errorf(codes.DataLoss, "UnaryEcho: failed to get metadata")
62 }
63 if t, ok := md["timestamp"]; ok {
64 fmt.Printf("timestamp from metadata:\n")
65 for i, e := range t {
66 fmt.Printf(" %d. %s\n", i, e)
67 }
68 }
69
70 // Create and send header.
71 header := metadata.New(map[string]string{"location": "MTV", "timestamp": time.Now().Format(timestampFormat)})
72 grpc.SendHeader(ctx, header)
73
74 fmt.Printf("request received: %v, sending echo\n", in)
75
76 return &pb.EchoResponse{Message: in.Message}, nil
77}
78
79func (s *server) ServerStreamingEcho(in *pb.EchoRequest, stream pb.Echo_ServerStreamingEchoServer) error {
80 fmt.Printf("--- ServerStreamingEcho ---\n")

Callers

nothing calls this directly

Calls 8

PairsFunction · 0.92
SetTrailerFunction · 0.92
FromIncomingContextFunction · 0.92
ErrorfFunction · 0.92
NewFunction · 0.92
SendHeaderFunction · 0.92
NowMethod · 0.80
PrintfMethod · 0.65

Tested by

no test coverage detected