ratelimit

package
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 4 Imported by: 4 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 throttles repeated failures, such as failed logins.

The default behavior is a growing back-off rather than a hard lockout: after Threshold failures within Window, the key is blocked for BaseDelay, and each further failure doubles the delay up to MaxDelay. A hard lockout lets anyone lock any user out, so it is left to LockoutHooks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Threshold int           // failures before blocking; default 5
	Window    time.Duration // failure counting window; default 15m
	BaseDelay time.Duration // first block; default 1s
	MaxDelay  time.Duration // cap on the block; default 15m
	MaxKeys   int           // memory bound; default 100k (oldest windows evicted first)

	// Now returns the current time. Nil means time.Now.
	Now func() time.Time
}

Config tunes the in-memory limiter. Zero values take the defaults shown.

type Limiter

type Limiter interface {
	// Check reports whether key may make an attempt, without counting one.
	Check(ctx context.Context, key string) (Result, error)

	// Fail records a failure and returns the key's new state.
	Fail(ctx context.Context, key string) (Result, error)

	// Reset clears a key, typically after a successful attempt.
	Reset(ctx context.Context, key string) error
}

Limiter tracks failures per key. Implementations must be safe for concurrent use. Keys are opaque strings chosen by the caller.

type LockoutHooks

type LockoutHooks interface {
	// OnFailure is called after every recorded failure.
	OnFailure(ctx context.Context, key string, r Result)

	// OnBlocked is called when a failure causes key to become blocked.
	OnBlocked(ctx context.Context, key string, r Result)
}

LockoutHooks lets the application react to failures, for example to send a notification or apply a hard lockout in its own user store.

type Memory

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

Memory is an in-process Limiter. In a multi-instance deployment each instance counts separately; use a shared store (see the redisstore module).

func NewMemory

func NewMemory(cfg Config) (*Memory, error)

NewMemory returns an in-memory limiter.

func (*Memory) Check

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

Check implements Limiter.

func (*Memory) Fail

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

Fail implements Limiter.

func (*Memory) Reset

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

Reset implements Limiter.

type Result

type Result struct {
	Allowed    bool          // false while the key is blocked
	RetryAfter time.Duration // how long until the block ends (0 if allowed)
	Failures   int           // failures counted in the current window
}

Result describes a key's state.

Jump to

Keyboard shortcuts

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