middleware

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 10 Imported by: ? 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 middleware provides reusable model.Client middlewares such as adaptive rate limiting.

Index

Constants

This section is empty.

Variables

View Source
var ErrRequestTooLarge = errors.New("model middleware: request exceeds rate limiter capacity")

ErrRequestTooLarge is returned when a single request's token cost exceeds the limiter's maximum tokens-per-minute capacity. Such a request can never be admitted no matter how long it waits, so the limiter fails fast instead of blocking. Callers should raise the limiter's max TPM or reduce the request size.

Functions

This section is empty.

Types

type AdaptiveRateLimiter

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

AdaptiveRateLimiter applies an AIMD-style adaptive token bucket on top of a model.Client. NewAdaptiveRateLimiter charges estimated input tokens. NewOutputReservationAdaptiveRateLimiter charges exact input tokens plus the requested maximum output. Both adjust effective tokens-per-minute capacity from provider rate-limit signals.

The limiter is process-local and designed to sit at the provider client boundary. Callers construct a single instance per process and wrap the underlying model.Client with Middleware before passing it to planners or runtimes.

func NewAdaptiveRateLimiter

func NewAdaptiveRateLimiter(ctx context.Context, m *rmap.Map, key string, initialTPM, maxTPM float64) *AdaptiveRateLimiter

NewAdaptiveRateLimiter constructs an AdaptiveRateLimiter with a tokens-per-minute budget. When m and key are set, it coordinates capacity across processes using a Pulse replicated map; otherwise it operates as a process-local limiter.

func NewOutputReservationAdaptiveRateLimiter added in v2.1.0

func NewOutputReservationAdaptiveRateLimiter(
	ctx context.Context,
	m *rmap.Map,
	key string,
	initialTPM, maxTPM float64,
) *AdaptiveRateLimiter

NewOutputReservationAdaptiveRateLimiter constructs an AdaptiveRateLimiter that charges exact input tokens plus each request's positive MaxTokens value. Its versioned cluster key keeps combined-cost coordination separate from input-only limiters during rolling upgrades.

func (*AdaptiveRateLimiter) Middleware

func (l *AdaptiveRateLimiter) Middleware() func(model.Client) model.Client

Middleware returns a model.Client middleware that enforces the request-cost mode selected by the constructor for both Complete and Stream calls.

Jump to

Keyboard shortcuts

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