forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc_client.go
539 lines (457 loc) · 13 KB
/
rpc_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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package rpc
import (
"crypto/tls"
"errors"
"net"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/TykTechnologies/tyk-pump/serializer"
"github.com/cenkalti/backoff/v4"
"github.com/gocraft/health"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
"golang.org/x/sync/singleflight"
"github.com/TykTechnologies/gorpc"
)
var (
GlobalRPCCallTimeout = 30 * time.Second
GlobalRPCPingTimeout = 60 * time.Second
Log = &logrus.Logger{}
Instrument *health.Stream
clientSingleton *gorpc.Client
clientSingletonMu sync.Mutex
funcClientSingleton *gorpc.DispatcherClient
dispatcher = gorpc.NewDispatcher()
addedFuncs = make(map[string]bool)
getGroupLoginCallback func(string, string) interface{}
emergencyModeCallback func()
emergencyModeLoadedCallback func()
killChan = make(chan int)
killed bool
id string
rpcLoginMu sync.Mutex
rpcConnectMu sync.Mutex
// UseSyncLoginRPC for tests where we dont need to execute as a goroutine
UseSyncLoginRPC bool
AnalyticsSerializers []serializer.AnalyticsSerializer
)
// ErrRPCIsDown this is returned when we can't reach rpc server.
var ErrRPCIsDown = errors.New("RPCStorageHandler: rpc is either down or was not configured")
// rpc.Login is callend may places we only need one in flight at a time.
var loginFlight singleflight.Group
var values rpcOpts
type rpcOpts struct {
// This tracks how many times have successfully logged. If this is 0 then we
// are in cold start.
loadCounts atomic.Value
emergencyMode atomic.Value
emergencyModeLoaded atomic.Value
config atomic.Value
clientIsConnected atomic.Value
}
func (r rpcOpts) ClientIsConnected() bool {
if v := r.clientIsConnected.Load(); v != nil {
return v.(bool)
}
return false
}
func (r rpcOpts) Config() Config {
if v := r.config.Load(); v != nil {
return v.(Config)
}
return Config{}
}
func (r *rpcOpts) Reset() {
r.loadCounts.Store(0)
r.emergencyMode.Store(false)
r.emergencyModeLoaded.Store(false)
r.clientIsConnected.Store(false)
}
func (r *rpcOpts) SetLoadCounts(n int) {
r.loadCounts.Store(n)
}
func (r *rpcOpts) IncrLoadCounts(n int) {
if v := r.loadCounts.Load(); v != nil {
r.loadCounts.Store(v.(int) + n)
} else {
r.loadCounts.Store(n)
}
}
func (r *rpcOpts) GetLoadCounts() int {
if v := r.loadCounts.Load(); v != nil {
return v.(int)
}
return 0
}
func (r *rpcOpts) SetEmergencyMode(n bool) {
r.emergencyMode.Store(n)
}
func (r *rpcOpts) GetEmergencyMode() bool {
if v := r.emergencyMode.Load(); v != nil {
return v.(bool)
}
return false
}
func (r *rpcOpts) SetEmergencyModeLoaded(n bool) {
r.emergencyModeLoaded.Store(n)
}
func (r *rpcOpts) GetEmergencyModeLoaded() bool {
if v := r.emergencyModeLoaded.Load(); v != nil {
return v.(bool)
}
return false
}
const (
ClientSingletonCall = "gorpcClientCall"
FuncClientSingletonCall = "gorpcDispatcherClientCall"
)
type Config struct {
UseSSL bool `json:"use_ssl"`
SSLInsecureSkipVerify bool `json:"ssl_insecure_skip_verify"`
SSLMinVersion uint16 `json:"ssl_min_version"`
SSLMaxVersion uint16 `json:"ssl_max_version"`
ConnectionString string `json:"connection_string"`
RPCKey string `json:"rpc_key"`
APIKey string `json:"api_key"`
GroupID string `json:"group_id"`
CallTimeout int `json:"call_timeout"`
PingTimeout int `json:"ping_timeout"`
RPCPoolSize int `json:"rpc_pool_size"`
}
func IsEmergencyMode() bool {
return values.GetEmergencyMode()
}
func LoadCount() int {
return values.GetLoadCounts()
}
func Reset() {
clientSingleton.Stop()
clientSingleton = nil
funcClientSingleton = nil
values.Reset()
}
func ResetEmergencyMode() {
values.SetEmergencyMode(false)
values.SetEmergencyModeLoaded(false)
}
func EmitErrorEvent(jobName string, funcName string, err error) {
if Instrument == nil {
return
}
job := Instrument.NewJob(jobName)
if emitErr := job.EventErr(funcName, err); emitErr != nil {
Log.WithError(emitErr).WithFields(logrus.Fields{
"jobName": jobName,
"funcName": funcName,
})
}
}
func EmitErrorEventKv(jobName string, funcName string, err error, kv map[string]string) {
if Instrument == nil {
return
}
job := Instrument.NewJob(jobName)
if emitErr := job.EventErrKv(funcName, err, kv); emitErr != nil {
Log.WithError(emitErr).WithFields(logrus.Fields{
"jobName": jobName,
"funcName": funcName,
"kv": kv,
})
}
}
// Connect will establish a connection to the RPC server specified in connection options
func Connect(connConfig Config, suppressRegister bool, dispatcherFuncs map[string]interface{},
getGroupLoginFunc func(string, string) interface{},
emergencyModeFunc func(),
emergencyModeLoadedFunc func()) bool {
rpcConnectMu.Lock()
defer rpcConnectMu.Unlock()
values.config.Store(connConfig)
getGroupLoginCallback = getGroupLoginFunc
emergencyModeCallback = emergencyModeFunc
emergencyModeLoadedCallback = emergencyModeLoadedFunc
if values.ClientIsConnected() {
Log.Debug("Using RPC singleton for connection")
return true
}
if clientSingleton != nil {
return !values.GetEmergencyMode()
}
// RPC Client is unset
// Set up the cache
Log.Info("Setting new RPC connection!")
connID := uuid.NewV4().String()
// Length should fit into 1 byte. Protection if we decide change uuid in future.
if len(connID) > 255 {
panic("connID is too long")
}
if values.Config().UseSSL {
clientCfg := &tls.Config{
InsecureSkipVerify: values.Config().SSLInsecureSkipVerify,
MinVersion: values.Config().SSLMinVersion,
MaxVersion: values.Config().SSLMaxVersion,
}
clientSingleton = gorpc.NewTLSClient(values.Config().ConnectionString, clientCfg)
} else {
clientSingleton = gorpc.NewTCPClient(values.Config().ConnectionString)
}
if Log.Level != logrus.DebugLevel {
clientSingleton.LogError = gorpc.NilErrorLogger
}
clientSingleton.OnConnect = onConnectFunc
clientSingleton.Conns = values.Config().RPCPoolSize
if clientSingleton.Conns == 0 {
clientSingleton.Conns = 20
}
clientSingleton.Dial = func(addr string) (conn net.Conn, err error) {
dialer := &net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 30 * time.Second,
}
useSSL := values.Config().UseSSL
if useSSL {
cfg := &tls.Config{
InsecureSkipVerify: values.Config().SSLInsecureSkipVerify,
MinVersion: values.Config().SSLMinVersion,
MaxVersion: values.Config().SSLMaxVersion,
}
conn, err = tls.DialWithDialer(dialer, "tcp", addr, cfg)
} else {
conn, err = dialer.Dial("tcp", addr)
}
if err != nil {
EmitErrorEventKv(
ClientSingletonCall,
"dial",
err,
map[string]string{
"addr": addr,
"useSSL": strconv.FormatBool(useSSL),
},
)
return
}
conn.Write([]byte("proto2"))
conn.Write([]byte{byte(len(connID))})
conn.Write([]byte(connID))
return conn, nil
}
clientSingleton.Start()
loadDispatcher(dispatcherFuncs)
if funcClientSingleton == nil {
funcClientSingleton = dispatcher.NewFuncClient(clientSingleton)
}
handleLogin()
if !suppressRegister {
register()
go checkDisconnect()
}
return true
}
func handleLogin() {
if UseSyncLoginRPC == true {
Login()
return
}
go Login()
}
// Login tries to login to the rpc sever. Returns true if it succeeds and false
// if it fails.
func Login() bool {
// I know this is extreme but rpc.Login() appears about 17 times and the
// methods appears to be sometimes called in goroutines.
//
// Unless someone audits to ensure all of where this appears the parent calls
// are not concurrent, this is a much safer solution.
v, _, _ := loginFlight.Do("Login", func() (interface{}, error) {
return loginBase(), nil
})
return v.(bool)
}
func loginBase() bool {
if !doLoginWithRetries(login, groupLogin, hasAPIKey, isGroup) {
rpcLoginMu.Lock()
if values.GetLoadCounts() == 0 && !values.GetEmergencyModeLoaded() {
Log.Warning("[RPC Store] --> Detected cold start, attempting to load from cache")
Log.Warning("[RPC Store] ----> Found APIs... beginning emergency load")
values.SetEmergencyModeLoaded(true)
if emergencyModeLoadedCallback != nil {
go emergencyModeLoadedCallback()
}
}
rpcLoginMu.Unlock()
return false
}
return true
}
func GroupLogin() bool {
return doGroupLogin(groupLogin)
}
func doGroupLogin(login func() error) bool {
if getGroupLoginCallback == nil {
Log.Error("GroupLogin call back is not set")
return false
}
b := backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 3)
return backoff.Retry(recoverOp(login), b) == nil
}
func groupLogin() error {
groupLoginData := getGroupLoginCallback(values.Config().APIKey, values.Config().GroupID)
ok, err := FuncClientSingleton("LoginWithGroup", groupLoginData)
if err != nil {
Log.WithError(err).Error("RPC Login failed")
EmitErrorEventKv(
FuncClientSingletonCall,
"LoginWithGroup",
err,
map[string]string{
"GroupID": values.Config().GroupID,
},
)
return err
}
if ok == false {
Log.Error("RPC Login incorrect")
return errLogFailed
}
Log.Debug("[RPC Store] Group Login complete")
values.IncrLoadCounts(1)
return nil
}
var errLogFailed = errors.New("Login incorrect")
func login() error {
k, err := FuncClientSingleton("Login", values.Config().APIKey)
if err != nil {
Log.WithError(err).Error("RPC Login failed")
EmitErrorEvent(FuncClientSingletonCall, "Login", err)
return err
}
ok := k.(bool)
if !ok {
Log.Error("RPC Login incorrect")
return errLogFailed
}
Log.Debug("[RPC Store] Login complete")
values.IncrLoadCounts(1)
return nil
}
func hasAPIKey() bool {
return len(values.Config().APIKey) != 0
}
func isGroup() bool {
return values.Config().GroupID != ""
}
// doLoginWithRetries uses login as a login function by calling it with retries
// until it succeeds or ultimately fail.
//
// hasAPIKey is called to check whether config.APIKey is set if this function
// returns false we exit the process.
//
// isGroup returns true if the config.GroupID is set. If this returns true then
// we perform group login.
func doLoginWithRetries(login, group func() error, hasAPIKey, isGroup func() bool) bool {
Log.Debug("[RPC Store] Login initiated")
if !hasAPIKey() {
Log.Fatal("No API Key set!")
}
// If we have a group ID, lets login as a group
if isGroup() {
return doGroupLogin(group)
}
b := backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 3)
return backoff.Retry(recoverOp(login), b) == nil
}
func recoverOp(fn func() error) func() error {
n := 0
return func() error {
err := fn()
if err != nil {
if n == 0 {
// we failed at our first call so we are in emergency mode now
values.SetEmergencyMode(true)
}
n++
return err
}
if values.GetEmergencyMode() {
values.SetEmergencyMode(false)
values.SetEmergencyModeLoaded(false)
if emergencyModeCallback != nil {
emergencyModeCallback()
}
}
return nil
}
}
// FuncClientSingleton performs RPC call. This might be called before we have
// established RPC connection, in that case we perform a retry with exponential
// backoff ensuring indeed we can't connect to the rpc, this will eventually
// fall into emergency mode( That is handled outside of this function call)
func FuncClientSingleton(funcName string, request interface{}) (result interface{}, err error) {
be := backoff.Retry(func() error {
if !values.ClientIsConnected() {
return ErrRPCIsDown
}
result, err = funcClientSingleton.CallTimeout(funcName, request, GlobalRPCCallTimeout)
return nil
}, backoff.WithMaxRetries(
backoff.NewConstantBackOff(10*time.Millisecond), 3,
))
if be != nil {
err = be
}
return
}
var rpcConnectionsPool []net.Conn
func onConnectFunc(conn net.Conn) (net.Conn, string, error) {
values.clientIsConnected.Store(true)
remoteAddr := conn.RemoteAddr().String()
Log.WithField("remoteAddr", remoteAddr).Debug("connected to RPC server")
rpcConnectionsPool = append(rpcConnectionsPool, conn)
return conn, remoteAddr, nil
}
func CloseConnections() {
for k, v := range rpcConnectionsPool {
err := v.Close()
if err != nil {
Log.WithError(err).Error("closing connection")
} else {
rpcConnectionsPool = append(rpcConnectionsPool[:k], rpcConnectionsPool[k+1:]...)
}
}
}
func Disconnect() bool {
values.clientIsConnected.Store(false)
return true
}
func register() {
id = uuid.NewV4().String()
Log.Debug("RPC Client registered")
}
func checkDisconnect() {
res := <-killChan
Log.WithField("res", res).Info("RPC Client disconnecting")
killed = true
Disconnect()
}
func loadDispatcher(dispatcherFuncs map[string]interface{}) {
for funcName, funcBody := range dispatcherFuncs {
if addedFuncs[funcName] {
continue
}
dispatcher.AddFunc(funcName, funcBody)
addedFuncs[funcName] = true
}
}
// ForceConnected only intended to be used in tests
// do not use it for any other thing
func ForceConnected(t *testing.T) {
values.clientIsConnected.Store(true)
}
// SetEmergencyMode used in tests to force emergency mode
func SetEmergencyMode(t *testing.T, value bool) {
values.SetEmergencyMode(value)
}