Documentation
¶
Overview ¶
Package ratelimit throttles repeated failures, such as failed logins.
The default behavior is a growing back-off rather than a hard lockout: after Threshold failures within Window, the key is blocked for BaseDelay, and each further failure doubles the delay up to MaxDelay. A hard lockout lets anyone lock any user out, so it is left to LockoutHooks.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Threshold int // failures before blocking; default 5
Window time.Duration // failure counting window; default 15m
BaseDelay time.Duration // first block; default 1s
MaxDelay time.Duration // cap on the block; default 15m
MaxKeys int // memory bound; default 100k (oldest windows evicted first)
// Now returns the current time. Nil means time.Now.
Now func() time.Time
}
Config tunes the in-memory limiter. Zero values take the defaults shown.
type Limiter ¶
type Limiter interface {
// Check reports whether key may make an attempt, without counting one.
Check(ctx context.Context, key string) (Result, error)
// Fail records a failure and returns the key's new state.
Fail(ctx context.Context, key string) (Result, error)
// Reset clears a key, typically after a successful attempt.
Reset(ctx context.Context, key string) error
}
Limiter tracks failures per key. Implementations must be safe for concurrent use. Keys are opaque strings chosen by the caller.
type LockoutHooks ¶
type LockoutHooks interface {
// OnFailure is called after every recorded failure.
OnFailure(ctx context.Context, key string, r Result)
// OnBlocked is called when a failure causes key to become blocked.
OnBlocked(ctx context.Context, key string, r Result)
}
LockoutHooks lets the application react to failures, for example to send a notification or apply a hard lockout in its own user store.
type Memory ¶
type Memory struct {
// contains filtered or unexported fields
}
Memory is an in-process Limiter. In a multi-instance deployment each instance counts separately; use a shared store (see the redisstore module).