tokenbucket

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package tokenbucket implements a token-bucket rate limiter for QPS control. Tokens are refilled at a fixed rate (rate per second) up to a maximum burst size.

Two variants are provided:

  • New(rate, burst) — non-blocking, returns ErrLimitExceeded
  • NewBlocking(rate, burst) — blocks until a token is available or ctx is done

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(rate, burst int) limiter.Limiter

New creates a non-blocking token-bucket rate limiter.

  • rate: sustained tokens per second (QPS)
  • burst: maximum burst size (initial token count)

Acquire returns limiter.ErrLimitExceeded immediately when no token is available.

func NewAtomic

func NewAtomic(rate, burst int) limiter.Limiter

NewAtomic creates a high-throughput non-blocking token-bucket using atomic operations. It is designed for scenarios with very high QPS where lock contention on the mutex variant becomes a bottleneck.

func NewBlocking

func NewBlocking(rate, burst int) limiter.Limiter

NewBlocking creates a blocking token-bucket rate limiter. Acquire blocks until a token is available or ctx is cancelled.

Types

This section is empty.

Jump to

Keyboard shortcuts

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