-
Notifications
You must be signed in to change notification settings - Fork 5
/
limiter.go
44 lines (37 loc) · 1.04 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
package rerate
import "time"
// Limiter a redis-based ratelimiter
type Limiter struct {
Counter
max int64
}
// NewLimiter create a new redis-based ratelimiter
// the Limiter limits the rate to max times per period
func NewLimiter(newBuckets BucketsFactory, pfx string, period, interval time.Duration, max int64) *Limiter {
return &Limiter{
Counter: *NewCounter(newBuckets, pfx, period, interval),
max: max,
}
}
func (l *Limiter) remainingAt(id string, t time.Time) (int64, error) {
occurs, err := l.countAt(id, t)
if err != nil {
return 0, err
}
return l.max - occurs, nil
}
// Remaining return the number of requests left for the time window
func (l *Limiter) Remaining(id string) (int64, error) {
return l.remainingAt(id, time.Now())
}
func (l *Limiter) exceededAt(id string, t time.Time) (bool, error) {
rem, err := l.Remaining(id)
if err != nil {
return false, err
}
return rem <= 0, nil
}
// Exceeded is exceeded the rate limit or not
func (l *Limiter) Exceeded(id string) (bool, error) {
return l.exceededAt(id, time.Now())
}