securitylimit

package
v0.2.0-dev.8 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0 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 securitylimit provides the shared backend for security-sensitive limits.

Index

Constants

View Source
const DefaultMaxKeysPerName = 8192

DefaultMaxKeysPerName is the per-name cap used when NewMemory gets no positive cap.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket added in v0.1.27

type Bucket struct {
	Name   string
	Burst  float64
	Refill time.Duration
}

Bucket describes a token bucket.

type Decision

type Decision struct {
	Allowed     bool
	RetryAfter  time.Duration
	Remaining   int
	First       bool
	WindowStart time.Time
}

Decision describes a limiter operation. First is set on the first request that reaches a window limit, so callers can report that lockout once.

type Limiter

type Limiter interface {
	Take(ctx context.Context, b Bucket, key string) (Decision, error)
	Peek(ctx context.Context, w Window, key string) (Decision, error)
	Record(ctx context.Context, w Window, key string) (Decision, error)
	// RecordN atomically reserves n successful units when all of them fit in the window and returns that window's start.
	RecordN(ctx context.Context, w Window, key string, n int) (Decision, error)
	// UndoN refunds a reservation only if its original window is still current.
	UndoN(ctx context.Context, w Window, key string, n int, windowStart time.Time) error
	Reset(ctx context.Context, w Window, key string) error
}

Limiter covers token buckets and bounded failure windows used by security checks.

type Memory

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

Memory is the VPS implementation. Names isolate each limiter's key space.

func NewMemory

func NewMemory(now func() time.Time, maxKeysPerName int) *Memory

NewMemory returns an in-memory limiter with the production clock and key cap when the corresponding arguments are nil or non-positive.

func (*Memory) Peek added in v0.1.27

func (m *Memory) Peek(ctx context.Context, spec Window, key string) (Decision, error)

func (*Memory) Record added in v0.1.27

func (m *Memory) Record(ctx context.Context, spec Window, key string) (Decision, error)

func (*Memory) RecordN

func (m *Memory) RecordN(ctx context.Context, spec Window, key string, n int) (Decision, error)

RecordN reserves n successful units atomically. Unlike Record, it never records a partial reservation: callers can reject a whole operation before changing any users.

func (*Memory) Reset

func (m *Memory) Reset(ctx context.Context, spec Window, key string) error

func (*Memory) Size

func (m *Memory) Size(name string) int

Size reports the number of remembered window keys in a namespace.

func (*Memory) Take

func (m *Memory) Take(ctx context.Context, spec Bucket, key string) (Decision, error)

func (*Memory) UndoN

func (m *Memory) UndoN(ctx context.Context, spec Window, key string, n int, windowStart time.Time) error

UndoN removes an unsuccessful reservation only from the exact window that accepted it.

type Window added in v0.1.27

type Window struct {
	Name    string
	Limit   int
	Span    time.Duration
	Lockout time.Duration
}

Window describes a counted window. A positive Lockout locks the key as soon as a Record reaches Limit.

Jump to

Keyboard shortcuts

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