ratelimit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package ratelimit provides a generic, transport-agnostic rate limiter backed by SQLite. It can rate-limit by any key: tool name, user ID, IP address, or any composite key.

The limiter uses a sliding window counter stored in SQLite for persistence and an in-memory fast path for hot keys.

Index

Constants

View Source
const Schema = `` /* 316-byte string literal not displayed */

Schema creates the rate limiter configuration table.

Variables

View Source
var ErrRateLimited = errors.New("ratelimit: rate limit exceeded")

ErrRateLimited is returned when a rate limit is exceeded.

Functions

This section is empty.

Types

type Limiter

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

Limiter is a generic rate limiter with SQLite-backed configuration and in-memory token buckets.

func New

func New(db *sql.DB, opts ...Option) *Limiter

New creates a Limiter backed by the given database. Call Init() to create tables, then Reload() to load rules.

func (*Limiter) AddRule

func (l *Limiter) AddRule(key string, maxRequests, windowSeconds int) error

AddRule inserts or updates a rule in the database. Call Reload() to pick up changes.

func (*Limiter) Allow

func (l *Limiter) Allow(ctx context.Context, key string, limit int, window time.Duration) error

Allow checks and consumes a token for the given key. Uses the provided limit and window if no rule is configured in the database for this key. Returns nil if allowed, ErrRateLimited if exhausted.

func (*Limiter) AllowN

func (l *Limiter) AllowN(ctx context.Context, key string, n int, limit int, window time.Duration) error

AllowN checks and consumes n tokens for the given key.

func (*Limiter) HTTPMiddleware

func (l *Limiter) HTTPMiddleware(limit int, window time.Duration) func(http.Handler) http.Handler

HTTPMiddleware returns an HTTP middleware that rate-limits by client IP. The key format is "ip:{client_ip}".

func (*Limiter) Init

func (l *Limiter) Init() error

Init creates the rate_limiter_rules table.

func (*Limiter) ListRules

func (l *Limiter) ListRules() ([]RuleEntry, error)

ListRules returns all rules (active and inactive).

func (*Limiter) MCPMiddleware

func (l *Limiter) MCPMiddleware(limit int, window time.Duration) MCPMiddlewareFunc

MCPMiddleware returns a function that can be used as a PolicyFunc in mcprt. The key format is "tool:{tool_name}".

func (*Limiter) Reload

func (l *Limiter) Reload() error

Reload loads all active rules from the database.

func (*Limiter) RemoveRule

func (l *Limiter) RemoveRule(key string) error

RemoveRule deactivates a rule in the database.

func (*Limiter) StartReloader

func (l *Limiter) StartReloader(ctx context.Context)

StartReloader starts background goroutines for rule reloading (every 60s) and bucket GC (every 5min). Stops when ctx is cancelled.

type MCPMiddlewareFunc

type MCPMiddlewareFunc func(ctx context.Context, toolName string) error

MCPMiddlewareFunc is a function that wraps tool execution with rate limiting. It takes the tool name as the rate limit key.

type Option

type Option func(*Limiter)

Option configures a Limiter.

func WithClock

func WithClock(fn func() time.Time) Option

WithClock sets a custom clock function (for testing).

type RuleConfig

type RuleConfig struct {
	MaxRequests   int
	WindowSeconds int
	IsActive      bool
}

RuleConfig defines the rate limit for a single key pattern.

type RuleEntry

type RuleEntry struct {
	Key           string
	MaxRequests   int
	WindowSeconds int
	IsActive      bool
}

RuleEntry is a row from rate_limiter_rules.

Jump to

Keyboard shortcuts

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