forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_storage_handler.go
627 lines (493 loc) · 14.2 KB
/
rpc_storage_handler.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
package main
import (
"errors"
"github.com/garyburd/redigo/redis"
"github.com/lonelycode/go-uuid/uuid"
"github.com/lonelycode/gorpc"
"github.com/pmylund/go-cache"
"io"
"strings"
"time"
)
type InboundData struct {
KeyName string
Value string
SessionState string
Timeout int64
Per int64
Expire int64
}
type DefRequest struct {
OrgId string
Tags []string
}
type KeysValuesPair struct {
Keys []string
Values []string
}
var ErrorDenied error = errors.New("Access Denied")
// ------------------- CLOUD STORAGE MANAGER -------------------------------
var RPCClients = map[string]chan int{}
func ClearRPCClients() {
for _, c := range RPCClients {
c <- 1
}
}
// RPCStorageHandler is a storage manager that uses the redis database.
type RPCStorageHandler struct {
RPCClient *gorpc.Client
Client *gorpc.DispatcherClient
KeyPrefix string
HashKeys bool
UserKey string
Address string
cache *cache.Cache
killChan chan int
Connected bool
ID string
SuppressRegister bool
}
func (r *RPCStorageHandler) Register() {
r.ID = uuid.NewUUID().String()
myChan := make(chan int)
RPCClients[r.ID] = myChan
r.killChan = myChan
log.Debug("RPC Client registered")
}
func (r *RPCStorageHandler) checkDisconnect() {
select {
case res := <-r.killChan:
log.Debug("RPC Client disconnecting: ", res)
r.Disconnect()
}
}
// Connect will establish a connection to the DB
func (r *RPCStorageHandler) Connect() bool {
// Set up the cache
r.cache = cache.New(30*time.Second, 15*time.Second)
r.RPCClient = gorpc.NewTCPClient(r.Address)
r.RPCClient.OnConnect = r.OnConnectFunc
r.RPCClient.Conns = 10
r.RPCClient.Start()
d := GetDispatcher()
r.Client = d.NewFuncClient(r.RPCClient)
r.Login()
if !r.SuppressRegister {
r.Register()
go r.checkDisconnect()
}
return true
}
func (r *RPCStorageHandler) OnConnectFunc(remoteAddr string, rwc io.ReadWriteCloser) (io.ReadWriteCloser, error) {
r.Connected = true
return rwc, nil
}
func (r *RPCStorageHandler) Disconnect() bool {
if r.Connected {
r.RPCClient.Stop()
r.Connected = false
delete(RPCClients, r.ID)
}
return true
}
func (r *RPCStorageHandler) hashKey(in string) string {
if !r.HashKeys {
// Not hashing? Return the raw key
return in
}
return doHash(in)
}
func (r *RPCStorageHandler) fixKey(keyName string) string {
setKeyName := r.KeyPrefix + r.hashKey(keyName)
log.Debug("Input key was: ", setKeyName)
return setKeyName
}
func (r *RPCStorageHandler) cleanKey(keyName string) string {
setKeyName := strings.Replace(keyName, r.KeyPrefix, "", 1)
return setKeyName
}
func (r *RPCStorageHandler) Login() {
log.Debug("[RPC Store] Login initiated")
if len(r.UserKey) == 0 {
log.Fatal("No API Key set!")
}
ok, err := r.Client.Call("Login", r.UserKey)
if err != nil {
log.Fatal("RPC Login failed: ", err)
}
if !ok.(bool) {
log.Fatal("RPC Login incorrect")
}
log.Debug("[RPC Store] Login complete")
}
// GetKey will retreive a key from the database
func (r *RPCStorageHandler) GetKey(keyName string) (string, error) {
start := time.Now() // get current time
log.Debug("[STORE] Getting WAS: ", keyName)
log.Debug("[STORE] Getting: ", r.fixKey(keyName))
// Check the cache first
if config.SlaveOptions.EnableRPCCache {
cachedVal, found := r.cache.Get(r.fixKey(keyName))
if found {
elapsed := time.Since(start)
log.Debug("GetKey took ", elapsed)
log.Debug(cachedVal.(string))
return cachedVal.(string), nil
}
}
// Not cached
value, err := r.Client.Call("GetKey", r.fixKey(keyName))
if err != nil {
if r.IsAccessError(err) {
r.Login()
return r.GetKey(keyName)
}
log.Debug("Error trying to get value:", err)
return "", KeyError{}
}
elapsed := time.Since(start)
log.Debug("GetKey took ", elapsed)
if config.SlaveOptions.EnableRPCCache {
// Cache it
r.cache.Set(r.fixKey(keyName), value, cache.DefaultExpiration)
}
return value.(string), nil
}
func (r *RPCStorageHandler) GetRawKey(keyName string) (string, error) {
log.Error("Not Implemented!")
return "", nil
}
func (r *RPCStorageHandler) GetExp(keyName string) (int64, error) {
log.Debug("GetExp called")
value, err := r.Client.Call("GetExp", r.fixKey(keyName))
if err != nil {
if r.IsAccessError(err) {
r.Login()
return r.GetExp(keyName)
}
log.Error("Error trying to get TTL: ", err)
} else {
return value.(int64), nil
}
return 0, KeyError{}
}
// SetKey will create (or update) a key value in the store
func (r *RPCStorageHandler) SetKey(keyName string, sessionState string, timeout int64) error {
start := time.Now() // get current time
ibd := InboundData{
KeyName: r.fixKey(keyName),
SessionState: sessionState,
Timeout: timeout,
}
_, err := r.Client.Call("SetKey", ibd)
if r.IsAccessError(err) {
r.Login()
return r.SetKey(keyName, sessionState, timeout)
}
elapsed := time.Since(start)
log.Debug("SetKey took ", elapsed)
return nil
}
func (r *RPCStorageHandler) SetRawKey(keyName string, sessionState string, timeout int64) error {
return nil
}
// Decrement will decrement a key in redis
func (r *RPCStorageHandler) Decrement(keyName string) {
log.Warning("Decrement called")
_, err := r.Client.Call("Decrement", keyName)
if r.IsAccessError(err) {
r.Login()
r.Decrement(keyName)
return
}
}
// IncrementWithExpire will increment a key in redis
func (r *RPCStorageHandler) IncrememntWithExpire(keyName string, expire int64) int64 {
ibd := InboundData{
KeyName: keyName,
Expire: expire,
}
val, err := r.Client.Call("IncrememntWithExpire", ibd)
if r.IsAccessError(err) {
r.Login()
return r.IncrememntWithExpire(keyName, expire)
}
return val.(int64)
}
// GetKeys will return all keys according to the filter (filter is a prefix - e.g. tyk.keys.*)
func (r *RPCStorageHandler) GetKeys(filter string) []string {
log.Error("GetKeys Not Implemented")
return []string{}
}
// GetKeysAndValuesWithFilter will return all keys and their values with a filter
func (r *RPCStorageHandler) GetKeysAndValuesWithFilter(filter string) map[string]string {
searchStr := r.KeyPrefix + r.hashKey(filter) + "*"
log.Debug("[STORE] Getting list by: ", searchStr)
kvPair, err := r.Client.Call("GetKeysAndValuesWithFilter", searchStr)
if r.IsAccessError(err) {
r.Login()
return r.GetKeysAndValuesWithFilter(filter)
}
returnValues := make(map[string]string)
for i, v := range kvPair.(*KeysValuesPair).Keys {
returnValues[r.cleanKey(v)] = kvPair.(*KeysValuesPair).Values[i]
}
return returnValues
}
// GetKeysAndValues will return all keys and their values - not to be used lightly
func (r *RPCStorageHandler) GetKeysAndValues() map[string]string {
searchStr := r.KeyPrefix + "*"
kvPair, err := r.Client.Call("GetKeysAndValues", searchStr)
if r.IsAccessError(err) {
r.Login()
return r.GetKeysAndValues()
}
returnValues := make(map[string]string)
for i, v := range kvPair.(*KeysValuesPair).Keys {
returnValues[r.cleanKey(v)] = kvPair.(*KeysValuesPair).Values[i]
}
return returnValues
}
// DeleteKey will remove a key from the database
func (r *RPCStorageHandler) DeleteKey(keyName string) bool {
log.Debug("DEL Key was: ", keyName)
log.Debug("DEL Key became: ", r.fixKey(keyName))
ok, err := r.Client.Call("DeleteKey", r.fixKey(keyName))
if r.IsAccessError(err) {
r.Login()
return r.DeleteKey(keyName)
}
return ok.(bool)
}
// DeleteKey will remove a key from the database without prefixing, assumes user knows what they are doing
func (r *RPCStorageHandler) DeleteRawKey(keyName string) bool {
ok, err := r.Client.Call("DeleteRawKey", keyName)
if r.IsAccessError(err) {
r.Login()
return r.DeleteRawKey(keyName)
}
return ok.(bool)
}
// DeleteKeys will remove a group of keys in bulk
func (r *RPCStorageHandler) DeleteKeys(keys []string) bool {
if len(keys) > 0 {
asInterface := make([]string, len(keys))
for i, v := range keys {
asInterface[i] = r.fixKey(v)
}
log.Debug("Deleting: ", asInterface)
ok, err := r.Client.Call("DeleteKeys", asInterface)
if r.IsAccessError(err) {
r.Login()
return r.DeleteKeys(keys)
}
return ok.(bool)
} else {
log.Debug("RPCStorageHandler called DEL - Nothing to delete")
return true
}
return true
}
// DeleteKeys will remove a group of keys in bulk without a prefix handler
func (r *RPCStorageHandler) DeleteRawKeys(keys []string, prefix string) bool {
log.Error("DeleteRawKeys Not Implemented")
return false
}
// StartPubSubHandler will listen for a signal and run the callback with the message
func (r *RPCStorageHandler) StartPubSubHandler(channel string, callback func(redis.Message)) error {
log.Warning("NO PUBSUB DEFINED")
return nil
}
func (r *RPCStorageHandler) Publish(channel string, message string) error {
log.Warning("NO PUBSUB DEFINED")
return nil
}
func (r *RPCStorageHandler) GetAndDeleteSet(keyName string) []interface{} {
log.Error("GetAndDeleteSet Not implemented, please disable your purger")
return []interface{}{}
}
func (r *RPCStorageHandler) AppendToSet(keyName string, value string) {
ibd := InboundData{
KeyName: keyName,
Value: value,
}
_, err := r.Client.Call("AppendToSet", ibd)
if r.IsAccessError(err) {
r.Login()
r.AppendToSet(keyName, value)
return
}
}
// SetScrollingWindow is used in the rate limiter to handle rate limits fairly.
func (r *RPCStorageHandler) SetRollingWindow(keyName string, per int64, val string) (int, []interface{}) {
start := time.Now() // get current time
ibd := InboundData{
KeyName: keyName,
Per: per,
Expire: -1,
}
intVal, err := r.Client.Call("SetRollingWindow", ibd)
if r.IsAccessError(err) {
r.Login()
return r.SetRollingWindow(keyName, per, val)
}
elapsed := time.Since(start)
log.Debug("SetRollingWindow took ", elapsed)
return intVal.(int), []interface{}{}
}
func (r RPCStorageHandler) GetSet(keyName string) (map[string]string, error) {
log.Error("Not implemented")
return map[string]string{}, nil
}
func (r RPCStorageHandler) AddToSet(keyName string, value string) {
log.Error("Not implemented")
}
func (r RPCStorageHandler) RemoveFromSet(keyName string, value string) {
log.Error("Not implemented")
}
func (r RPCStorageHandler) IsAccessError(err error) bool {
if err != nil {
if err.Error() == "Access Denied" {
return true
}
return false
}
return false
}
// GetAPIDefinitions will pull API definitions from the RPC server
func (r *RPCStorageHandler) GetApiDefinitions(orgId string, tags []string) string {
dr := DefRequest{
OrgId: orgId,
Tags: tags,
}
defString, err := r.Client.Call("GetApiDefinitions", dr)
if err != nil {
if r.IsAccessError(err) {
r.Login()
return r.GetApiDefinitions(orgId, tags)
}
}
log.Debug("API Definitions retrieved")
return defString.(string)
}
// GetPolicies will pull Policies from the RPC server
func (r *RPCStorageHandler) GetPolicies(orgId string) string {
defString, err := r.Client.Call("GetPolicies", orgId)
if err != nil {
if r.IsAccessError(err) {
r.Login()
return r.GetPolicies(orgId)
}
}
return defString.(string)
}
// CheckForReload will start a long poll
func (r *RPCStorageHandler) CheckForReload(orgId string) {
log.Debug("[RPC STORE] Check Reload called...")
reload, err := r.Client.CallTimeout("CheckReload", orgId, time.Second*60)
if err != nil {
if r.IsAccessError(err) {
log.Warning("[RPC STORE] CheckReload: Not logged in")
r.Login()
}
} else {
log.Debug("[RPC STORE] CheckReload: Received response")
if reload.(bool) {
// Do the reload!
log.Warning("[RPC STORE] Received Reload instruction!")
go ReloadURLStructure()
}
}
}
func (r *RPCStorageHandler) StartRPCLoopCheck(orgId string) {
log.Info("Starting keyspace poller")
for {
r.CheckForKeyspaceChanges(orgId)
time.Sleep(30 * time.Second)
}
}
// CheckForKeyspaceChanges will poll for keysace changes
func (r *RPCStorageHandler) CheckForKeyspaceChanges(orgId string) {
keys, err := r.Client.Call("GetKeySpaceUpdate", orgId)
if err != nil {
if r.IsAccessError(err) {
r.Login()
r.CheckForKeyspaceChanges(orgId)
}
}
if keys == nil {
log.Error("Keys returned nil object, skipping check")
return
}
if len(keys.([]string)) > 0 {
log.Info("Keyspace changes detected, updating local cache")
go r.ProcessKeySpaceChanges(keys.([]string))
}
}
func (r *RPCStorageHandler) ProcessKeySpaceChanges(keys []string) {
for _, key := range keys {
log.Info("--> removing cached key: ", key)
handleDeleteKey(key, "-1")
}
}
func GetDispatcher() *gorpc.Dispatcher {
var Dispatch *gorpc.Dispatcher = gorpc.NewDispatcher()
Dispatch.AddFunc("Login", func(clientAddr string, userKey string) bool {
return false
})
Dispatch.AddFunc("GetKey", func(keyName string) (string, error) {
return "", nil
})
Dispatch.AddFunc("SetKey", func(ibd *InboundData) error {
return nil
})
Dispatch.AddFunc("GetExp", func(keyName string) (int64, error) {
return 0, nil
})
Dispatch.AddFunc("GetKeys", func(keyName string) ([]string, error) {
return []string{}, nil
})
Dispatch.AddFunc("DeleteKey", func(keyName string) (bool, error) {
return true, nil
})
Dispatch.AddFunc("DeleteRawKey", func(keyName string) (bool, error) {
return true, nil
})
Dispatch.AddFunc("GetKeysAndValues", func(searchString string) (*KeysValuesPair, error) {
return nil, nil
})
Dispatch.AddFunc("GetKeysAndValuesWithFilter", func(searchString string) (*KeysValuesPair, error) {
return nil, nil
})
Dispatch.AddFunc("DeleteKeys", func(keys []string) (bool, error) {
return true, nil
})
Dispatch.AddFunc("Decrement", func(keyName string) error {
return nil
})
Dispatch.AddFunc("IncrememntWithExpire", func(ibd *InboundData) (int64, error) {
return 0, nil
})
Dispatch.AddFunc("AppendToSet", func(ibd *InboundData) error {
return nil
})
Dispatch.AddFunc("SetRollingWindow", func(ibd *InboundData) (int, error) {
return 0, nil
})
Dispatch.AddFunc("GetApiDefinitions", func(dr *DefRequest) (string, error) {
return "", nil
})
Dispatch.AddFunc("GetPolicies", func(orgId string) (string, error) {
return "", nil
})
Dispatch.AddFunc("PurgeAnalyticsData", func(data string) error {
return nil
})
Dispatch.AddFunc("CheckReload", func(clientAddr string, orgId string) (bool, error) {
return false, nil
})
Dispatch.AddFunc("GetKeySpaceUpdate", func(clientAddr string, orgId string) ([]string, error) {
return []string{}, nil
})
return Dispatch
}