Help us shape the future of Go! Take the 2026 Go Developer Survey

throttle

package
v1.78.2 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package throttle slows an endpoint that answers anyone and costs something to answer: a recovery proof, which spends memory on a digest or a call to the cluster's API server.

It counts refusals per SOURCE and, beside that, across all sources. The per-source count is what protects the endpoint from one noisy caller; the global ceiling is what protects the process from many, and it is set well above what one source may do. A single global counter alone -- the first version of this -- is a lock somebody else holds: ten wrong proofs from anywhere shut the door on the one operator who has the right one, which for a break-glass path is the attack.

Index

Constants

View Source
const (
	DefaultPerSource = 5
	DefaultGlobal    = 100
	DefaultWindow    = time.Minute
	// DefaultSources is how many sources are told apart. Beyond it, new
	// sources share one count, so a caller who can vary its address cannot
	// grow the process.
	DefaultSources = 4096
)

Defaults for a recovery endpoint: five refusals a source may spend in a window, and a ceiling across every source at twenty times that.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter struct {
	// contains filtered or unexported fields
}

Limiter is a fixed-window count of refusals. The zero value is not ready: use New.

func Default

func Default() *Limiter

Default returns a Limiter with every default.

func New

func New(perSource, global int, window time.Duration, sources int, now func() time.Time) *Limiter

New returns a Limiter allowing perSource refusals from one source, and global across all, per window, telling apart at most sources sources. A non-positive value takes the default. now is the clock; nil is time.Now.

func (*Limiter) Accepted

func (l *Limiter) Accepted(source string)

Accepted clears source's refusals: the right proof from a source that fumbled one is not a source to go on slowing. The global count is not cleared; it measures work, not people.

func (*Limiter) Blocked

func (l *Limiter) Blocked(source string) bool

Blocked reports whether source may not try: it has spent its refusals for the window, or every source together have.

func (*Limiter) Len

func (l *Limiter) Len() int

Len is how many sources are held; for tests.

func (*Limiter) Refused

func (l *Limiter) Refused(source string)

Refused records that source's attempt was refused.

func (*Limiter) RefusedBySource added in v1.77.1

func (l *Limiter) RefusedBySource(source string)

RefusedBySource records a refusal against source alone, and not against the ceiling across every source: for an attempt that cost nothing to refuse, which must not be a way to use up everybody's share.

Jump to

Keyboard shortcuts

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