/
grpc_options.go
45 lines (40 loc) · 930 Bytes
/
grpc_options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
func appendMetadataGrpcOption(in []grpc.DialOption, key, value string) []grpc.DialOption {
in = append(in, grpc.WithChainUnaryInterceptor(func(
ctx context.Context,
method string,
req interface{},
reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
newCtx := metadata.AppendToOutgoingContext(
ctx,
key,
value,
)
return invoker(newCtx, method, req, reply, cc, opts...)
}))
in = append(in, grpc.WithChainStreamInterceptor(func(
ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
newCtx := metadata.AppendToOutgoingContext(
ctx,
key,
value,
)
return streamer(newCtx, desc, cc, method, opts...)
}))
return in
}