Documentation
¶
Overview ¶
Package middleware provides standard app middlware implementations
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorRateMiddleware ¶ added in v1.2.12
func ErrorRateMiddleware(tracker *ErrorRateTracker) func(http.Handler) http.Handler
ErrorRateMiddleware returns HTTP middleware that records the outgoing response status on each request into the given ErrorRateTracker. The tracker can then be read by a readiness / liveness health check to decide whether this instance is still serving requests successfully.
If the wrapped handler does not call WriteHeader, the response defaults to 200 OK (matching Go's net/http behavior) and is recorded as a non-failure.
If the wrapped handler panics, the panic is first recorded as a 500 in the tracker (so the health check sees it) and then re-raised for the outer panic-recovery middleware to translate into an HTTP 500 response. Without this, a panicking endpoint would never register as failing on the health check.
Types ¶
type ErrorRateTracker ¶ added in v1.2.12
type ErrorRateTracker struct {
// contains filtered or unexported fields
}
ErrorRateTracker maintains a bounded, time-windowed view of HTTP response statuses so a health check can ask "is the rate of 5xx responses on this endpoint exceeding some threshold?" — without hair-trigger flapping on a single transient error.
The tracker classifies any status code in [500, 600) as a failure. All other codes count as a success.
Two views are exposed:
- Healthy: instantaneous rate, ideal for readiness. As soon as failures age out of the window or are drowned in successes, it returns true. This lets a pod that's recovered rejoin the Service quickly.
- HealthyWithinCooldown: sticky-latch view, ideal for liveness. Once an evaluation returns unhealthy, the tracker remembers the time, and HealthyWithinCooldown keeps returning false until the cooldown has elapsed since the last unhealthy evaluation. This prevents a readiness-eviction → no-traffic → window-ages-out → readmit flap loop and ensures a persistently-bad pod actually gets restarted.
Pruning of old events happens lazily on each Record and Healthy call. No background goroutine.
func NewErrorRateTracker ¶ added in v1.2.12
func NewErrorRateTracker(window time.Duration) *ErrorRateTracker
NewErrorRateTracker creates a tracker that retains response events for the given window. Events older than the window are pruned on the next call.
func NewErrorRateTrackerWithClock ¶ added in v1.2.12
func NewErrorRateTrackerWithClock(window time.Duration, now func() time.Time) *ErrorRateTracker
NewErrorRateTrackerWithClock is a test seam for injecting a deterministic clock.
func (*ErrorRateTracker) Healthy ¶ added in v1.2.12
func (t *ErrorRateTracker) Healthy(rateThreshold float64, minFailures int) bool
Healthy reports whether the tracker's recent window is within acceptable bounds: the failure rate must be at or below rateThreshold, or the absolute number of failures must be below minFailures. The minFailures floor prevents a single transient error (or a small burst on a quiet endpoint) from marking the endpoint unhealthy.
This is the instantaneous view; use it for readiness probes so a pod that has recovered rejoins the Service as soon as its recent responses are healthy again.
func (*ErrorRateTracker) HealthyWithinCooldown ¶ added in v1.2.12
func (t *ErrorRateTracker) HealthyWithinCooldown(rateThreshold float64, minFailures int, cooldown time.Duration) bool
HealthyWithinCooldown is the sticky-latch view on top of Healthy. It returns false if either:
- the instantaneous evaluation is unhealthy, or
- some earlier evaluation tripped the threshold within the last cooldown.
Use this for liveness probes so a pod that has been demonstrated unhealthy cannot look healthy again just because traffic stopped arriving (which would cause the sliding window to empty and Healthy to flip back to true). The latch guarantees that if a pod is persistently bad, liveness eventually fires and the pod is restarted instead of flapping Ready / NotReady.
func (*ErrorRateTracker) Record ¶ added in v1.2.12
func (t *ErrorRateTracker) Record(statusCode int)
Record adds an observation of one HTTP response to the window.