ratelimit

package
v0.9.14 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 5 Imported by: 0 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 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

View Source
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

func New(maxBuckets int, now func() time.Time) *Limiter

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

func (l *Limiter) Allow(key string, rate Rate) Decision

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

func (l *Limiter) Overflowed() int64

Overflowed is how many requests, since the start, used an overflow bucket because the table was full.

func (*Limiter) Size

func (l *Limiter) Size() int

Size is how many buckets are held now.

func (*Limiter) Sweep

func (l *Limiter) Sweep()

Sweep drops every idle bucket now. Allow does it a shard at a time, once a minute, as keys come in; this is for a caller that wants the table small without waiting for traffic.

type Rate

type Rate struct {
	Limit  int
	Window time.Duration
}

Rate is an allowance: Limit requests per Window (the size of the bucket and how fast it refills). The zero value, or a Limit below 1, is no limit.

func (Rate) Unlimited

func (r Rate) Unlimited() bool

Unlimited reports whether r limits nothing.

Jump to

Keyboard shortcuts

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