middleware

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 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 HTTP admission control for Go services. A shared Service applies one total limit and critical reservation across its handlers. It does not start servers, read environment files, or install global handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdaptiveConfig

type AdaptiveConfig struct {
	MinConcurrent     int
	InitialConcurrent int
	Interval          time.Duration
	MinSamples        int
	TargetLatency     time.Duration
}

AdaptiveConfig enables request-driven adjustment when attached to ServiceConfig. Zero fields select conservative defaults. MaxConcurrent remains the ceiling.

type HandlerOption

type HandlerOption func(*handlerOptions)

func WithLatencyTarget

func WithLatencyTarget(target time.Duration) HandlerOption

WithLatencyTarget gives this handler its own budget so naturally slower work is not compared directly to fast handlers sharing the same service capacity.

type OutcomeStatistics

type OutcomeStatistics struct {
	Outcome         string
	StatusClasses   [7]uint64 // 1xx, 2xx, 3xx, 4xx, 5xx, other, none
	Count           uint64
	DurationSum     float64
	DurationBuckets [11]uint64 // cumulative, seconds
}

type Priority

type Priority string
const (
	Critical   Priority = "critical"
	Background Priority = "background"
)

type PriorityStatistics

type PriorityStatistics struct {
	Priority                                           Priority
	Admitted, RejectedConcurrency, RejectedReservation uint64
	Outcomes                                           [3]OutcomeStatistics // completed, canceled, panicked
}

type Service

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

Service must be constructed with NewService; its zero value is not usable. It is safe for concurrent use. Reuse one instance for handlers sharing capacity; construct separate instances for independent service budgets.

func NewService

func NewService(c ServiceConfig) (*Service, error)

func (*Service) Middleware

func (s *Service) Middleware(priority Priority, next http.Handler, options ...HandlerOption) http.Handler

Middleware wraps next with a route-configured priority. Invalid priorities or nil handlers panic at registration time. Caller headers cannot change priority. The slot is held until next returns (or panics); work outliving the handler is outside that lifetime. Cancellation is forwarded through the request context. There is no waiting queue, preemption, automatic retry, or process-wide budget.

func (*Service) Snapshot

func (s *Service) Snapshot() ServiceState

func (*Service) Statistics

func (s *Service) Statistics() Statistics

Statistics returns coherent histogram buckets/counts/sums and a live capacity snapshot. Scraping this method does not count as service traffic.

type ServiceConfig

type ServiceConfig struct {
	// Name identifies this service in rejection responses and logs; default "service".
	Name string
	// MaxConcurrent is required and must be positive.
	MaxConcurrent int
	// ReservedCritical ranges from zero through MaxConcurrent, inclusive.
	ReservedCritical int
	// Logger optionally records rejections. Nil disables library logging.
	Logger *slog.Logger
	// Adaptive nil retains fixed mode.
	Adaptive *AdaptiveConfig
}

type ServiceState

type ServiceState struct {
	Mode                                                                      string
	Limit, Active, Background, ReservedCritical, MinConcurrent, MaxConcurrent int
}

ServiceState is a read-only snapshot. Active may temporarily exceed Limit after a reduction while previously admitted requests drain; they are not interrupted.

type Statistics

type Statistics struct {
	Name           string
	State          ServiceState
	Priorities     [2]PriorityStatistics
	DurationBounds [11]float64
	Adjustments    [2][3]uint64 // decrease/increase; latency_pressure/server_errors/healthy_saturation
}

Statistics is a bounded snapshot independent of any metrics backend. Histograms contain admitted work only. Rejected requests do not pollute handler latency.

Jump to

Keyboard shortcuts

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