-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratelimit.go
101 lines (83 loc) · 1.77 KB
/
ratelimit.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
package ratelimit
import (
"time"
)
type Limiter struct {
duration time.Duration
amount uint64
limit uint64
slot chan bool
ticker *time.Ticker
preFill uint64
}
func NewLimiter(options ...LimiterOptionFunc) (*Limiter, error) {
l := &Limiter{}
for _, fn := range options {
if fn == nil {
continue
}
if err := fn(l); err != nil {
return nil, err
}
}
l.slot = make(chan bool, l.limit)
if l.preFill != 0 {
if l.preFill > l.limit {
l.preFill = l.limit
}
for i := uint64(0); i < l.preFill; i++ {
l.slot <- true
}
}
if l.duration != 0 {
l.ticker = time.NewTicker(l.duration)
go func(b *Limiter) {
for range b.ticker.C {
b.refill()
}
}(l)
}
return l, nil
}
func (l *Limiter) Take() {
<-l.slot
}
func (l *Limiter) refill() {
amount := l.amount
if l.limit != 0 && uint64(len(l.slot))+amount > l.limit {
amount = l.limit - uint64(len(l.slot))
}
for i := uint64(0); i < amount; i++ {
l.slot <- true
}
}
// LimiterOptionFunc can be used to customize a new Bucket
type LimiterOptionFunc func(limiter *Limiter) error
// WithPreFill fills the bucket with the specified amount of slots
func WithPreFill(count uint64) LimiterOptionFunc {
return func(l *Limiter) error {
l.preFill = count
return nil
}
}
// WithLimit sets the limit for the bucket
func WithLimit(count uint64) LimiterOptionFunc {
return func(l *Limiter) error {
l.limit = count
return nil
}
}
// WithAmount sets the amount of slots to refill
func WithAmount(amount uint64) LimiterOptionFunc {
return func(l *Limiter) error {
l.amount = amount
return nil
}
}
// WithDuration sets the duration for the refill
func WithDuration(duration time.Duration) LimiterOptionFunc {
return func(l *Limiter) error {
l.duration = duration
return nil
}
}