requestbudget

package
v0.5.6 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package requestbudget bounds expensive request work across an akari process.

Index

Constants

View Source
const (
	// DefaultCapacity is sixteen 8 MiB-equivalent work units. It permits two
	// concurrent Argon2 operations or one maximum-sized MCP spool with room for
	// lighter database work.
	DefaultCapacity int64 = 16
	// DefaultWaitTimeout lets normal bursts drain through the queue while bounding
	// how long an overloaded request occupies a connection.
	DefaultWaitTimeout = 5 * time.Second
)
View Source
const MinCapacity = int64(12)

MinCapacity is the smallest useful capacity because the heaviest class must be able to run once. Smaller values would leave MCP POSTs queued until timeout.

Variables

View Source
var (
	// ErrWaitTimeout means the budget could not admit work within its bounded wait.
	ErrWaitTimeout = errors.New("request budget wait timeout")
	// ErrInvalidClass means a caller supplied a class the budget cannot safely use.
	ErrInvalidClass = errors.New("invalid request budget work class")
)

Functions

This section is empty.

Types

type Budget

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

Budget is a fair, process-wide weighted admission queue. Its semaphore is FIFO, so lighter work cannot repeatedly jump ahead of an older expensive request.

func New

func New(capacity int64, wait time.Duration) (*Budget, error)

New constructs a weighted budget. Capacity must be large enough to admit the heaviest built-in work class at least once.

func (*Budget) Acquire

func (b *Budget) Acquire(ctx context.Context, class WorkClass) (func(), error)

Acquire waits for class capacity or returns ErrWaitTimeout. A cancellation from the caller is returned unchanged. The returned release function is idempotent so cleanup remains safe when error paths converge.

func (*Budget) ServeHTTP

func (b *Budget) ServeHTTP(w http.ResponseWriter, _ *http.Request)

ServeHTTP exposes the queue in Prometheus text format. The metrics contain no request identity or route parameters and are safe for a normal metrics scraper.

type WorkClass

type WorkClass uint8

WorkClass is one measured class of expensive request work. The closed constants keep names and weights coupled so a caller cannot admit work under a misleading metric label or an accidentally smaller weight.

const (
	// MCPSpool reserves approximately the 100 MiB request ceiling owned by #134.
	// Applying it to MCP POSTs now also bounds the SDK's current request buffering.
	MCPSpool WorkClass = iota + 1
	PublicAnalytics
	OAuthRegistration
)

Password work is deliberately absent: request-triggered Argon2 hashing is bounded by its own fixed worker pool (see httpapi's passwordWork), and a second gate here would let budget pressure produce a login response that differs from the uniform invalid-credentials path.

Jump to

Keyboard shortcuts

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