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

Function unaryInterceptor

examples/features/metadata_interceptor/server/main.go:46–68  ·  view source on GitHub ↗
(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler)

Source from the content-addressed store, hash-verified

44}
45
46func unaryInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
47 md, ok := metadata.FromIncomingContext(ctx)
48 if !ok {
49 return nil, errMissingMetadata
50 }
51
52 // Create and set metadata from interceptor to server.
53 md.Append("key1", "value1")
54 ctx = metadata.NewIncomingContext(ctx, md)
55
56 // Call the handler to complete the normal execution of the RPC.
57 resp, err := handler(ctx, req)
58
59 // Create and set header metadata from interceptor to client.
60 header := metadata.Pairs("header-key", "val")
61 grpc.SetHeader(ctx, header)
62
63 // Create and set trailer metadata from interceptor to client.
64 trailer := metadata.Pairs("trailer-key", "val")
65 grpc.SetTrailer(ctx, trailer)
66
67 return resp, err
68}
69
70func (s *server) UnaryEcho(ctx context.Context, in *pb.EchoRequest) (*pb.EchoResponse, error) {
71 fmt.Printf("--- UnaryEcho ---\n")

Callers

nothing calls this directly

Calls 6

FromIncomingContextFunction · 0.92
NewIncomingContextFunction · 0.92
PairsFunction · 0.92
SetHeaderFunction · 0.92
SetTrailerFunction · 0.92
AppendMethod · 0.80

Tested by

no test coverage detected