-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratelimit.go
197 lines (173 loc) · 5.02 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
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
package ratelimit
// module dependencies
import "net"
import "time"
import "github.com/garyburd/redigo/redis"
// module constants
const MaxQuota = 5000
const LimitInterval = 3600 * time.Second
const MilliSecond = int64(time.Millisecond)
const MaxIdle = 40
const IdleTimeout = 240 * time.Second
// `RedisLimiter` Limits connections based
// on a max quota that they (`end_user_of_api`)
// are entitled to, and the quota that they have
// already consumed. Useful for Web Applications,
// to avoid abuse of APIs etc, by limiting everyone
// to a max quota of ex: 5000 requests per hour per
// `identifier`. The `identifier` has to be choosen
// by the `user` cosuming `RedisLimiter`
type RedisLimiter struct {
Pool *redis.Pool
PrefixQuota string
PrefixRemaining string
PrefixReset string
Duration time.Duration
Quota int
}
// `Limit` struct defines the model of `Limiter`.
// essential fields include:
//
// - `Quota`
// - `Used`
// - `Remaining`
// - `RetryAfter` epoch timestamp
type Limit struct {
Quota int
Used int
Remaining int
RetryAfter time.Time
}
// `Bucket` defines the model for
// `Quota`, `Used` & `Remaining`
type Bucket struct {
id string
value int
}
// `RetryAfter` defines for `self`
type RetryAfter struct {
id string
value int64
}
// Init returns a new RedisLimiter.
// Options:
// - `address` net.Addr
//
// @return *RedisLimiter, error
func Init(address net.Addr) (*RedisLimiter, error) {
rl := &RedisLimiter{
// http://godoc.org/github.com/garyburd/redigo/redis#Pool
Pool: &redis.Pool{
MaxIdle: MaxIdle,
IdleTimeout: IdleTimeout,
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
Dial: func() (redis.Conn, error) {
c, err := redis.Dial(address.Network(), address.String())
if err != nil {
return nil, err
}
return c, err
},
},
PrefixQuota: "RateLimit:Quota:",
PrefixRemaining: "RateLimit:Remaining:",
PrefixReset: "RateLimit:Reset:",
Duration: LimitInterval,
Quota: MaxQuota,
}
_, err := rl.ping()
return rl, err
}
// ping does an internal ping against
// `Redis` to check if it is alive.
func (s *RedisLimiter) ping() (bool, error) {
conn := s.Pool.Get()
defer conn.Close()
data, err := conn.Do("PING")
if err != nil || data == nil {
return false, err
}
return (data == "PONG"), nil
}
// Get `Limit` for an `identifier` from Redis
// and implicitly apply `RateLimit` calculations
// against this `identifier` back into Redis.
func (s *RedisLimiter) Get(id string) (*Limit, error) {
// Local variables
var (
err error
reply []interface{}
)
// Fetch Redis Pool
conn := s.Pool.Get()
defer conn.Close()
if err = conn.Err(); err != nil {
return nil, err
}
// create `Bucket` for holding `id` & `value`
quota := &Bucket{id: s.PrefixQuota + id}
remaining := &Bucket{id: s.PrefixRemaining + id}
reset := &RetryAfter{id: s.PrefixReset + id}
// calculate `expiry` based on: time.Now() + LimitInterval
expiry := (time.Now().UnixNano()/MilliSecond + int64(s.Duration)/MilliSecond) / 1000
// WATCH for changes at key:remaining
// also, defer to UNWATCH
conn.Send("WATCH", remaining)
defer conn.Send("UNWATCH", remaining)
// fetch quota, remaining & reset time
// to check if it already exists.
//
// if already exists, use these values
// and decrement. else, create them
if reply, err = redis.Values(conn.Do("MGET", quota.id, remaining.id, reset.id)); err != nil {
return nil, err
}
// get the above MGET values copied into the respective variables
if _, err = redis.Scan(reply, "a.value, &remaining.value, &reset.value); err != nil {
return nil, err
}
// as noted above, keys do not exist
// which means that we've to create
// them now, to use it further
if quota.value == 0 {
// @TODO: can we do this better?
// EX: expire in time.Seconds
// NX: create, only if it does not exist
conn.Send("MULTI")
conn.Send("SET", quota.id, s.Quota, "EX", s.Duration.Seconds(), "NX")
conn.Send("SET", remaining.id, s.Quota-1, "EX", s.Duration.Seconds(), "NX")
conn.Send("SET", reset.id, expiry, "EX", s.Duration.Seconds(), "NX")
if reply, err = redis.Values(conn.Do("EXEC")); err != nil {
return nil, err
} else if reply[0] != "OK" {
// @TODO: race condition
}
// copy the values, to use them in a uniform way
quota.value = s.Quota
remaining.value = s.Quota - 1
reset.value = expiry
} else if remaining.value > 0 {
// keys exist, and the remaining value
// is greater than 0. decrement it.
conn.Do("DECR", remaining.id)
remaining.value--
}
// we do not have to handle the case of
// remaining value <= 0, since the MGET
// command initialized it to 0 for us.
// finally, return the bucket
// holding the `Limit` values
return &Limit{
Quota: quota.value,
Used: quota.value - remaining.value,
Remaining: remaining.value,
RetryAfter: time.Unix(reset.value, 0),
}, nil
}
// Close closes the underlying *redis.Pool
func (s *RedisLimiter) Close() error {
return s.Pool.Close()
}