limit

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 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 limit paces how often the server is allowed to drive a browser at X.

Fetching too eagerly is what gets a session flagged, so every uncached path goes through a budget. Reads and writes each get their own.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Budget

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

Budget enforces a floor between calls and a ceiling within a rolling window.

func New

func New(p Params) *Budget

New builds a budget from p.

func (*Budget) Remaining

func (b *Budget) Remaining() int

Remaining reports how many calls are still available in the current window.

func (*Budget) Wait

func (b *Budget) Wait(ctx context.Context) error

Wait blocks until the caller may proceed, then records the call.

It returns *ExhaustedError when the window is full — that is a real answer for the caller to surface, not something to wait out, since the wait could be most of the window.

The lock is never held while sleeping: a slow caller would otherwise stall every other request behind it for the whole interval.

type ExhaustedError

type ExhaustedError struct {
	RetryAfter time.Duration
}

ExhaustedError reports that the rolling budget is spent.

func (*ExhaustedError) Error

func (e *ExhaustedError) Error() string

type Params added in v0.0.4

type Params struct {
	MinInterval time.Duration
	Window      time.Duration
	Max         int

	// Jitter spreads the gap between calls over MinInterval..MinInterval+Jitter.
	//
	// A fixed interval is itself a signature: nothing human acts at exactly the
	// same spacing every time. It matters for actions X can see as engagement,
	// and not at all for reads.
	Jitter time.Duration
}

Params configures a Budget.

Jump to

Keyboard shortcuts

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