ratelimit

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ratelimit provides fixed-window counters shared by all API replicas through Redis, with an in-memory fallback when Redis is not configured or temporarily unreachable.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

type Counter interface {
	// Hit records one event for key and returns the count in the current
	// window and the time until the window resets.
	Hit(ctx context.Context, key string, window time.Duration) (count int64, reset time.Duration, err error)
	// Count returns the current count without recording an event.
	Count(ctx context.Context, key string) (count int64, reset time.Duration, err error)
	// Reset clears key.
	Reset(ctx context.Context, key string) error
}

Counter counts events per key in fixed time windows.

func New

func New(rdb *redis.Client, prefix string) Counter

New returns the counter to use: Redis with in-memory fallback, or memory only when rdb is nil.

type Fallback

type Fallback struct {
	Primary, Secondary Counter
	// contains filtered or unexported fields
}

Fallback uses primary and switches to secondary for any call where primary fails, so a Redis outage degrades limits to per-replica instead of disabling them or blocking logins.

func (*Fallback) Count

func (f *Fallback) Count(ctx context.Context, key string) (int64, time.Duration, error)

func (*Fallback) Hit

func (f *Fallback) Hit(ctx context.Context, key string, window time.Duration) (int64, time.Duration, error)

func (*Fallback) Reset

func (f *Fallback) Reset(ctx context.Context, key string) error

type Memory

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

Memory is a per-process Counter. It is correct for a single replica and is used as a fallback when Redis fails.

func NewMemory

func NewMemory() *Memory

NewMemory returns an empty in-memory counter.

func (*Memory) Count

func (m *Memory) Count(_ context.Context, key string) (int64, time.Duration, error)

func (*Memory) Hit

func (m *Memory) Hit(_ context.Context, key string, window time.Duration) (int64, time.Duration, error)

func (*Memory) Reset

func (m *Memory) Reset(_ context.Context, key string) error

type Redis

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

Redis is a Counter shared by every replica using the same Redis.

func NewRedis

func NewRedis(rdb *redis.Client, prefix string) *Redis

NewRedis returns a Redis counter whose keys start with prefix.

func (*Redis) Count

func (r *Redis) Count(ctx context.Context, key string) (int64, time.Duration, error)

func (*Redis) Hit

func (r *Redis) Hit(ctx context.Context, key string, window time.Duration) (int64, time.Duration, error)

func (*Redis) Reset

func (r *Redis) Reset(ctx context.Context, key string) error

Jump to

Keyboard shortcuts

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