ratelimit

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package ratelimit provides token bucket rate limiting for agent requests. It implements a simple but effective rate limiting strategy that allows burst traffic while maintaining an average rate limit.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

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

Limiter implements a token bucket rate limiter. It is safe for concurrent use.

func NewLimiter

func NewLimiter(rate float64, burst int) *Limiter

NewLimiter creates a new rate limiter with the given rate (requests per second) and burst size. Rate of 0 or negative disables rate limiting entirely. Burst must be at least 1 if rate limiting is enabled.

func (*Limiter) Allow

func (l *Limiter) Allow() bool

Allow checks if a request can proceed immediately without waiting. It returns true if a token is available, false otherwise.

func (*Limiter) GetStats

func (l *Limiter) GetStats() Stats

GetStats returns current statistics about the rate limiter.

func (*Limiter) SetBurst

func (l *Limiter) SetBurst(burst int)

SetBurst updates the burst size. Burst must be at least 1.

func (*Limiter) SetRate

func (l *Limiter) SetRate(rate float64)

SetRate updates the rate limit. If rate is 0 or negative, rate limiting is disabled. This is useful for dynamic rate limit adjustments.

func (*Limiter) String

func (l *Limiter) String() string

String returns a human-readable representation of the rate limiter configuration.

func (*Limiter) Wait

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

Wait blocks until the rate limiter allows the request or the context is canceled. It returns an error if the context is canceled before the request can proceed.

type Stats

type Stats struct {
	Rate            float64
	Burst           int
	AvailableTokens float64
	Disabled        bool
}

Stats returns current rate limiter statistics.

Jump to

Keyboard shortcuts

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