ratelimit

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RateMin            = 5.0   // Floor: never go below this
	RateMax            = 500.0 // Ceiling: never exceed this
	RateBackoffFactor  = 0.7   // Reduce to 70% on 429 (like AWS SDK beta)
	RateRecoveryFactor = 1.1   // +10% on sustained success
	RateRecoveryAfter  = 10    // Successful requests before recovery
)

Rate limiting constants. These are exported for testing purposes.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdaptiveRateLimiter

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

AdaptiveRateLimiter implements adaptive rate limiting for HTTP requests. It starts disabled (or with an initial rate) and adapts based on throttle responses.

func NewAdaptiveRateLimiter

func NewAdaptiveRateLimiter(initialRate float64, name string) *AdaptiveRateLimiter

NewAdaptiveRateLimiter creates a new adaptive rate limiter. If initialRate is 0, the limiter starts disabled and enables on first throttle. If initialRate > 0, the limiter starts enabled at that rate.

func (*AdaptiveRateLimiter) CurrentRate

func (a *AdaptiveRateLimiter) CurrentRate() float64

CurrentRate returns the current rate limit (requests per second). Returns 0 if disabled.

func (*AdaptiveRateLimiter) IsEnabled

func (a *AdaptiveRateLimiter) IsEnabled() bool

IsEnabled returns whether the rate limiter is currently active.

func (*AdaptiveRateLimiter) RecordSuccess

func (a *AdaptiveRateLimiter) RecordSuccess()

RecordSuccess tracks a successful request. After enough consecutive successes, the rate limit is gradually increased.

func (*AdaptiveRateLimiter) RecordThrottle

func (a *AdaptiveRateLimiter) RecordThrottle()

RecordThrottle enables the limiter (if not already) and reduces the rate. This should be called when a 429 or throttling error is received.

func (*AdaptiveRateLimiter) Wait

Wait blocks until a request is allowed or the context is canceled. If the limiter is disabled, it returns immediately.

Jump to

Keyboard shortcuts

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