ratelimit

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package ratelimit provides an in-memory token-bucket implementation of aauth.Limiter for single-instance deployments. State is per process: a deployment with several instances needs a shared limiter instead.

Index

Constants

View Source
const DefaultMaxKeys = 100_000

DefaultMaxKeys bounds the number of keys a TokenBucket tracks. Keys are created by callers, so the table must be bounded.

Variables

This section is empty.

Functions

This section is empty.

Types

type TokenBucket

type TokenBucket struct {
	// Rate is the refill rate in actions per second; Burst the bucket
	// size. Both must be positive.
	Rate  float64
	Burst int
	// MaxKeys bounds tracked keys (default DefaultMaxKeys).
	MaxKeys int
	// Now returns the current time; nil means time.Now.
	Now func() time.Time
	// contains filtered or unexported fields
}

TokenBucket is an aauth.Limiter that allows Burst actions per key at once and refills at Rate actions per second. Idle keys whose bucket has refilled are dropped; when MaxKeys is reached, refilled keys are pruned and, if none can be, the request is refused (fail closed).

func New

func New(perSecond float64, burst int) *TokenBucket

New returns a TokenBucket allowing burst actions per key, refilled at perSecond actions per second.

func Per

func Per(n int, interval time.Duration) *TokenBucket

Per returns a TokenBucket allowing n actions per interval per key, all of which may be used at once.

func (*TokenBucket) Allow

func (b *TokenBucket) Allow(_ context.Context, key string) (bool, time.Duration)

Allow implements aauth.Limiter.

Jump to

Keyboard shortcuts

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