This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
/
rate_limiter.go
149 lines (131 loc) · 3.67 KB
/
rate_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
143
144
145
146
147
148
149
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use rl file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package util
import (
"sync"
"time"
)
// RateLimiter is used for limiting the rate of transporting.
type RateLimiter struct {
capacity int32
bucket int32
rate int32
ratePerWindow int32
window int64
last int64
mu sync.Mutex
}
// NewRateLimiter creates a RateLimiter instance.
// rate: how many tokens are generated per second. 0 represents that don't limit the rate.
// window: generating tokens interval (millisecond, [1,1000]).
// The production of rate and window should be division by 1000.
func NewRateLimiter(rate int32, window int64) *RateLimiter {
rl := new(RateLimiter)
rl.capacity = rate
rl.bucket = 0
rl.rate = rate
rl.setWindow(window)
rl.computeRatePerWindow()
rl.last = time.Now().UnixNano()
return rl
}
// AcquireBlocking acquires tokens. It will be blocking unit the bucket has enough required
// number of tokens.
func (rl *RateLimiter) AcquireBlocking(token int32) int32 {
return rl.acquire(token, true)
}
// AcquireNonBlocking acquires tokens. It will return -1 immediately when there is no enough
// number of tokens.
func (rl *RateLimiter) AcquireNonBlocking(token int32) int32 {
return rl.acquire(token, false)
}
// SetRate sets rate of RateLimiter.
func (rl *RateLimiter) SetRate(rate int32) {
if rl.rate != rate {
rl.capacity = rate
rl.rate = rate
rl.computeRatePerWindow()
}
}
func (rl *RateLimiter) acquire(token int32, blocking bool) int32 {
if rl.capacity <= 0 || token < 1 {
return token
}
tmpCapacity := Max(rl.capacity, token)
var process func() int32
process = func() int32 {
now := time.Now().UnixNano()
newTokens := rl.createTokens(now)
curTotal := Min(newTokens+rl.bucket, tmpCapacity)
if curTotal >= token {
rl.bucket = curTotal - token
rl.last = now
return token
}
if blocking {
rl.blocking(token - curTotal)
return process()
}
return -1
}
rl.mu.Lock()
defer rl.mu.Unlock()
return process()
}
func (rl *RateLimiter) setWindow(window int64) {
if window >= 1 && window <= 1000 {
rl.window = window
} else if window < 1 {
rl.window = 1
} else {
rl.window = 1000
}
}
func (rl *RateLimiter) computeRatePerWindow() {
if rl.rate <= 0 {
return
}
ratePerWindow := int32(int64(rl.rate) * int64(rl.window) / 1000)
if ratePerWindow > 0 {
rl.ratePerWindow = ratePerWindow
return
}
rl.ratePerWindow = 1
rl.setWindow(int64(rl.ratePerWindow * 1000 / rl.rate))
}
func (rl *RateLimiter) createTokens(timeNano int64) int32 {
diff := timeNano - rl.last
if diff < time.Millisecond.Nanoseconds() {
return 0
}
return int32(diff/(rl.window*time.Millisecond.Nanoseconds())) * rl.ratePerWindow
}
func (rl *RateLimiter) blocking(requiredToken int32) {
if requiredToken <= 0 {
return
}
windowCount := int64(Max(requiredToken/rl.ratePerWindow, 1))
time.Sleep(time.Duration(windowCount * rl.window * time.Millisecond.Nanoseconds()))
}
// TransRate trans the rate to multiples of 1000
// For NewRateLimiter, the production of rate should be division by 1000.
func TransRate(rate int) int32 {
if rate <= 0 {
rate = 10 * 1024 * 1024
}
rate = (rate/1000 + 1) * 1000
return int32(rate)
}