ratelimit

package
v1.0.2 Latest Latest
Warning

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

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

Documentation

Overview

Package ratelimit provides token bucket rate limiting with worker pools for managing concurrent API requests with configurable burst and period settings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	BurstSize      int           // Maximum concurrent requests
	Period         time.Duration // Time between allowing new requests
	QueueSize      int           // Buffer size for pending requests
	MaxRetries     int           // Maximum retry attempts
	RetryBaseDelay time.Duration // Initial retry delay
	RetryMaxDelay  time.Duration // Maximum retry delay
}

Config holds configuration for the rate limiter.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults for rate limiting.

type Limiter

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

Limiter implements rate limiting using a token bucket algorithm.

func New

func New(cfg Config) *Limiter

New creates a new rate limiter with the given configuration.

func (*Limiter) Enqueue

func (l *Limiter) Enqueue(req *Request)

Enqueue adds a request to the queue for rate-limited execution.

func (*Limiter) Start

func (l *Limiter) Start(ctx context.Context, workerCount int)

Start begins processing the queue with the specified number of workers.

func (*Limiter) Stop

func (l *Limiter) Stop()

Stop gracefully stops the rate limiter.

type Request

type Request struct {
	ID      string
	Execute func(context.Context) error
	Result  chan error
	Ctx     context.Context
}

Request represents a queued request waiting for rate limit permission.

Jump to

Keyboard shortcuts

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