ratelimit

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 9 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 ratelimit implements per-user, per-agent, and per-tenant rate limiting, configurable via langgraph.json's "rate_limit" section.

Two backends:

  • memory (default): process-local token buckets via golang.org/x/time/rate
  • redis: shared Lua token buckets on REDIS_URL, so N control-plane replicas share one ceiling instead of each enforcing its own copy

Disabled entirely (zero overhead) unless a rate_limit section is configured.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(limiter *Limiter, next http.Handler) http.Handler

Middleware enforces the global, per-user, and per-tenant dimensions. Per-agent enforcement happens separately, directly in handlers that have already parsed agent_id from the body (createRun and POST /governance/decide). Sniffing it generically here would mean buffering and restoring the request body on every route just for those few.

Must be mounted inside auth.Middleware (i.e. auth runs first) so auth.FromContext has an identity, and tenant.FromContext has a tenant, to key their respective buckets on.

Types

type Config

type Config struct {
	Backend   string // "memory", "redis", or "" (auto)
	Global    *Rule
	PerUser   *Rule
	PerAgent  *Rule
	PerTenant *Rule
}

Config is rate-limiting configuration, loaded from langgraph.json's "rate_limit" section (see internal/config.RateLimitEntry). Any subset of dimensions may be configured; unconfigured dimensions are unlimited.

Backend selects the store: "memory" (process-local, default), "redis" (shared across replicas via REDIS_URL), or "" (auto: redis when a client is supplied at construction, otherwise memory).

PerTenant is keyed by tenant.FromContext -- which resolves to tenant.DefaultTenant when multi-tenancy isn't configured, meaning a per_tenant rule on a single-tenant deployment behaves identically to a global rule (one bucket, "default"). It only becomes a real per-tenant limit once auth is configured to supply real tenant IDs (see the Multi-tenancy section of the README).

type ErrRateLimited

type ErrRateLimited struct {
	Scope string // "global", "user", "agent", "tenant"
}

ErrRateLimited is returned by Limiter checks and by createRun when a configured limit is exceeded. internal/api's handleStoreError maps this to HTTP 429.

func (*ErrRateLimited) Error

func (e *ErrRateLimited) Error() string

type Limiter

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

Limiter enforces a Config's rules using per-scope token buckets.

func New

func New(cfg *Config) *Limiter

New builds a process-local (memory) Limiter from cfg. cfg may be nil (limiter is then always a pass-through, matching "disabled by default").

func NewRedis

func NewRedis(cfg *Config, rdb RedisClient) *Limiter

NewRedis builds a Limiter whose buckets live in Redis (shared across every replica using the same REDIS_URL). rdb must be non-nil and already pinged by the caller. cfg must be enabled (at least one dimension set).

func (*Limiter) AllowAgent

func (l *Limiter) AllowAgent(agentID string) bool

AllowAgent checks the per-agent bucket for agentID.

func (*Limiter) AllowGlobal

func (l *Limiter) AllowGlobal() bool

AllowGlobal checks (and, if allowed, consumes) the global bucket.

func (*Limiter) AllowTenant

func (l *Limiter) AllowTenant(tenantID string) bool

AllowTenant checks the per-tenant bucket for tenantID (see tenant.FromContext -- always non-empty, "default" when unconfigured).

func (*Limiter) AllowUser

func (l *Limiter) AllowUser(identity string) bool

AllowUser checks the per-user bucket for identity. An empty identity (no auth provider configured, so there's no user to key on) is unlimited for this dimension -- global still applies.

func (*Limiter) BackendName

func (l *Limiter) BackendName() string

BackendName reports which store is in use ("memory", "redis", or "" when disabled). Useful for startup logs.

func (*Limiter) Enabled

func (l *Limiter) Enabled() bool

Enabled reports whether any dimension is configured. Callers use this to skip all rate-limit bookkeeping entirely when it's nil/unconfigured.

type RedisClient

type RedisClient interface {
	Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd
}

RedisClient is the subset of go-redis used by redisBackend -- small so tests can stub Eval without a real server when needed.

type Rule

type Rule struct {
	RPS   float64
	Burst int
}

Rule configures one token bucket: RPS is the sustained rate, Burst is the peak capacity (how many requests can arrive back-to-back before limiting kicks in).

Jump to

Keyboard shortcuts

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