ratelimit

package
v0.14.1 Latest Latest
Warning

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

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

Documentation

Overview

Package ratelimit provides a shared rate limiter for broker API clients.

It wraps golang.org/x/time/rate to provide context-aware, per-broker request throttling. Multiple callers sharing the same Limiter are serialized correctly without exceeding the configured rate.

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 throttles API requests using a token-bucket algorithm.

func New

func New(name string, rps float64, burst int) *Limiter

New creates a Limiter that allows rps requests per second with the given burst size. Burst controls how many requests can fire at once before throttling kicks in.

func Shared added in v0.14.0

func Shared(name string, rps float64, burst int, key string) *Limiter

Shared returns the Limiter registered for (name, key), creating it on first access. Subsequent calls with the same (name, key) return the same *Limiter regardless of the rps/burst arguments — the values from the first caller win. Use when multiple clients must cooperatively stay under a shared upstream quota.

func (*Limiter) Allow

func (l *Limiter) Allow() bool

Allow reports whether a request can proceed right now without waiting.

func (*Limiter) Name

func (l *Limiter) Name() string

Name returns the limiter's name (for logging/debugging).

func (*Limiter) Wait

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

Wait blocks until the limiter allows one request, or ctx is cancelled.

Jump to

Keyboard shortcuts

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