throttle

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 10 Imported by: 0

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 Limit

type Limit struct {
	Count  int
	Window time.Duration
}

Limit defines a single rate limit: at most Count requests per Window.

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

func New(cachePath string, limits ...Limit) (*Throttle, error)

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

func (t *Throttle) Close() error

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.

Jump to

Keyboard shortcuts

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