Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrLoadShed = errors.New("request shed due to load")
ErrLoadShed indicates a request was rejected due to load shedding.
Functions ¶
func DefaultThresholds ¶
DefaultThresholds returns the default priority thresholds.
func IsLoadShed ¶
IsLoadShed checks whether err is a load shedding rejection.
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 ¶
PriorityFromContext extracts the priority from a context. Returns PriorityNormal if not set.
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 (*Shedder) Admit ¶
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 ¶
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) Utilization ¶
Utilization returns the current capacity utilization as a percentage.