/
grpcclient.go
187 lines (156 loc) · 4.76 KB
/
grpcclient.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package grpc
import (
"context"
"crypto/tls"
"crypto/x509"
"strconv"
"strings"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"github.com/dezswap/cosmwasm-etl/configs"
"github.com/stretchr/testify/mock"
)
// BACK_OFF_MAX_DELAY is default max delay for exponential backoff algorithm
const BACK_OFF_MAX_DELAY = 3 * time.Second
var serviceDomain string
type ServiceDesc interface {
GetConnection(opts ...grpc.DialOption) *grpc.ClientConn
GetConnectionWithContext(ctx context.Context, opts ...grpc.DialOption) *grpc.ClientConn
CloseConnection() error
}
func SetServiceDomain(domain string) {
serviceDomain = domain
}
type serviceDescImpl struct {
alias string
destination string
destinationPort int
backoffMaxDelay time.Duration
noTLS bool
serviceConn *grpc.ClientConn
}
func GetServiceDesc(alias string, c configs.GrpcConfig) ServiceDesc {
return &serviceDescImpl{
alias: alias,
destination: c.Host,
destinationPort: c.Port,
backoffMaxDelay: c.BackoffMaxDelay.Duration,
noTLS: c.NoTLS,
}
}
// GetConnection returns gRPC client connection
func (c *serviceDescImpl) GetConnection(opts ...grpc.DialOption) *grpc.ClientConn {
return c.GetConnectionWithContext(context.Background(), opts...)
}
// GetConnectionWithContext returns gRPC client connection with context
func (c *serviceDescImpl) GetConnectionWithContext(ctx context.Context, opts ...grpc.DialOption) *grpc.ClientConn {
if c.serviceConn != nil {
return c.serviceConn
}
dest := c.destination
// Alias to name
if dest != "" && !strings.Contains(dest, ".") && dest != "localhost" {
if serviceDomain == "" {
panic("service domain is not set")
}
dest = strings.Join([]string{dest, serviceDomain}, ".")
}
// Assemble port
dest = strings.Join([]string{dest, strconv.Itoa(c.destinationPort)}, ":")
options := opts
if !c.noTLS {
// FIXME: it is very slow procedure, we should have cert in local filesystem or cache it
conn, err := tls.Dial("tcp", dest, &tls.Config{
InsecureSkipVerify: true,
})
if err != nil {
logger.Warn("cannot dial to TLS enabled server:", err)
return nil
}
certs := conn.ConnectionState().PeerCertificates
err = conn.Close()
if err != nil {
logger.Warn("cannot close TLS connection:", err)
}
pool := x509.NewCertPool()
pool.AddCert(certs[0])
clientCert := credentials.NewClientTLSFromCert(pool, "")
options = append(options, grpc.WithTransportCredentials(clientCert))
} else {
options = append(options, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
backoff := backoff.DefaultConfig
// We apply backoff max delay (3 seconds by default)
backoff.MaxDelay = func() time.Duration {
delay := c.backoffMaxDelay
if delay != 0 {
return delay
}
return BACK_OFF_MAX_DELAY
}()
options = append(options, grpc.WithConnectParams(grpc.ConnectParams{
Backoff: backoff,
}))
logger.Info("dial [", c.alias, "]: ", dest)
conn, err := grpc.DialContext(ctx, dest, options...)
if err != nil {
// It is very likely unreachable code under non-blocking dialing
logger.Panic("cannot connect to gRPC server:", err)
}
c.serviceConn = conn
// State change logger
go func() {
isReady := false
for {
s := conn.GetState()
if s == connectivity.Shutdown {
logger.Info("connection state [", c.alias, "]: ", s)
break
} else if isReady && s == connectivity.TransientFailure {
logger.Info("connection state [", c.alias, "]: ", s)
isReady = false
}
if !conn.WaitForStateChange(ctx, s) {
// Logging last state just after ctx expired
// Even this can miss last "shutdown" state. very unlikely.
last := conn.GetState()
if s != last {
logger.Info("connection state [", c.alias, "]: ", last)
}
break
}
}
}()
return c.serviceConn
}
// CloseConnection closes existing connection
func (c *serviceDescImpl) CloseConnection() error {
if c.serviceConn == nil {
// Very unlikely
logger.Warn("connection is already closed or not connected yet")
return nil
}
err := c.serviceConn.Close()
if err != nil {
return err
}
c.serviceConn = nil
return nil
}
type ServiceDescMock struct {
mock.Mock
}
var _ ServiceDesc = &ServiceDescMock{}
func (s *ServiceDescMock) GetConnection(opts ...grpc.DialOption) *grpc.ClientConn {
return s.Mock.MethodCalled("GetConnection", opts).Get(0).(*grpc.ClientConn)
}
func (s *ServiceDescMock) GetConnectionWithContext(ctx context.Context, opts ...grpc.DialOption) *grpc.ClientConn {
return s.Mock.MethodCalled("GetConnectionWithContext", ctx, opts).Get(0).(*grpc.ClientConn)
}
func (s *ServiceDescMock) CloseConnection() error {
return s.Mock.MethodCalled("CloseConnection").Error(0)
}