-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rate.go
222 lines (162 loc) · 3.95 KB
/
rate.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
package internal
import (
"net/http"
"strconv"
"strings"
"sync"
"time"
)
type RateLimit struct {
endpoints map[string]*Methods
appRate *Rate
}
type Methods struct {
methods map[string]*Rate
}
type Rate struct {
Seconds *RateTiming
Minutes *RateTiming
}
func NewRateLimit() *RateLimit {
return &RateLimit{
endpoints: map[string]*Methods{},
appRate: &Rate{
Seconds: &RateTiming{},
Minutes: &RateTiming{},
},
}
}
type RateTiming struct {
// The amount in seconds the rate limit should reset
Time int
// Maximum amount of requests in n seconds
Limit int
// Current count of requests made in n seconds
Count int
Ticker *time.Ticker
Mutex *sync.Mutex
}
func (r *RateTiming) tick() {
for range r.Ticker.C {
r.Mutex.Lock()
r.Count = 0
r.Mutex.Unlock()
}
}
// Checks if the *Rate is currently rate limited
func (r *RateLimit) IsRateLimited(rate *Rate) bool {
if rate.Seconds.Limit == 0 {
return false
}
rate.Seconds.Mutex.Lock()
defer rate.Seconds.Mutex.Unlock()
if rate.Seconds.Count >= rate.Seconds.Limit {
return true
}
rate.Minutes.Mutex.Lock()
defer rate.Minutes.Mutex.Unlock()
return rate.Minutes.Count >= rate.Minutes.Limit
}
func (r *RateLimit) Get(endpointName string, methodName string) *Rate {
endpoint := r.endpoints[endpointName]
if endpoint != nil {
rate := endpoint.methods[methodName]
if rate != nil {
return rate
}
return nil
}
return nil
}
func (r *RateLimit) Set(endpointName string, methodName string, rate *Rate) {
endpoint := r.endpoints[endpointName]
if endpoint == nil {
r.endpoints[endpointName] = &Methods{
methods: map[string]*Rate{},
}
if r.endpoints[endpointName].methods[methodName] == nil {
r.endpoints[endpointName].methods[methodName] = rate
go rate.Seconds.tick()
if rate.Minutes.Limit != 0 {
go rate.Minutes.tick()
}
}
return
}
methodRate := endpoint.methods[methodName]
// Update seconds count
updateRateCount(methodRate.Seconds, rate.Seconds)
// Update minutes count
updateRateCount(methodRate.Minutes, rate.Minutes)
}
func (r *RateLimit) SetAppRate(rate *Rate) {
if r.appRate.Seconds.Limit == 0 {
r.appRate = rate
go rate.Seconds.tick()
if rate.Minutes.Limit != 0 {
go rate.Minutes.tick()
}
return
}
// Update seconds count
updateRateCount(r.appRate.Seconds, rate.Seconds)
// Update minutes count
updateRateCount(r.appRate.Minutes, rate.Minutes)
}
func ParseHeaders(headers http.Header, limitHeader string, countHeader string) *Rate {
rate := &Rate{
Seconds: &RateTiming{},
Minutes: &RateTiming{},
}
rateLimit := headers.Get(limitHeader)
if rateLimit == "" {
return nil
}
// Obtaining Rate-Limit header values
rates := strings.Split(rateLimit, ",")
// Obtaining rate limit for seconds
limit, seconds := getNumberPairs(rates[0])
rate.Seconds = getRateTiming(limit, seconds)
if len(rates) == 2 {
// Obtaining rate limit for minutes
limit, seconds = getNumberPairs(rates[1])
rate.Minutes = getRateTiming(limit, seconds)
}
rateCount := headers.Get(countHeader)
if rateCount == "" {
return nil
}
// Obtaining rate counts
counts := strings.Split(rateCount, ",")
// Obtaining rate count for seconds
current, _ := getNumberPairs(counts[0])
rate.Seconds.Count = current
if len(counts) == 2 {
// Obtaining rate count for seconds
current, _ = getNumberPairs(counts[1])
rate.Minutes.Count = current
}
return rate
}
func getNumberPairs(str string) (int, int) {
numbers := strings.Split(str, ":")
number, _ := strconv.Atoi(numbers[0])
limit, _ := strconv.Atoi(numbers[1])
return number, limit
}
func getRateTiming(limit int, seconds int) *RateTiming {
timing := &RateTiming{
Time: seconds,
Limit: limit,
Count: 0,
Ticker: &time.Ticker{},
Mutex: &sync.Mutex{},
}
timing.Ticker = time.NewTicker(time.Duration(seconds) * time.Second)
return timing
}
func updateRateCount(old *RateTiming, new *RateTiming) {
old.Mutex.Lock()
defer old.Mutex.Unlock()
old.Count = new.Count
}