loadshed

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLoadShed = errors.New("request shed due to load")

ErrLoadShed indicates a request was rejected due to load shedding.

Functions

func DefaultThresholds

func DefaultThresholds() map[Priority]int

DefaultThresholds returns the default priority thresholds.

func IsLoadShed

func IsLoadShed(err error) bool

IsLoadShed checks whether err is a load shedding rejection.

func WithPriority

func WithPriority(ctx context.Context, p Priority) context.Context

WithPriority attaches a priority to a context.

Types

type Config

type Config struct {
	// Capacity is the total concurrent request capacity.
	Capacity int

	// PriorityThresholds maps each priority level to the max percentage
	// of capacity it can consume. Requests exceeding the threshold are shed.
	//
	// Example:
	//   {PriorityCritical: 100, PriorityHigh: 80, PriorityNormal: 60, PriorityLow: 30, PriorityBestEffort: 10}
	//
	// This means:
	//   - Critical: always allowed (100% of capacity)
	//   - High: allowed up to 80% utilization
	//   - Normal: allowed up to 60% utilization
	//   - Low: allowed up to 30% utilization
	//   - BestEffort: allowed up to 10% utilization
	//
	// If not set, DefaultThresholds are used.
	PriorityThresholds map[Priority]int

	// OnShed is called when a request is shed. Best-effort, must not block.
	OnShed func(priority Priority, utilization float64)
}

Config defines load shedding parameters.

type Priority

type Priority int

Priority defines the importance of a request. Lower numeric values = higher priority.

const (
	// PriorityCritical is the highest priority (e.g., health checks, auth).
	PriorityCritical Priority = iota

	// PriorityHigh is for important business operations.
	PriorityHigh

	// PriorityNormal is the default priority.
	PriorityNormal

	// PriorityLow is for background or batch operations.
	PriorityLow

	// PriorityBestEffort is the lowest priority, shed first.
	PriorityBestEffort
)

func PriorityFromContext

func PriorityFromContext(ctx context.Context) Priority

PriorityFromContext extracts the priority from a context. Returns PriorityNormal if not set.

func (Priority) String

func (p Priority) String() string

String returns the string representation.

type Shedder

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

Shedder implements priority-based load shedding.

When capacity utilization exceeds a priority's threshold, requests at that priority level (and below) are rejected. Higher-priority requests continue to be admitted.

Thread-safe via atomic operations.

func NewShedder

func NewShedder(cfg Config) *Shedder

NewShedder creates a new load shedder.

func (*Shedder) Admit

func (s *Shedder) Admit(priority Priority) error

Admit checks whether a request at the given priority should be admitted.

Returns nil if admitted (caller MUST call Done when finished). Returns ErrLoadShed if the request should be shed.

func (*Shedder) AdmitFromContext

func (s *Shedder) AdmitFromContext(ctx context.Context) error

AdmitFromContext extracts priority from context and checks admission.

func (*Shedder) Done

func (s *Shedder) Done()

Done releases one unit of capacity. Must be called after Admit returns nil.

func (*Shedder) Inflight

func (s *Shedder) Inflight() int

Inflight returns the current number of in-flight requests.

func (*Shedder) Utilization

func (s *Shedder) Utilization() float64

Utilization returns the current capacity utilization as a percentage.

Jump to

Keyboard shortcuts

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