-
Notifications
You must be signed in to change notification settings - Fork 10
/
token_bucket_limiter.go
53 lines (46 loc) · 1.16 KB
/
token_bucket_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
package limiter
import (
"sync"
"time"
)
// TokenBucketLimiter 令牌桶限流器
type TokenBucketLimiter struct {
capacity int // 容量
currentTokens int // 令牌数量
rate int // 发放令牌速率/秒
lastTime time.Time // 上次发放令牌时间
mutex sync.Mutex // 避免并发问题
}
func NewTokenBucketLimiter(capacity, rate int) *TokenBucketLimiter {
return &TokenBucketLimiter{
capacity: capacity,
rate: rate,
lastTime: time.Now(),
}
}
func (l *TokenBucketLimiter) TryAcquire() bool {
l.mutex.Lock()
defer l.mutex.Unlock()
// 尝试发放令牌
now := time.Now()
// 距离上次发放令牌的时间
interval := now.Sub(l.lastTime)
if interval >= time.Second {
// 当前令牌数量+距离上次发放令牌的时间(秒)*发放令牌速率
l.currentTokens = minInt(l.capacity, l.currentTokens+int(interval/time.Second)*l.rate)
l.lastTime = now
}
// 如果没有令牌,请求失败
if l.currentTokens == 0 {
return false
}
// 如果有令牌,当前令牌-1,请求成功
l.currentTokens--
return true
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}