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
- Variables
- type Limiter
- func (l *Limiter) AddRule(key string, maxRequests, windowSeconds int) error
- func (l *Limiter) Allow(ctx context.Context, key string, limit int, window time.Duration) error
- func (l *Limiter) AllowN(ctx context.Context, key string, n int, limit int, window time.Duration) error
- func (l *Limiter) HTTPMiddleware(limit int, window time.Duration) func(http.Handler) http.Handler
- func (l *Limiter) Init() error
- func (l *Limiter) ListRules() ([]RuleEntry, error)
- func (l *Limiter) MCPMiddleware(limit int, window time.Duration) MCPMiddlewareFunc
- func (l *Limiter) Reload() error
- func (l *Limiter) RemoveRule(key string) error
- func (l *Limiter) StartReloader(ctx context.Context)
- type MCPMiddlewareFunc
- type Option
- type RuleConfig
- type RuleEntry
Constants ¶
const Schema = `` /* 316-byte string literal not displayed */
Schema creates the rate limiter configuration table.
Variables ¶
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 ¶
New creates a Limiter backed by the given database. Call Init() to create tables, then Reload() to load rules.
func (*Limiter) AddRule ¶
AddRule inserts or updates a rule in the database. Call Reload() to pick up changes.
func (*Limiter) Allow ¶
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 ¶
HTTPMiddleware returns an HTTP middleware that rate-limits by client IP. The key format is "ip:{client_ip}".
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) RemoveRule ¶
RemoveRule deactivates a rule in the database.
func (*Limiter) StartReloader ¶
StartReloader starts background goroutines for rule reloading (every 60s) and bucket GC (every 5min). Stops when ctx is cancelled.
type MCPMiddlewareFunc ¶
MCPMiddlewareFunc is a function that wraps tool execution with rate limiting. It takes the tool name as the rate limit key.
type RuleConfig ¶
RuleConfig defines the rate limit for a single key pattern.