ratelimit

package
v1.9.2 Latest Latest
Warning

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

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

Documentation

Overview

Package ratelimit counts requests and tokens in fixed windows for the quota stage's RPM and TPM checks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decision

type Decision struct {
	// Allowed reports whether the window was within limit before this
	// charge. A charge of 0 checks without counting.
	Allowed bool
	// Count is the window's count after the charge.
	Count int64
	// RetryAfter is the time left in the window.
	RetryAfter time.Duration
}

Decision is what Allow decided.

type Limiter

type Limiter interface {
	Allow(ctx context.Context, key string, n, limit int64, window time.Duration) (Decision, error)
	Kind() string
}

Limiter charges n against key's current fixed window of the given size. It always charges, even when it refuses, so a client that keeps retrying inside a window stays refused until the window ends.

type Memory

type Memory struct {
	// contains filtered or unexported fields
}

Memory is the default limiter. Its windows live in this process, so with several replicas each one allows the full limit. Safe for concurrent use.

func NewMemory

func NewMemory(opts ...MemoryOption) *Memory

NewMemory returns an in-process limiter.

func (*Memory) Allow

func (m *Memory) Allow(_ context.Context, key string, n, limit int64, window time.Duration) (Decision, error)

Allow charges n against key's current fixed window of the given size. It returns a Decision and an error. If window is <= 0 or n is < 0, Allow returns a zero Decision and an error.

func (*Memory) Kind

func (m *Memory) Kind() string

Kind returns the type of this limiter.

type MemoryOption

type MemoryOption func(*Memory)

MemoryOption configures Memory.

func WithClock

func WithClock(now func() time.Time) MemoryOption

WithClock replaces time.Now, for tests.

Directories

Path Synopsis
Package redislimit is a ratelimit.Limiter whose windows live in Redis, so every replica shares them.
Package redislimit is a ratelimit.Limiter whose windows live in Redis, so every replica shares them.

Jump to

Keyboard shortcuts

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