Documentation ¶
Overview ¶
Package ratelimit wraps the Limiter from golang.org/x/time/rate and creates a simple interface for global and per-host limits.
Usage:
// Ratelimit globally to 1000 req/s, per-host to 5 req/s and cache // latest 30000 per-host limits rl = ratelimit.New(1000, 5, 30000) .... if !rl.Allow() { dropConnection(conn) } if !rl.AllowHost(conn.RemoteAddr()) { dropConnection(conn) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limiter ¶ added in v0.9.0
type Limiter struct {
// contains filtered or unexported fields
}
Limiter controls how frequently events are allowed to happen globally or per-host. It uses a token-bucket limiter for the global limit and instantiates a token-bucket limiter for every unique host. The number of per-host limiters is limited to an upper bound ("cache size").
A negative rate limit means "no limit" and a zero rate limit means "Infinite".
func New ¶
Create a new token bucket rate limiter that limits globally at 'g' requests/sec and per-host at 'p' requests/sec; It remembers the rate of the 'cachesize' most recent hosts (and their limits). The burst rates are pre-configured to be: Global burst limit: 3 * b; Per host burst limit: 2 * p
func (*Limiter) Allow ¶ added in v0.9.0
Allow returns true if the global rate limit can consume 1 token and false otherwise. Use this if you intend to drop/skip events that exceed a configured global rate limit, otherwise, use Wait().
func (*Limiter) AllowHost ¶ added in v0.9.0
AllowHost returns true if the per-host rate limit for host 'a' can consume 1 token and false otherwise. Use this if you intend to drop/skip events that exceed a configured global rate limit, otherwise, use WaitHost().