ratelimit

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: GPL-3.0 Imports: 8 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 in-memory fixed-window limits and routing middleware.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Enforce

func Enforce(l *Limiter, rule Rule) middleware.Middleware

func Inject

func Inject(l *Limiter) middleware.Middleware

Types

type Engine

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

Engine applies registered rate-limit rules as middleware.

func NewEngine

func NewEngine(l *Limiter) *Engine

NewEngine creates a rule engine using l or a new limiter when nil.

func (*Engine) AddRule

func (e *Engine) AddRule(rule Rule)

func (*Engine) Limiter

func (e *Engine) Limiter() *Limiter

func (*Engine) Middleware

func (e *Engine) Middleware() middleware.Middleware

type Limiter

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

Limiter tracks fixed-window counters by action and key.

func NewLimiter

func NewLimiter() *Limiter

NewLimiter creates an empty concurrency-safe limiter.

func NewLimiterWithCapacity added in v2.0.1

func NewLimiterWithCapacity(maxEntries int) *Limiter

NewLimiterWithCapacity creates a limiter with a fail-closed cardinality bound. Existing callers of NewLimiter retain the historical API.

func (*Limiter) Allow

func (l *Limiter) Allow(action, key string, limit int, window time.Duration) (bool, time.Duration)

Allow returns whether the action is allowed and retry-after duration when denied.

type Rule

type Rule struct {
	Action     string
	Limit      int
	Window     time.Duration
	Scope      Scope
	Command    string
	Callback   string
	KeyFunc    func(*botctx.Context) string
	OnDenyText string
	OnlyType   event.Type
}

type Scope

type Scope string
const (
	ScopeUser     Scope = "user"
	ScopeChat     Scope = "chat"
	ScopeCommand  Scope = "command"
	ScopeCallback Scope = "callback"
	ScopeGlobal   Scope = "global"
	ScopeCustom   Scope = "custom"
)

Jump to

Keyboard shortcuts

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