-
Notifications
You must be signed in to change notification settings - Fork 18
/
grpc.go
38 lines (33 loc) · 981 Bytes
/
grpc.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
package upstream
import (
"context"
"google.golang.org/grpc"
"time"
)
// PerRPCCredentials interface implementation.
func (upstream *Upstream) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"registration-token": upstream.registrationToken,
"session-token": upstream.sessionToken,
"worker-name": upstream.workerName,
}, nil
}
// PerRPCCredentials interface implementation.
func (upstream *Upstream) RequireTransportSecurity() bool {
return !upstream.rpcInsecure
}
func deadlineUnaryInterceptor(duration time.Duration) grpc.UnaryClientInterceptor {
return func(
ctx context.Context,
method string,
req,
reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
ctxWithDeadline, cancel := context.WithDeadline(ctx, time.Now().Add(duration))
defer cancel()
return invoker(ctxWithDeadline, method, req, reply, cc, opts...)
}
}