throttle

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package throttle wraps an http.RoundTripper with a client-side token-bucket rate limiter backed by golang.org/x/time/rate.

RoundTrip blocks until the limiter admits the request or the request's context is cancelled, whichever comes first.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidRate = errors.New("throttle: rps must be > 0 and burst must be >= 1")

ErrInvalidRate is returned when NewTransport is called with a non-positive rps or a burst < 1. Keeping this as an error (instead of panicking or silently hanging callers) lets the kit validate at construction time.

Functions

func NewTransport

func NewTransport(base http.RoundTripper, rps float64, opts ...Option) (http.RoundTripper, error)

NewTransport returns an http.RoundTripper that admits at most rps requests per second on average, with the configured burst allowance. When base is nil, http.DefaultTransport is used.

Returns ErrInvalidRate when rps <= 0 or burst < 1 so callers see the misconfiguration at construction rather than a hanging request later.

The returned RoundTripper honours req.Context(): when the context is cancelled before the limiter admits the request, RoundTrip returns the context error and does not call the underlying transport.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures a Transport.

func WithBurst

func WithBurst(n int) Option

WithBurst sets the burst capacity of the token bucket. Default: 1 (requests are strictly paced at 1/rps seconds apart). Higher burst values allow short bursts of activity followed by the sustained rps rate. Values < 1 are not silently clamped; they surface as ErrInvalidRate from NewTransport.

Jump to

Keyboard shortcuts

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