forked from streamingfast/dgrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
83 lines (73 loc) · 2.95 KB
/
client.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2019 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dgrpc
import (
"time"
"go.opencensus.io/plugin/ocgrpc"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
)
// var balancerDialOption = grpc.WithBalancerName(roundrobin.Name)
var cfg = `
{
"load_balancing_config": { "round_robin": {} }
}`
var serviceConfig = grpc.WithDefaultServiceConfig(cfg)
var insecureDialOption = grpc.WithInsecure()
var tracingDialOption = grpc.WithStatsHandler(&ocgrpc.ClientHandler{})
var tlsClientDialOption = grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, ""))
var maxCallRecvMsgSize = 1024 * 1024 * 1024
var defaultCallOptions = []grpc.CallOption{
grpc.MaxCallRecvMsgSize(maxCallRecvMsgSize),
grpc.WaitForReady(true),
}
// very lightweight keepalives: see https://www.evanjones.ca/grpc-is-tricky.html
var keepaliveDialOption = grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 5 * time.Minute, // send pings every ... when there is no activity
Timeout: 5 * time.Second, // wait that amount of time for ping ack before considering the connection dead
PermitWithoutStream: false,
})
// NewInternalClient creates a grpc ClientConn with keep alive, tracing and plain text
// connection (so no TLS involved, the server must also listen to a plain text socket).
//
// It's possible to debug low-level message using `export GODEBUG=http2debug=2`.
func NewInternalClient(remoteAddr string) (*grpc.ClientConn, error) {
conn, err := grpc.Dial(
remoteAddr,
serviceConfig,
insecureDialOption,
keepaliveDialOption,
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()),
grpc.WithDefaultCallOptions(defaultCallOptions...),
)
return conn, err
}
// NewExternalClient creates a grpc ClientConn with keepalive, tracing and secure TLS
func NewExternalClient(remoteAddr string, extraOpts ...grpc.DialOption) (*grpc.ClientConn, error) {
opts := []grpc.DialOption{
serviceConfig,
keepaliveDialOption,
tlsClientDialOption,
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()),
grpc.WithDefaultCallOptions(defaultCallOptions...),
}
if len(extraOpts) > 0 {
opts = append(opts, extraOpts...)
}
return grpc.Dial(remoteAddr, opts...)
}