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 ¶
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 ¶
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 ¶
AllowAgent checks the per-agent bucket for agentID.
func (*Limiter) AllowGlobal ¶
AllowGlobal checks (and, if allowed, consumes) the global bucket.
func (*Limiter) AllowTenant ¶
AllowTenant checks the per-tenant bucket for tenantID (see tenant.FromContext -- always non-empty, "default" when unconfigured).
func (*Limiter) AllowUser ¶
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 ¶
BackendName reports which store is in use ("memory", "redis", or "" when disabled). Useful for startup logs.