forked from vitessio/vitess
/
client.go
122 lines (99 loc) · 4.56 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright 2017 Google 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 grpcclient contains utility methods for gRPC client implementations
// to use. It also supports plug-in authentication.
package grpcclient
import (
"flag"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/xsec-lab/go/vt/grpccommon"
"github.com/xsec-lab/go/vt/vttls"
"github.com/xsec-lab/go/vt/log"
)
var (
keepaliveTime = flag.Duration("grpc_keepalive_time", 0, "After a duration of this time if the client doesn't see any activity it pings the server to see if the transport is still alive.")
keepaliveTimeout = flag.Duration("grpc_keepalive_timeout", 0, "After having pinged for keepalive check, the client waits for a duration of Timeout and if no activity is seen even after that the connection is closed.")
initialConnWindowSize = flag.Int("grpc_initial_conn_window_size", 0, "grpc initial connection window size")
initialWindowSize = flag.Int("grpc_initial_window_size", 0, "grpc initial window size")
)
// FailFast is a self-documenting type for the grpc.FailFast.
type FailFast bool
// grpcDialOptions is a registry of functions that append grpcDialOption to use when dialing a service
var grpcDialOptions []func(opts []grpc.DialOption) ([]grpc.DialOption, error)
// RegisterGRPCDialOptions registers an implementation of AuthServer.
func RegisterGRPCDialOptions(grpcDialOptionsFunc func(opts []grpc.DialOption) ([]grpc.DialOption, error)) {
grpcDialOptions = append(grpcDialOptions, grpcDialOptionsFunc)
}
// Dial creates a grpc connection to the given target.
// failFast is a non-optional parameter because callers are required to specify
// what that should be.
func Dial(target string, failFast FailFast, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
grpccommon.EnableTracingOpt()
newopts := []grpc.DialOption{
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(*grpccommon.MaxMessageSize),
grpc.MaxCallSendMsgSize(*grpccommon.MaxMessageSize),
grpc.FailFast(bool(failFast)),
),
}
if *keepaliveTime != 0 || *keepaliveTimeout != 0 {
kp := keepalive.ClientParameters{
// After a duration of this time if the client doesn't see any activity it pings the server to see if the transport is still alive.
Time: *keepaliveTime,
// After having pinged for keepalive check, the client waits for a duration of Timeout and if no activity is seen even after that
// the connection is closed. (This will eagerly fail inflight grpc requests even if they don't have timeouts.)
Timeout: *keepaliveTimeout,
PermitWithoutStream: true,
}
newopts = append(newopts, grpc.WithKeepaliveParams(kp))
}
if *initialConnWindowSize != 0 {
newopts = append(newopts, grpc.WithInitialConnWindowSize(int32(*initialConnWindowSize)))
}
if *initialWindowSize != 0 {
newopts = append(newopts, grpc.WithInitialWindowSize(int32(*initialWindowSize)))
}
newopts = append(newopts, opts...)
var err error
for _, grpcDialOptionInitializer := range grpcDialOptions {
newopts, err = grpcDialOptionInitializer(newopts)
if err != nil {
log.Fatalf("There was an error initializing client grpc.DialOption: %v", err)
}
}
if *grpccommon.EnableGRPCPrometheus {
newopts = append(newopts, grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor))
newopts = append(newopts, grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor))
}
return grpc.Dial(target, newopts...)
}
// SecureDialOption returns the gRPC dial option to use for the
// given client connection. It is either using TLS, or Insecure if
// nothing is set.
func SecureDialOption(cert, key, ca, name string) (grpc.DialOption, error) {
// No security options set, just return.
if (cert == "" || key == "") && ca == "" {
return grpc.WithInsecure(), nil
}
// Load the config.
config, err := vttls.ClientConfig(cert, key, ca, name)
if err != nil {
return nil, err
}
// Create the creds server options.
creds := credentials.NewTLS(config)
return grpc.WithTransportCredentials(creds), nil
}