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

Function unaryInterceptor

examples/features/interceptor/client/main.go:49–68  ·  view source on GitHub ↗

unaryInterceptor is an example unary interceptor.

(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption)

Source from the content-addressed store, hash-verified

47
48// unaryInterceptor is an example unary interceptor.
49func unaryInterceptor(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
50 var credsConfigured bool
51 for _, o := range opts {
52 _, ok := o.(grpc.PerRPCCredsCallOption)
53 if ok {
54 credsConfigured = true
55 break
56 }
57 }
58 if !credsConfigured {
59 opts = append(opts, grpc.PerRPCCredentials(oauth.TokenSource{
60 TokenSource: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: fallbackToken}),
61 }))
62 }
63 start := time.Now()
64 err := invoker(ctx, method, req, reply, cc, opts...)
65 end := time.Now()
66 logger("RPC: %s, start time: %s, end time: %s, err: %v", method, start.Format("Basic"), end.Format(time.RFC3339), err)
67 return err
68}
69
70// wrappedStream wraps around the embedded grpc.ClientStream, and intercepts the RecvMsg and
71// SendMsg method call.

Callers

nothing calls this directly

Calls 3

PerRPCCredentialsFunction · 0.92
NowMethod · 0.80
loggerFunction · 0.70

Tested by

no test coverage detected