Documentation
¶
Overview ¶
Package ratelimit is the request limiter of design/rate-limiting.md: a token bucket per key, held in memory on one backd instance. It makes no I/O and takes no HTTP types, so the server decides what a key is (a class, a principal, a client address) and what a refusal looks like.
A bucket holds up to Limit tokens and refills Limit tokens per Window; a request takes one. A client may burst to the whole allowance and then runs at the refill rate. A bucket that is full is the same as no bucket, so idle ones are dropped, and the number of buckets is capped: when the table is full a key it has not seen shares one overflow bucket, so a flood of new keys degrades to a shared limit instead of growing memory.
Index ¶
Constants ¶
const (
// DefaultMaxBuckets is how many buckets one Limiter keeps.
DefaultMaxBuckets = 100_000
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Decision ¶
type Decision struct {
Allowed bool
// Limit is the size of the bucket.
Limit int
// Remaining is how many more requests would pass right now.
Remaining int
// RetryAfter is how long until one request passes; zero when Allowed.
RetryAfter time.Duration
// Reset is how long until the bucket is full again.
Reset time.Duration
}
Decision is what Allow decided, with what a client needs to slow down before it is refused.
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter holds the buckets of one instance. It is safe for concurrent use.
func New ¶
New returns a Limiter that keeps at most maxBuckets buckets (DefaultMaxBuckets when it is below 1). now is the clock; time.Now when nil.
func (*Limiter) Allow ¶
Allow takes one token from key's bucket under rate and says whether the request passes. A rate that limits nothing always passes and stores nothing.
func (*Limiter) Overflowed ¶
Overflowed is how many requests, since the start, used an overflow bucket because the table was full.