/
rate.go
253 lines (218 loc) · 5.13 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package rate
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/bluekaki/pkg/errors"
"github.com/go-redis/redis/v7"
"go.uber.org/zap"
"golang.org/x/time/rate"
)
var _ Limiter = (*limiter)(nil)
const (
prefix = "bluekaki-ratelimiter:"
defaultTickerInterval = time.Millisecond * 300
)
// Config how setup Limiter
type Config struct {
// Identifier a distinguish between different limiters
Identifier string
// TickerInterval duration of report/fetch increment to/from redis
TickerInterval time.Duration
// Limit the expected rate
Limit struct {
// Upper the expected maximum speed
Upper uint32
// Lower used when lose redis
Lower uint16
}
// Log setup logger, if enable and no logger set, zap.NewProduction() will used.
Log struct {
Enable bool
Logger *zap.Logger
}
}
// Redis a single or cluster redis instance
type Redis interface {
// Close the redis
Close() error
// IncrBy incr by value
IncrBy(key string, value int64) *redis.IntCmd
// Get by key
Get(key string) *redis.StringCmd
}
// Limiter a distributed speed limiter
type Limiter interface {
// Close the limiter
Close() error
// UpdateRate dynamic update rate, err will returned if parameters set zero
UpdateRate(limitUpper uint32, limitLower uint16) error
// Allow whether event can happen at time now
Allow()
}
type limiter struct {
ctx context.Context
cancel context.CancelFunc
logger *zap.Logger
mux *sync.Mutex
redis Redis
identifier string
limitUpper int
limitLower int
limiter *rate.Limiter
summary uint64
}
// NewLimiter create a new instance of Limiter
func NewLimiter(conf *Config, redis Redis) (Limiter, error) {
if conf == nil {
return nil, errors.New("conf required")
}
if conf.Identifier == "" {
return nil, errors.New("conf.Identifier required")
}
if conf.Limit.Upper == 0 {
return nil, errors.New("conf.Limit.Upper required")
}
if conf.Limit.Lower == 0 {
return nil, errors.New("conf.Limit.Lower required")
}
if redis == nil {
return nil, errors.New("redis required")
}
ctx, cancel := context.WithCancel(context.Background())
limiter := &limiter{
ctx: ctx,
cancel: cancel,
logger: conf.Log.Logger,
mux: new(sync.Mutex),
redis: redis,
identifier: prefix + conf.Identifier,
limitUpper: int(conf.Limit.Upper),
limitLower: int(conf.Limit.Lower),
limiter: rate.NewLimiter(rate.Limit(1), 1), // slow start
}
if conf.Log.Logger == nil && conf.Log.Enable {
// just disable log if err occurs
if logger, err := zap.NewProduction(); err == nil {
limiter.logger = logger
}
}
go func() {
time.Sleep(time.Second * 5) // avoid rate increase rapidly during multi instances startup
limiter.accelerate()
}()
tickerInterval := conf.TickerInterval
if tickerInterval == 0 {
tickerInterval = defaultTickerInterval
}
go limiter.report(tickerInterval)
go limiter.fetch(tickerInterval)
return limiter, nil
}
func (l *limiter) accelerate() {
l.mux.Lock()
defer l.mux.Unlock()
l.limiter.SetLimit(rate.Limit(l.limitUpper))
}
func (l *limiter) decelerate() {
l.mux.Lock()
defer l.mux.Unlock()
l.limiter.SetLimit(rate.Limit(l.limitLower))
}
func (l *limiter) Close() error {
select {
case <-l.ctx.Done():
default:
l.cancel()
if err := l.redis.Close(); err != nil {
return errors.Wrap(err, "close redis err")
}
}
return nil
}
func (l *limiter) UpdateRate(limitUpper uint32, limitLower uint16) error {
if limitUpper == 0 {
return errors.New("limitUpper required")
}
if limitLower == 0 {
return errors.New("limitLower required")
}
l.mux.Lock()
l.limitUpper = int(limitUpper)
l.limitLower = int(limitLower)
l.mux.Unlock()
l.accelerate()
return nil
}
func (l *limiter) Allow() {
if !l.limiter.Allow() {
time.Sleep(l.limiter.Reserve().Delay())
}
atomic.AddUint64(&l.summary, 1)
}
// report increment to redis
func (l *limiter) report(tickerInterval time.Duration) {
ticker := time.NewTicker(tickerInterval)
defer ticker.Stop()
var last uint64
do := func() {
now := atomic.LoadUint64(&l.summary)
if now == last {
return
}
if err := l.redis.IncrBy(l.identifier, int64(now-last)).Err(); err != nil && l.logger != nil {
l.logger.Error("redis.IncrBy err", zap.String("key", l.identifier), zap.Error(err))
}
last = now
}
for {
select {
case <-l.ctx.Done():
return
case <-ticker.C:
do()
}
}
}
// fetch increment from redis
func (l *limiter) fetch(tickerInterval time.Duration) {
ticker := time.NewTicker(tickerInterval)
defer ticker.Stop()
const (
initMode = iota
safeMode
normalMode
)
mode := initMode
var last uint64
do := func() {
now, err := l.redis.Get(l.identifier).Uint64()
if err != nil { // first time may err cause key not exist
if l.logger != nil {
l.logger.Error("redis.Get err", zap.String("key", l.identifier), zap.Error(err))
}
if mode != safeMode {
l.decelerate()
mode = safeMode
}
return
}
if now != last {
if mode != normalMode {
l.accelerate()
mode = normalMode
}
l.limiter.ReserveN(time.Now(), int(now-last))
last = now
}
}
for {
select {
case <-l.ctx.Done():
return
case <-ticker.C:
do()
}
}
}