ratelimit

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0, MIT Imports: 6 Imported by: 0

Documentation

Overview

Package ratelimit provides a Redis-backed sliding-window rate limiter with a local in-memory fallback when Redis is unavailable (Lite / single-instance).

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 enforces per-key sliding-window limits. max is evaluated per Allow call so callers (e.g. embed channels) can vary budgets without rebuilding the limiter.

func New

func New(redisClient *redis.Client, keyPrefix string, window time.Duration, instanceID string) *Limiter

New constructs a limiter. keyPrefix should include a trailing delimiter (e.g. "embed:ratelimit:"). When redis is nil, only the local fallback runs.

func (*Limiter) Allow

func (l *Limiter) Allow(ctx context.Context, key string, max int) bool

Allow reports whether key is within budget for the current window.

func (*Limiter) StartCleanup

func (l *Limiter) StartCleanup(stopCh <-chan struct{})

StartCleanup runs periodic eviction for the local fallback map. No-op when only Redis is in use, but cheap to call either way.

Jump to

Keyboard shortcuts

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