1
- package rate
1
+ package rate // import "gopkg.in/go-redis/rate.v4"
2
2
3
3
import (
4
4
"fmt"
@@ -15,15 +15,14 @@ type rediser interface {
15
15
Pipelined (func (pipe * redis.Pipeline ) error ) ([]redis.Cmder , error )
16
16
}
17
17
18
- //Limiter type
18
+ // Limiter controls how frequently events are allowed to happen.
19
+ // It uses redis to store data and fallbacks to the fallbackLimiter
20
+ // when Redis Server is not available.
19
21
type Limiter struct {
20
22
fallbackLimiter * timerate.Limiter
21
23
redis rediser
22
24
}
23
25
24
- // NewLimiter creates a limiter that controls how frequently events
25
- // are allowed to happen. It uses redis to store data and fallbacks
26
- // to the fallbackLimiter when Redis Server is not available.
27
26
func NewLimiter (redis rediser , fallbackLimiter * timerate.Limiter ) * Limiter {
28
27
return & Limiter {
29
28
fallbackLimiter : fallbackLimiter ,
@@ -32,8 +31,8 @@ func NewLimiter(redis rediser, fallbackLimiter *timerate.Limiter) *Limiter {
32
31
}
33
32
34
33
// AllowN reports whether an event with given name may happen at time now.
35
- // It allows up to maxn events within duration dur, with each interaction incrementing
36
- // the limit by n.
34
+ // It allows up to maxn events within duration dur, with each interaction
35
+ // incrementing the limit by n.
37
36
func (l * Limiter ) AllowN (name string , maxn int64 , dur time.Duration , n int64 ) (count , reset int64 , allow bool ) {
38
37
udur := int64 (dur / time .Second )
39
38
slot := time .Now ().Unix () / udur
@@ -49,8 +48,7 @@ func (l *Limiter) AllowN(name string, maxn int64, dur time.Duration, n int64) (c
49
48
return count , reset , allow
50
49
}
51
50
52
- // Allow reports whether an event with given name may happen at time now.
53
- // It allows up to maxn events within duration dur.
51
+ // Allow is shorthand for AllowN(name, max, dur, 1).
54
52
func (l * Limiter ) Allow (name string , maxn int64 , dur time.Duration ) (count , reset int64 , allow bool ) {
55
53
return l .AllowN (name , maxn , dur , 1 )
56
54
}
0 commit comments