Documentation
¶
Overview ¶
Package ratelimit limits requests per key with token buckets and tells rejected clients when to retry.
It uses only the standard library, so the oauth protocol packages can depend on it.
Every host and Go Mode handler that answers 429 sets Retry-After with SetRetryAfter, or with WriteHeaders for a Limiter result, so clients see one rule: whole seconds, rounded up, at least one.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RetryAfterSeconds ¶
RetryAfterSeconds is wait in whole seconds, rounded up so a retry lands after it, and at least 1.
func SetRetryAfter ¶
SetRetryAfter sets the Retry-After header of a 429 or 503 response to wait.
func WriteHeaders ¶
WriteHeaders describes result to the client: X-RateLimit-Limit (requests per window), X-RateLimit-Remaining, and X-RateLimit-Reset (Unix seconds when the bucket is full again), plus Retry-After on a rejection. Call it before the handler writes, on every limited response. An unlimited result writes nothing.
Types ¶
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter admits requests per key at a sustained rate with a burst. The zero value is not usable; call New.
func New ¶
New returns a limiter allowing requests per window per key, up to burst at once. A requests or window of 0 or less means unlimited.
func (*Limiter) Allow ¶
Allow consumes one request for key when its bucket has a token. When the bucket table is full, a new key waits until the next prune can free a bucket.
type Result ¶
type Result struct {
Allowed bool
// Limit is the configured requests per window; 0 means unlimited.
Limit int
// Remaining is how many requests the key can make now.
Remaining int
// ResetAt is when the key's bucket is full again.
ResetAt time.Time
// RetryAfter is how long a rejected key waits until its next request is allowed; 0 when allowed.
RetryAfter time.Duration
}
Result is the outcome of one Allow call.