Documentation
¶
Overview ¶
Package shedder provides pod-level load shedding for Kubernetes services.
The library tracks how many HTTP requests are currently in flight for a given pod. When the number exceeds a configurable HardLimit, the readiness endpoint returns 503, signaling Kubernetes to remove the pod from load balancing until load drops.
Basic Usage ¶
// Create a shedder with a hard limit of 100 concurrent requests
s := shedder.New(shedder.Config{
HardLimit: 100,
})
// Use the middleware with your HTTP server
http.Handle("/api/", s.Middleware(apiHandler))
// Add the readiness endpoint
http.Handle("/ready", s.ReadyHandler())
Soft Limit with Callback ¶
An optional SoftLimit enables selective shedding of low-priority requests before reaching the HardLimit:
s := shedder.New(shedder.Config{
HardLimit: 100,
SoftLimit: 80,
ShedDecider: func(r *http.Request) bool {
// Shed requests with low priority header
return r.Header.Get("X-Priority") == "low"
},
})
Soft Limit with Header Matching ¶
Alternatively, use HeaderMatcher for simple header-based shedding:
s := shedder.New(shedder.Config{
HardLimit: 100,
SoftLimit: 80,
ShedHeader: &shedder.HeaderMatcher{
Name: "X-Priority",
Value: "low",
},
})
Integration with Kubernetes ¶
Important: Use SEPARATE endpoints for liveness and readiness probes. The readiness probe should use ReadyHandler (returns 503 when overloaded). The liveness probe should use HealthHandler (always returns 200). Using the readiness endpoint for liveness would cause Kubernetes to restart healthy-but-busy pods.
The handlers are path-agnostic - register them at any path you prefer:
// Liveness - always 200 if process is running
http.Handle("/healthz", shedder.HealthHandler())
// Readiness - 503 when overloaded
http.Handle("/readyz", s.ReadyHandler())
Kubernetes deployment configuration (paths must match your registration):
livenessProbe:
httpGet:
path: /healthz
port: 8080
readinessProbe:
httpGet:
path: /readyz
port: 8080
periodSeconds: 5
failureThreshold: 1
Index ¶
- func HealthHandler() http.Handler
- type Config
- type HeaderMatcher
- type ShedDecider
- type ShedReason
- type Shedder
- func (s *Shedder) Inflight() int64
- func (s *Shedder) IsOverloaded() bool
- func (s *Shedder) IsSoftOverloaded() bool
- func (s *Shedder) Middleware(next http.Handler) http.Handler
- func (s *Shedder) MiddlewareFunc() func(http.Handler) http.Handler
- func (s *Shedder) ReadyHandler() http.Handler
- func (s *Shedder) ReadyHandlerFunc() http.HandlerFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HealthHandler ¶
HealthHandler returns a simple health check handler that always returns 200 OK. This is suitable for Kubernetes liveness probes.
Types ¶
type Config ¶
type Config struct {
// HardLimit is the maximum number of in-flight requests before the
// readiness endpoint returns 503. This is required and must be > 0.
HardLimit int64
// SoftLimit is the threshold for soft overload behavior.
// If SoftLimit > 0 and inflight > SoftLimit (but <= HardLimit),
// the ShedDecider is consulted to determine if requests should be fast-failed.
// If SoftLimit is 0 or negative, soft overload behavior is disabled.
SoftLimit int64
// ShedDecider is called when in soft overload state to determine
// whether to shed a request. If nil and SoftLimit > 0, soft shedding
// is effectively disabled unless ShedHeader is set.
ShedDecider ShedDecider
// ShedHeader specifies a header name and value for automatic shedding.
// When in soft overload state, requests with this header matching will be shed.
// This is an alternative to ShedDecider for simple priority-based shedding.
// If both ShedDecider and ShedHeader are set, ShedDecider takes precedence.
ShedHeader *HeaderMatcher
// OnShed is an optional callback invoked when a request is shed.
// Useful for logging or metrics (without adding direct dependencies).
OnShed func(r *http.Request, reason ShedReason)
}
Config holds the configuration for a Shedder instance.
type HeaderMatcher ¶
type HeaderMatcher struct {
Name string // Header name, e.g., "X-Priority"
Value string // Header value to match, e.g., "low"
}
HeaderMatcher defines a header name and value to match for shedding.
type ShedDecider ¶
ShedDecider is a callback function that determines whether a request should be shed when in soft overload state. It receives the incoming request and returns true if the request should be rejected.
type ShedReason ¶
type ShedReason int
ShedReason indicates why a request was shed.
const ( // ShedReasonHardLimit indicates the request was shed because // in-flight requests exceeded HardLimit. ShedReasonHardLimit ShedReason = iota // ShedReasonSoftLimit indicates the request was shed because // in-flight requests exceeded SoftLimit and the ShedDecider // (or header match) determined it should be shed. ShedReasonSoftLimit )
func (ShedReason) String ¶
func (r ShedReason) String() string
type Shedder ¶
type Shedder struct {
// contains filtered or unexported fields
}
Shedder tracks in-flight requests and provides load shedding capabilities.
func NewWithLimits ¶
NewWithLimits creates a new Shedder with just hard and soft limits. This is a convenience function for simple use cases without callbacks.
func (*Shedder) IsOverloaded ¶
IsOverloaded returns true if in-flight requests exceed HardLimit.
func (*Shedder) IsSoftOverloaded ¶
IsSoftOverloaded returns true if soft limit is configured and in-flight requests exceed SoftLimit (but not HardLimit).
func (*Shedder) Middleware ¶
Middleware returns an http.Handler that wraps the given handler with load shedding logic.
The middleware:
- Increments the in-flight counter
- Checks if HardLimit is exceeded - if so, returns 503 immediately
- If SoftLimit is exceeded and ShedDecider returns true, returns 503
- Otherwise, calls the wrapped handler
- Decrements the in-flight counter when done (even on panic)
func (*Shedder) MiddlewareFunc ¶
MiddlewareFunc is a convenience wrapper that returns a function suitable for use with middleware chains that expect func(http.Handler) http.Handler.
func (*Shedder) ReadyHandler ¶
ReadyHandler returns an http.Handler that implements a Kubernetes readiness probe endpoint.
Returns:
- 200 OK when in-flight requests <= HardLimit
- 503 Service Unavailable when in-flight requests > HardLimit
func (*Shedder) ReadyHandlerFunc ¶
func (s *Shedder) ReadyHandlerFunc() http.HandlerFunc
ReadyHandlerFunc is a convenience function that returns the readiness handler as an http.HandlerFunc.