Documentation ¶
Index ¶
- Constants
- type Identity
- type IdentityStringer
- type Limiter
- func (q *Limiter) Check(from Identity) (limited bool)
- func (q *Limiter) CheckStringer(from fmt.Stringer) bool
- func (q *Limiter) DebugChannel() chan string
- func (q *Limiter) Peek(from Identity) bool
- func (q *Limiter) PeekStringer(from fmt.Stringer) bool
- func (q *Limiter) ResetItem(from Identity)
- func (q *Limiter) SetDebug(on bool)
- type Policy
Constants ¶
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 )
const ( DebugDisabled uint32 = iota DebugEnabled )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Identity ¶
type Identity interface {
UniqueKey() string
}
Identity is an interface that allows any arbitrary type to be used for a unique key in ratelimit checks when implemented.
type IdentityStringer ¶ added in v1.2.0
type IdentityStringer struct {
// contains filtered or unexported fields
}
IdentityStringer is an implentation of Identity that acts as a shim for types that implement fmt.Stringer.
func (IdentityStringer) UniqueKey ¶ added in v1.2.0
func (i IdentityStringer) UniqueKey() string
type Limiter ¶
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 *sync.RWMutex // contains filtered or unexported fields }
Limiter implements an Enforcer to create an arbitrary ratelimiter.
func NewCustomLimiter ¶
NewCustomLimiter returns a ratelimiter with the given Policy applied as the Ruleset.
func NewDefaultLimiter ¶
func NewDefaultLimiter() *Limiter
NewDefaultLimiter returns a ratelimiter with default settings without Strict mode.
- Default window: 25 seconds
- Default burst: 25 requests
func NewDefaultStrictLimiter ¶
func NewDefaultStrictLimiter() *Limiter
NewDefaultStrictLimiter returns a ratelimiter with default settings with Strict mode.
- Default window: 25 seconds
- Default burst: 25 requests
func NewHardcoreLimiter ¶ added in v1.1.0
NewHardcoreLimiter returns a custom limiter with Strict + Hardcore modes enabled.
Hardcore mode causes the time limited to be multiplied by the number of hits. This differs from strict mode which is only using addition instead of multiplication.
func NewLimiter ¶
NewLimiter returns a custom limiter witout Strict mode.
- Window is the time in seconds that the limiter will cache requests.
- Burst is the number of requests that can be made in the window.
func NewStrictLimiter ¶
NewStrictLimiter returns a custom limiter with Strict mode enabled.
- Window is the time in seconds that the limiter will cache requests.
- Burst is the number of requests that can be made in the window.
func (*Limiter) Check ¶
Check checks and increments an Identities UniqueKey() output against a list of cached strings to determine and raise it's ratelimitting status.
func (*Limiter) CheckStringer ¶ added in v1.2.0
func (*Limiter) DebugChannel ¶
DebugChannel enables debug mode and returns a channel where debug messages are sent.
NOTE: If you do not read from this channel, the debug messages will eventually be lost. If this happens,
func (*Limiter) Peek ¶
Peek checks an Identities UniqueKey() output against a list of cached strings to determine ratelimitting status without adding to its request count.
func (*Limiter) PeekStringer ¶ added in v1.2.0
type Policy ¶
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 }
Policy defines the mechanics of our ratelimiter.