ratelimit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package ratelimit provides token bucket rate limiting.

Index

Constants

This section is empty.

Variables

View Source
var ErrRateLimited = errors.New("rate limited")

ErrRateLimited is returned when the rate limit is exceeded.

Functions

This section is empty.

Types

type Config

type Config struct {
	Rate  int // tokens per second
	Burst int // max burst size
}

Config configures a rate limiter.

type Limiter

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

Limiter is a token bucket rate limiter.

func New

func New(cfg Config) *Limiter

New creates a new rate limiter.

func PerMinute

func PerMinute(rpm int) *Limiter

PerMinute creates a rate limiter with the given requests per minute.

func PerSecond

func PerSecond(rps int) *Limiter

PerSecond creates a rate limiter with the given requests per second.

func (*Limiter) Allow

func (l *Limiter) Allow() bool

Allow checks if a single request should be allowed.

func (*Limiter) Tokens

func (l *Limiter) Tokens() float64

Tokens returns the current number of available tokens.

func (*Limiter) TryAcquire

func (l *Limiter) TryAcquire(n int) bool

TryAcquire attempts to acquire n tokens.

func (*Limiter) Wait

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

Wait blocks until a token is available or the context is cancelled.

type Manager

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

Manager manages multiple named rate limiters.

func NewManager

func NewManager() *Manager

NewManager creates a new rate limiter manager.

func (*Manager) Get

func (m *Manager) Get(name string, cfg Config) *Limiter

Get returns a limiter by name, creating it if needed.

Jump to

Keyboard shortcuts

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