ratelimit

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: AGPL-3.0 Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func RetryAfterSeconds(wait time.Duration) int

RetryAfterSeconds is wait in whole seconds, rounded up so a retry lands after it, and at least 1.

func SetRetryAfter

func SetRetryAfter(h http.Header, wait time.Duration)

SetRetryAfter sets the Retry-After header of a 429 or 503 response to wait.

func WriteHeaders

func WriteHeaders(h http.Header, result Result)

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

func New(requests int, window time.Duration, burst int) *Limiter

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

func (l *Limiter) Allow(key string) Result

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.

func (*Limiter) Count

func (l *Limiter) Count() int64

Count reports how many requests Allow has checked.

func (*Limiter) Update

func (l *Limiter) Update(requests int, window time.Duration, burst int)

Update changes the limit for every key. Existing buckets keep their tokens, capped at the new burst.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL