-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathratelimit.go
97 lines (76 loc) · 1.79 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
package ratelimit
import (
"errors"
"strconv"
"github.com/garyburd/redigo/redis"
)
// ErrBlocked is returned when an attribute is rate limited
var ErrBlocked = errors.New("rate limit: blocked")
// RateLimiter interface for rate limiting key
type RateLimiter interface {
Run(key string) error
RateLimitExceeded(key string) bool
Reset(key string) error
}
// RateLimit type for ratelimiting
type RateLimit struct {
redisPool *redis.Pool
config *RateLimitConfig
}
// NewRateLimit func to create a new rate limiting type
func NewRateLimit(redisPool *redis.Pool, config *RateLimitConfig) *RateLimit {
return &RateLimit{
redisPool: redisPool,
config: config,
}
}
// Run initiates ratelimiting for the key given
func (rl *RateLimit) Run(key string) error {
conn := rl.redisPool.Get()
defer conn.Close()
conn.Send("MULTI")
conn.Send("INCR", key)
conn.Send("EXPIRE", key, rl.config.WindowInSeconds)
_, err := conn.Do("EXEC")
if err != nil {
return err
}
value, err := redis.String(conn.Do("GET", key))
if err != nil {
return err
}
result, err := strconv.Atoi(value)
if err != nil {
return err
}
if result > rl.config.Attempts {
_, err := conn.Do("EXPIRE", key, rl.config.CooldownInSeconds)
if err != nil {
return err
}
}
return nil
}
// RateLimitExceeded returns state of a RateLimit for a key given
func (rl *RateLimit) RateLimitExceeded(key string) bool {
conn := rl.redisPool.Get()
defer conn.Close()
value, err := redis.Int(conn.Do("GET", key))
if err != nil {
return false
}
if value > rl.config.Attempts {
return true
}
return false
}
// Reset func clears the key from rate limiting
func (rl *RateLimit) Reset(key string) error {
conn := rl.redisPool.Get()
defer conn.Close()
_, err := conn.Do("DEL", key)
if err != nil {
return err
}
return nil
}