securitylimit

package
v0.1.81-dev.6 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 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
}

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)
	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) 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)

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