-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaky_bucket.go
91 lines (76 loc) · 1.65 KB
/
leaky_bucket.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
package limiter
import (
"sync"
"time"
)
type LbConfig struct {
Rate float64
MaxSlack int64
}
type LeakyBucket struct {
*LbConfig
m sync.Mutex
perRequest time.Duration
bufferTime time.Duration
slackTime time.Duration
lastTime time.Time
}
var _ Limiter = &LeakyBucket{}
func NewLeakyBucket(c *LbConfig) Limiter {
lb := &LeakyBucket{
LbConfig: c,
m: sync.Mutex{},
bufferTime: 0,
lastTime: time.Time{},
}
// 速率
lb.perRequest = time.Second / time.Duration(c.Rate)
// 松弛度
lb.slackTime = (^time.Duration(c.MaxSlack) * time.Second / time.Duration(c.Rate)) + 1
return lb
}
func (lb *LeakyBucket) Take() error {
lb.m.Lock()
defer lb.m.Unlock()
n := time.Now()
if lb.lastTime.Second() == 0 {
lb.lastTime = n
return nil
}
if lb.MaxSlack > 0 {
return lb.withSlack()
} else {
return lb.withoutSlack()
}
}
func (lb *LeakyBucket) withoutSlack() error {
n := time.Now()
lb.bufferTime = lb.perRequest - n.Sub(lb.lastTime)
// 请求间隔时间+buffer时间 < 速率, 拒绝服务
if lb.bufferTime > 0 {
return ErrNoTEnoughToken
} else {
lb.lastTime = n
}
return nil
}
func (lb *LeakyBucket) withSlack() error{
n := time.Now()
lb.bufferTime += lb.perRequest - n.Sub(lb.lastTime)
// 请求间隔时间+buffer时间 < 速率, 拒绝服务
if lb.bufferTime > 0 {
return ErrNoTEnoughToken
} else {
lb.lastTime = n
}
// 允许抵消的最长时间
if lb.bufferTime < lb.slackTime {
lb.bufferTime = lb.slackTime
}
return nil
}
func (lb *LeakyBucket) Cnt() int64 {
n := time.Now()
bufferTime := lb.bufferTime + lb.perRequest - n.Sub(lb.lastTime)
return int64(bufferTime / lb.perRequest)
}