securitylimit

package
v0.1.26 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: AGPL-3.0 Imports: 5 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

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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, name, key string, now time.Time, burst float64, refill time.Duration, cost float64) (Decision, error)
	CheckWindow(ctx context.Context, name, key string, now time.Time, limit int, window time.Duration, maxKeys int, touch bool) (Decision, error)
	RecordWindow(ctx context.Context, name, key string, now time.Time, limit int, window time.Duration, maxKeys int) (Decision, error)
	FailWindow(ctx context.Context, name, key string, now time.Time, window time.Duration, maxKeys int) error
	Reset(ctx context.Context, name, 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() *Memory

NewMemory returns a fresh in-memory limiter backend.

func (*Memory) CheckWindow

func (m *Memory) CheckWindow(ctx context.Context, name, key string, now time.Time, limit int, duration time.Duration, maxKeys int, touch bool) (Decision, error)

func (*Memory) FailWindow

func (m *Memory) FailWindow(ctx context.Context, name, key string, now time.Time, duration time.Duration, maxKeys int) error

FailWindow adds a failed attempt without a threshold check. It is paired with CheckWindow when the caller only counts failed operations, as Enroll does.

func (*Memory) RecordWindow

func (m *Memory) RecordWindow(ctx context.Context, name, key string, now time.Time, limit int, duration time.Duration, maxKeys int) (Decision, error)

func (*Memory) Reset

func (m *Memory) Reset(ctx context.Context, name, 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, name, key string, now time.Time, burst float64, refill time.Duration, cost float64) (Decision, error)

Jump to

Keyboard shortcuts

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