Documentation
¶
Overview ¶
Package throttle provides an HTTP client middleware that enforces sliding-window rate limits. One or more Limit values define how many requests are allowed per time window; all windows must pass before a request is forwarded.
State can optionally be persisted to disk so that limits survive process restarts. Call Throttle.Close when the throttle is no longer needed to flush state to the configured cache path.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Throttle ¶
type Throttle struct {
// contains filtered or unexported fields
}
Throttle enforces one or more sliding-window rate limits on outgoing HTTP requests.
func New ¶
New creates a Throttle that enforces the given limits. At least one limit must be provided and each limit's Count and Window must be positive. Duplicate (Count, Window) pairs are not allowed.
If cachePath is non-empty, previously persisted state is loaded from that file so that rate-limit counters survive process restarts.
func (*Throttle) Close ¶
Close persists the current sliding-window state to disk when a cachePath was provided to New. It returns nil when no cachePath was configured.
func (*Throttle) Middleware ¶
func (t *Throttle) Middleware() httpkit.Middleware
Middleware returns a httpkit.Middleware that blocks until all configured rate-limit windows allow the request to proceed. If the request's context is canceled while waiting, the error is returned immediately and the request is not forwarded.