limit

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 5 Imported by: 0

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