forked from rudderlabs/rudder-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
50 lines (43 loc) · 1.32 KB
/
options.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
package throttling
import "github.com/go-redis/redis/v8"
// Option is a functional option for the limiter, see With* functions for reference
type Option func(*Limiter)
// WithInMemoryGoRate allows to setup a limiter with golang.org/x/time/rate (supports returning tokens)
func WithInMemoryGoRate() Option {
return func(l *Limiter) {
l.useGoRate = true
}
}
// WithInMemoryGCRA allows to use the GCRA algorithm (in-memory) with the specified burst or with the
// burst as rate if the provided burst is <= 0
func WithInMemoryGCRA(burst int64) Option {
return func(l *Limiter) {
l.useGCRA = true
if burst > 0 {
l.gcraBurst = burst
}
}
}
// WithRedisGCRA allows to use the GCRA algorithm (Redis version) with the specified burst or with the
// burst as rate if the provided burst is <= 0
func WithRedisGCRA(rc *redis.Client, burst int64) Option {
return func(l *Limiter) {
l.useGCRA = true
l.redisSpeaker = rc
if burst > 0 {
l.gcraBurst = burst
}
}
}
// WithRedisSortedSet allows to use the Redis SortedSet algorithm for rate limiting
func WithRedisSortedSet(rc *redis.Client) Option {
return func(l *Limiter) {
l.redisSpeaker = rc
}
}
// WithStatsCollector allows to setup a stats collector for the limiter
func WithStatsCollector(sc statsCollector) Option {
return func(l *Limiter) {
l.statsCollector = sc
}
}