-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
66 lines (55 loc) · 1.95 KB
/
models.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
package rate5
import (
"fmt"
"sync"
"sync/atomic"
"github.com/patrickmn/go-cache"
)
const (
// DefaultWindow is the standard window of time ratelimit triggers are observed in seconds.
DefaultWindow = 25
// DefaultBurst is the standard amount of triggers observed within Window before ratelimiting occurs.
DefaultBurst = 25
)
// Identity is an interface that allows any arbitrary type to be used for a unique key in ratelimit checks when implemented.
type Identity interface {
UniqueKey() string
}
// IdentityStringer is an implentation of Identity that acts as a shim for types that implement fmt.Stringer.
type IdentityStringer struct {
stringer fmt.Stringer
}
func (i IdentityStringer) UniqueKey() string {
return i.stringer.String()
}
const (
DebugDisabled uint32 = iota
DebugEnabled
)
// Limiter implements an Enforcer to create an arbitrary ratelimiter.
type Limiter struct {
// Patrons gives access to the underlying cache type that powers the ratelimiter.
// It is exposed for testing purposes.
Patrons *cache.Cache
// Ruleset determines the Policy which is used to determine whether or not to ratelimit.
// It consists of a Window and Burst, see Policy for more details.
Ruleset Policy
debug uint32
debugChannel chan string
debugLost int64
known map[interface{}]*atomic.Int64
debugMutex *sync.RWMutex
*sync.RWMutex
}
// Policy defines the mechanics of our ratelimiter.
type Policy struct {
// Window defines the duration in seconds that we should keep track of ratelimit triggers,
Window int64
// Burst is the amount of times that Check will not trigger a limit within the duration defined by Window.
Burst int64
// Strict mode punishes triggers of the ratelimitter by increasing the wait time upon every trigger of the limiter.
Strict bool
// Hardcore mode implies strict mode but instead of using addition when adding to the wait time, it uses multiplication.
// This will cause exponential ratelimiting.
Hardcore bool
}