-
Notifications
You must be signed in to change notification settings - Fork 469
/
qps_limiter.go
executable file
·142 lines (117 loc) · 3.26 KB
/
qps_limiter.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
package qpslimiter
import (
"strconv"
"sync"
"github.com/go-chassis/go-chassis/core/archaius"
"github.com/go-chassis/go-chassis/core/lager"
"go.uber.org/ratelimit"
)
// constant qps default rate
const (
DefaultRate = 2147483647
)
// QPSLimiterMap qps limiter map struct
type QPSLimiterMap struct {
KeyMap map[string]ratelimit.Limiter
sync.RWMutex
}
// variables of qps limiter ansd mutex variable
var (
qpsLimiter *QPSLimiterMap
once = new(sync.Once)
)
// GetQPSTrafficLimiter get qps traffic limiter
func GetQPSTrafficLimiter() *QPSLimiterMap {
initializeMap := func() {
qpsLimiter = &QPSLimiterMap{}
qpsLimiter.KeyMap = make(map[string]ratelimit.Limiter)
}
once.Do(initializeMap)
return qpsLimiter
}
// ProcessQPSTokenReq process qps token request
func (qpsL *QPSLimiterMap) ProcessQPSTokenReq(key string, qpsRate int) {
qpsL.RLock()
limiter, ok := qpsL.KeyMap[key]
if !ok {
qpsL.RUnlock()
//If the key operation is not present in the map, then add the new key operation to the map
qpsL.ProcessDefaultRateRpsTokenReq(key, qpsRate)
return
}
qpsL.RUnlock()
limiter.Take()
return
}
// ProcessDefaultRateRpsTokenReq process default rate pps token request
func (qpsL *QPSLimiterMap) ProcessDefaultRateRpsTokenReq(key string, qpsRate int) {
var bucketSize int
// add a limiter object for the newly found operation in the Default Hash map
// so that the default rate will be applied to subsequent token requests to this new operation
if qpsRate >= 1 {
bucketSize = int(qpsRate)
} else {
bucketSize = DefaultRate
}
qpsL.Lock()
// Create a new bucket for the new operation
r := ratelimit.New(bucketSize)
qpsL.KeyMap[key] = r
qpsL.Unlock()
r.Take()
return
}
// GetQPSRate get qps rate
func GetQPSRate(rateConfig string) (int, bool) {
qpsRate := archaius.GetInt(rateConfig, DefaultRate)
if qpsRate == DefaultRate {
return qpsRate, false
}
return qpsRate, true
}
// GetQPSRateWithPriority get qps rate with priority
func (qpsL *QPSLimiterMap) GetQPSRateWithPriority(op *OperationMeta) (int, string) {
var (
key string
qpsVal int
configExist bool
)
key = op.GetMicroServiceSchemaOpQualifiedName()
qpsVal, configExist = GetQPSRate(key)
if configExist {
return qpsVal, op.GetMicroServiceSchemaOpQualifiedName()
}
key = op.GetSchemaQualifiedName()
qpsVal, configExist = GetQPSRate(key)
if configExist {
return qpsVal, op.GetSchemaQualifiedName()
}
key = op.GetMicroServiceName()
qpsVal, configExist = GetQPSRate(key)
if configExist {
return qpsVal, op.GetMicroServiceName()
}
return DefaultRate, op.GetMicroServiceName()
}
// UpdateRateLimit update rate limit
func (qpsL *QPSLimiterMap) UpdateRateLimit(key string, value interface{}) {
switch v := value.(type) {
case int:
qpsL.ProcessDefaultRateRpsTokenReq(key, value.(int))
case string:
convertedIntValue, err := strconv.Atoi(value.(string))
if err != nil {
lager.Logger.Warnf("Invalid Value type received for QPSLateLimiter: %v", v, err)
} else {
qpsL.ProcessDefaultRateRpsTokenReq(key, convertedIntValue)
}
default:
lager.Logger.Warnf("Invalid Value type received for QPSLateLimiter: %v", v)
}
}
// DeleteRateLimiter delete rate limiter
func (qpsL *QPSLimiterMap) DeleteRateLimiter(key string) {
qpsL.Lock()
delete(qpsL.KeyMap, key)
qpsL.Unlock()
}