-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimiter.go
125 lines (103 loc) · 2.8 KB
/
limiter.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
package limiter
import (
"strconv"
"time"
"github.com/gomodule/redigo/redis"
)
const redisPrefix = "rate:"
type Limit struct {
Rate int
Period time.Duration
Burst int
}
func PerSecond(rate int) *Limit {
return &Limit{
Rate: rate,
Period: time.Second,
Burst: rate,
}
}
func PerMinute(rate int) *Limit {
return &Limit{
Rate: rate,
Period: time.Minute,
Burst: rate,
}
}
func PerHour(rate int) *Limit {
return &Limit{
Rate: rate,
Period: time.Hour,
Burst: rate,
}
}
// ------------------------------------------------------------------------------
// Limiter controls how frequently events are allowed to happen.
type Limiter struct {
pool *redis.Pool
}
// NewLimiter returns a new Limiter.
func NewLimiter(pool *redis.Pool) *Limiter {
return &Limiter{
pool: pool,
}
}
// Allow is shorthand for AllowN(key, 1).
func (l *Limiter) Allow(key string, limit *Limit) (*Result, error) {
return l.AllowN(key, limit, 1)
}
// AllowN reports whether n events may happen at time now.
func (l *Limiter) AllowN(key string, limit *Limit, n int) (*Result, error) {
// Send script
kaa := []interface{}{redisPrefix + key, limit.Burst, limit.Rate, limit.Period.Seconds(), n}
conn := l.pool.Get()
defer conn.Close()
v, err := gcra.Do(l.pool.Get(), kaa...)
if err != nil {
return nil, err
}
values := v.([]interface{})
retryAfter, err := strconv.ParseFloat(string(values[2].([]byte)), 64)
if err != nil {
return nil, err
}
resetAfter, err := strconv.ParseFloat(string(values[3].([]byte)), 64)
if err != nil {
return nil, err
}
res := &Result{
Limit: limit,
Allowed: values[0].(int64) == 0,
Remaining: int(values[1].(int64)),
RetryAfter: dur(retryAfter),
ResetAfter: dur(resetAfter),
}
return res, nil
}
func dur(f float64) time.Duration {
if f == -1 {
return -1
}
return time.Duration(f * float64(time.Second))
}
type Result struct {
// Limit is the limit that was used to obtain this result.
Limit *Limit
// Allowed reports whether event may happen at time now.
Allowed bool
// Remaining is the maximum number of requests that could be
// permitted instantaneously for this key given the current
// state. For example, if a rate limiter allows 10 requests per
// second and has already received 6 requests for this key this
// second, Remaining would be 4.
Remaining int
// RetryAfter is the time until the next request will be permitted.
// It should be -1 unless the rate limit has been exceeded.
RetryAfter time.Duration
// ResetAfter is the time until the RateLimiter returns to its
// initial state for a given key. For example, if a rate limiter
// manages requests per second and received one request 200ms ago,
// Reset would return 800ms. You can also think of this as the time
// until Limit and Remaining will be equal.
ResetAfter time.Duration
}