ratelimit

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package ratelimit provides an in-memory token bucket rate limiter. Each key (IP or tenant) gets an independent bucket that refills at a configured rate. Stale buckets are cleaned up periodically.

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 is a thread-safe token bucket rate limiter keyed by string.

func New

func New(rate float64, burst int) *Limiter

New creates a Limiter that allows `rate` requests/second with a burst of `burst`. It starts a background goroutine to evict stale entries every minute.

func (*Limiter) Allow

func (l *Limiter) Allow(key string) bool

Allow reports whether a request for the given key is allowed. It consumes one token if available.

func (*Limiter) Burst

func (l *Limiter) Burst() int

Burst returns the max bucket capacity.

func (*Limiter) Remaining

func (l *Limiter) Remaining(key string) int

Remaining returns the current token count for a key (for headers).

func (*Limiter) Stop

func (l *Limiter) Stop()

Stop halts the background cleanup goroutine.

Jump to

Keyboard shortcuts

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