forked from go-redis/redis_rate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrate.go
155 lines (126 loc) · 4.14 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
package redis_rate
import (
"context"
"fmt"
"time"
"github.com/redis/go-redis/v9"
"golang.org/x/time/rate"
)
const redisPrefix = "rate"
type rediser interface {
Del(ctx context.Context, keys ...string) *redis.IntCmd
Pipelined(ctx context.Context, fn func(redis.Pipeliner) error) ([]redis.Cmder, error)
}
// Limiter controls how frequently events are allowed to happen.
type Limiter struct {
redis rediser
// Optional fallback limiter used when Redis is unavailable.
Fallback *rate.Limiter
}
func NewLimiter(redis rediser) *Limiter {
return &Limiter{
redis: redis,
}
}
// Reset resets the rate limit for the name in the given rate limit period.
func (l *Limiter) Reset(ctx context.Context, name string, period time.Duration) error {
secs := int64(period / time.Second)
slot := time.Now().Unix() / secs
name = allowName(name, slot)
return l.redis.Del(ctx, name).Err()
}
// ResetRate resets the rate limit for the name and limit.
func (l *Limiter) ResetRate(ctx context.Context, name string, rateLimit rate.Limit) error {
if rateLimit == 0 {
return nil
}
if rateLimit == rate.Inf {
return nil
}
_, period := limitPeriod(rateLimit)
slot := time.Now().UnixNano() / period.Nanoseconds()
name = allowRateName(name, period, slot)
return l.redis.Del(ctx, name).Err()
}
// AllowN reports whether an event with given name may happen at time now.
// It allows up to maxn events within period, with each interaction
// incrementing the limit by n.
func (l *Limiter) AllowN(
ctx context.Context, name string, maxn int64, period time.Duration, n int64,
) (count int64, delay time.Duration, allow bool) {
secs := int64(period / time.Second)
utime := time.Now().Unix()
slot := utime / secs
delay = time.Duration((slot+1)*secs-utime) * time.Second
if l.Fallback != nil {
allow = l.Fallback.Allow()
}
name = allowName(name, slot)
count, err := l.incr(ctx, name, period, n)
if err == nil {
allow = count <= maxn
}
return count, delay, allow
}
// Allow is shorthand for AllowN(name, max, period, 1).
func (l *Limiter) Allow(ctx context.Context, name string, maxn int64, period time.Duration) (count int64, delay time.Duration, allow bool) {
return l.AllowN(ctx, name, maxn, period, 1)
}
// AllowMinute is shorthand for Allow(name, maxn, time.Minute).
func (l *Limiter) AllowMinute(ctx context.Context, name string, maxn int64) (count int64, delay time.Duration, allow bool) {
return l.Allow(ctx, name, maxn, time.Minute)
}
// AllowHour is shorthand for Allow(name, maxn, time.Hour).
func (l *Limiter) AllowHour(ctx context.Context, name string, maxn int64) (count int64, delay time.Duration, allow bool) {
return l.Allow(ctx, name, maxn, time.Hour)
}
// AllowRate reports whether an event may happen at time now.
// It allows up to rateLimit events each second.
func (l *Limiter) AllowRate(ctx context.Context, name string, rateLimit rate.Limit) (delay time.Duration, allow bool) {
if rateLimit == 0 {
return 0, false
}
if rateLimit == rate.Inf {
return 0, true
}
limit, period := limitPeriod(rateLimit)
now := time.Now()
slot := now.UnixNano() / period.Nanoseconds()
name = allowRateName(name, period, slot)
count, err := l.incr(ctx, name, period, 1)
if err == nil {
allow = count <= limit
} else if l.Fallback != nil {
allow = l.Fallback.Allow()
}
if !allow {
delay = time.Duration(slot+1)*period - time.Duration(now.UnixNano())
}
return delay, allow
}
func limitPeriod(rl rate.Limit) (limit int64, period time.Duration) {
period = time.Second
if rl < 1 {
limit = 1
period *= time.Duration(1 / rl)
} else {
limit = int64(rl)
}
return limit, period
}
func (l *Limiter) incr(ctx context.Context, name string, period time.Duration, n int64) (int64, error) {
var incr *redis.IntCmd
_, err := l.redis.Pipelined(ctx, func(pipe redis.Pipeliner) error {
incr = pipe.IncrBy(ctx, name, n)
pipe.Expire(ctx, name, period+30*time.Second)
return nil
})
rate, _ := incr.Result()
return rate, err
}
func allowName(name string, slot int64) string {
return fmt.Sprintf("%s:%s-%d", redisPrefix, name, slot)
}
func allowRateName(name string, period time.Duration, slot int64) string {
return fmt.Sprintf("%s:%s-%d-%d", redisPrefix, name, period, slot)
}