Documentation
¶
Overview ¶
Package ratelimit implements a token bucket rate limiter. It does NOT use any timers or channels in its implementation. The core idea is that every call to ask for a Token also "drip fills" the bucket with fractional tokens. To evenly drip-fill the bucket, we do all our calculations in nanoseconds.
To ratelimit incoming connections on a per source basis, a convenient helper constructor is available for "PerIPRateLimiter".
Usage:
// Ratelimit to 1000 every 5 seconds rl = ratelimit.New(1000, 5) .... if rl.Limit() { dropConnection(conn) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Clock ¶
type Clock interface { // Return current time in seconds Now() time.Time // Sleep for a given time Sleep(time.Duration) }
Clock provides an interface to timekeeping. It is used in test harness.
type PerIPRateLimiter ¶
type PerIPRateLimiter struct {
// contains filtered or unexported fields
}
Manages a map of source IP:port to underlying ratelimiter Each entry is in a LRU Cache. The Per-IP Limiter is bounded to a maximum size when it is constructed.
func NewPerIP ¶
func NewPerIP(ratex, perx uint, max int) (*PerIPRateLimiter, error)
Create a new per-source rate limiter to limit each IP (host) to 'ratex' every 'perx' seconds. Hold a maximum of 'max' IP addresses in the rate-limiter
func (*PerIPRateLimiter) Limit ¶
func (p *PerIPRateLimiter) Limit(a net.Addr) bool
Return true if the source 'a' needs to be rate limited, false otherwise.
func (*PerIPRateLimiter) MaybeTake ¶
func (p *PerIPRateLimiter) MaybeTake(a net.Addr, n uint) bool
MaybeTake attempts to take 'n' tokens for the host 'a' Returns true if it can take all of them, false otherwise.
func (*PerIPRateLimiter) Reset ¶
func (p *PerIPRateLimiter) Reset(a net.Addr)
Reset ratelimiter state for this host
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter represents a token-bucket rate limiter
func New ¶
func New(rate, per uint) (*RateLimiter, error)
Create new limiter that limits to 'rate' every 'per' seconds
func NewBurst ¶
func NewBurst(rate, per, burst uint) (*RateLimiter, error)
Create new limiter that limits to 'rate' every 'per' seconds with burst of 'b' tokens in the same time period. The notion of burst is only meaningful when it is larger than its normal rate. Thus, bursts smaller than the actual rate are ignored.
func NewBurstWithClock ¶
func NewBurstWithClock(rate, per, burst uint, clk Clock) (*RateLimiter, error)
Create new limiter with a custom time keeper that limits to 'rate' every 'per' seconds with burst of 'b' tokens in the same time period. The notion of burst is only meaningful when it is larger than its normal rate. Thus, bursts smaller than the actual rate are ignored.
func NewWithClock ¶
func NewWithClock(rate, per uint, clk Clock) (*RateLimiter, error)
Make a new rate limiter using a custom timekeeper
func (*RateLimiter) Limit ¶
func (r *RateLimiter) Limit() bool
Return true if the current call exceeds the set rate, false otherwise
func (*RateLimiter) MaybeTake ¶
func (r *RateLimiter) MaybeTake(vn uint) (ok bool)
MaybeTake attempts to take 'vn' tokens from the rate limiter. Returns true if it can take all of them, false otherwise.
func (RateLimiter) String ¶
func (r RateLimiter) String() string
Stringer implementation for RateLimiter
func (*RateLimiter) Wait ¶
func (r *RateLimiter) Wait(n uint) bool
Wait until we have at least 'n' tokens available