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

limiter

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package limiter owns the designated protected VirtualKey admission SPI. The designated surfaces are Dimension and its LLM/MCP/Agent constants, Policy, Clock, LimiterRequest, LimiterDecision, LimiterBackend, LimiterProvider, Config, ModuleID, MemoryBackendType, Factory, NewMemoryBackend and the MemoryBackend Admit/Retire/Health/Close methods, ErrLimiterUnavailable and ErrLimiterRetired. Their exported fields and method signatures are protected beginning with v0.7.0 after published-tag consumer acceptance. Selection and its accessors are Community assembly implementation surfaces.

Policies and globally unique generations are committed before admission. One call atomically consumes one token or denies without consumption; errors can be ambiguous and are never retried by the gateway. Cancellation and bounded deadlines are mandatory. Retire permanently fences generations, even when a late operation arrives; Admit returns ErrLimiterRetired (possibly wrapped), which never triggers fail-open. Generation-scoped quota storage survives replacement of prepared backend connection handles on unrelated extension mutations. A switch of backend authority/config resets generations; no token transfer is promised. Prepare initializes handles, Health diagnoses, Close stops and drains once under the extension manager's common five-second lifecycle budget.

Index

Constants

View Source
const MemoryBackendType = "memory"
View Source
const ModuleID = "online.agentguide.community.limiter"

Variables

View Source
var ErrLimiterRetired = errors.New("limiter_retired")
View Source
var ErrLimiterUnavailable = errors.New("limiter_unavailable")

Functions

func Factory

func Factory(runtime bool) *extension.Factory

Factory provides strict syntax/dependency validation and prepares the selected linked backend. runtime=false is the schema-only CLI projection.

Types

type Clock

type Clock interface{ Now() time.Time }

Clock supports deterministic backend contract fixtures.

type Config

type Config struct {
	BackendModuleID string `json:"backend_module_id"`
	BackendType     string `json:"backend_type"`
	TimeoutMS       int    `json:"timeout_ms"`
	FailureMode     string `json:"failure_mode"`
}

Config is the Community-owned selection envelope (schema 1). No policy bypass is implicit: failure_mode defaults to closed.

type Dimension

type Dimension string

Dimension is one ingress traffic class.

const (
	DimensionLLM   Dimension = "llm"
	DimensionMCP   Dimension = "mcp"
	DimensionAgent Dimension = "agent"
)

type LimiterBackend

type LimiterBackend interface {
	Admit(context.Context, LimiterRequest) (LimiterDecision, error)
	Retire(context.Context, string) error
	Health(context.Context) error
	Close(context.Context) error
}

LimiterBackend is the candidate protected domain SPI. All methods honor context; diagnostics are replaced by bounded gateway codes. Retire fences a generation permanently, including late operations. Admit on a retired generation must return ErrLimiterRetired (possibly wrapped); it never fails open. Generation-scoped storage survives replacement of a module's prepared connection/resource handles. Close stops admissions, cancels/drains work, and runs once per owned handle. Backend factories initialize connections before extension publication.

type LimiterDecision

type LimiterDecision struct {
	Allowed    bool
	RetryAfter time.Duration
}

LimiterDecision reports one atomic consume-or-deny operation. A failure may have consumed a token; the caller must never retry it automatically.

type LimiterProvider

type LimiterProvider interface {
	LimiterBackend(string) (LimiterBackend, error)
}

LimiterProvider is implemented by a linked extension's prepared resource. Selection resolves this interface only during preparation, never per request.

type LimiterRequest

type LimiterRequest struct {
	Generation   string
	VirtualKeyID string
	Dimension    Dimension
	Limit        Policy
}

LimiterRequest is an immutable policy snapshot. Generation is an opaque, globally unique policy identity, never a bearer key. An unchanged policy keeps its generation across extension refreshes. Backends must preserve its state.

type MemoryBackend

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

MemoryBackend is the Community process-local reference backend. Its lifetime is one gateway; it is not a distributed quota authority.

func NewMemoryBackend

func NewMemoryBackend(clock Clock) *MemoryBackend

func (*MemoryBackend) Admit

func (*MemoryBackend) Close

func (b *MemoryBackend) Close(ctx context.Context) error

func (*MemoryBackend) Health

func (b *MemoryBackend) Health(ctx context.Context) error

func (*MemoryBackend) Retire

func (b *MemoryBackend) Retire(ctx context.Context, g string) error

type Policy

type Policy struct {
	RequestsPerMinute int
	Burst             int
}

Policy contains validated positive request-token limits.

type Selection

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

func DefaultSelection

func DefaultSelection() *Selection

func (*Selection) Backend

func (s *Selection) Backend() LimiterBackend

func (*Selection) Budget

func (s *Selection) Budget() time.Duration

func (*Selection) Close

func (s *Selection) Close(context.Context) error

func (*Selection) Configuration

func (s *Selection) Configuration() Config

Selection is Community runtime wiring, not an extension implementation API. It provides an immutable prepared backend selection to the VirtualKey owner.

func (*Selection) Health

func (s *Selection) Health(ctx context.Context) error

func (*Selection) Identity

func (s *Selection) Identity() string

func (*Selection) StateResetComparedTo

func (s *Selection) StateResetComparedTo(ctx context.Context, previous extension.Resource) (bool, error)

func (*Selection) String

func (s *Selection) String() string

Directories

Path Synopsis
Package contracttest provides reusable protected limiter conformance suites.
Package contracttest provides reusable protected limiter conformance suites.

Jump to

Keyboard shortcuts

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