ratelimit

package
v0.63.0 Latest Latest
Warning

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

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

Documentation

Overview

Package ratelimit admits events per key against one or more sliding windows ("at most Limit in any Per"), in process or shared through Redis (or Garnet). Both stores keep the same log of admitted event times, so a limit and its retry time mean the same thing with and without Redis.

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 admits events from per-key allowances.

func New

func New(o Options) (*Limiter, error)

New validates o and builds its Limiter.

func (*Limiter) Allow

func (l *Limiter) Allow(ctx context.Context, key string) (bool, time.Duration)

Allow admits one event for key, or refuses it (not counting it) with how long until one fits. A Redis error falls back to the per-process log for redisBackoff.

type Options

type Options struct {
	Windows []Window // an event must fit every one
	// Redis shares the log across replicas; nil keeps it per process.
	Redis  redis.UniversalClient
	Prefix string // Redis key prefix
	// Errors counts Redis failures; each is served by the per-process log.
	Errors *expvar.Int
	Logger *slog.Logger
	Now    func() time.Time // default time.Now
}

Options configure a Limiter.

type Window

type Window struct {
	Limit int
	Per   time.Duration
}

Window admits at most Limit events in any Per.

Jump to

Keyboard shortcuts

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