ratelimit

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPDoer

type HTTPDoer interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPDoer is an interface for making HTTP requests. Both *http.Client and *RateLimitedHTTPClient satisfy this interface.

type Limiter

type Limiter struct {
	// contains filtered or unexported fields
}

Limiter implements a token bucket rate limiter. Thread-safe for concurrent use.

func NewLimiter

func NewLimiter(maxTokens, refillRate float64) *Limiter

NewLimiter creates a rate limiter with specified capacity and refill rate.

Example: NewLimiter(100, 10.0) creates a limiter with: - 100 token capacity - 10 tokens per second refill rate - Allows bursts up to 100 requests - Steady state: 10 requests/sec

func (*Limiter) TryAcquire

func (l *Limiter) TryAcquire() bool

TryAcquire attempts to acquire a token without blocking. Returns true if token acquired, false if none available.

func (*Limiter) Wait

func (l *Limiter) Wait(ctx context.Context) error

Wait blocks until a token is available, respecting context cancellation. Returns context.Canceled or context.DeadlineExceeded if context ends.

type RateLimitedHTTPClient

type RateLimitedHTTPClient struct {
	// contains filtered or unexported fields
}

RateLimitedHTTPClient wraps an HTTPDoer with token bucket rate limiting.

func NewRateLimitedHTTPClient

func NewRateLimitedHTTPClient(inner HTTPDoer, limiter *Limiter) *RateLimitedHTTPClient

NewRateLimitedHTTPClient wraps an existing HTTPDoer with rate limiting. If limiter is nil, requests pass through without rate limiting.

func (*RateLimitedHTTPClient) Do

Do executes an HTTP request, blocking until a rate limit token is available.

Jump to

Keyboard shortcuts

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