ratelimit

package
v0.77.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: Apache-2.0 Imports: 28 Imported by: 1 module, 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 token-bucket rate limiting for MCP servers.

The public API consists of the Limiter interface, Decision result type, and NewLimiter constructor. The token bucket implementation is internal.

Index

Constants

View Source
const (
	CodeRateLimited    = contract.CodeRateLimited
	MessageRateLimited = contract.MessageRateLimited
)

Rate-limited JSON-RPC error code and message, aliased from contract.

View Source
const (
	// MiddlewareType is the type constant for the rate limit middleware.
	MiddlewareType = "ratelimit"
)

Variables

This section is empty.

Functions

func Allow added in v0.33.0

func Allow(ctx context.Context, limiter Limiter, identity *auth.Identity, toolName string) error

Allow checks whether identity may call toolName through limiter; see contract.Allow.

func CreateMiddleware added in v0.17.0

func CreateMiddleware(config *types.MiddlewareConfig, runner types.MiddlewareRunner) error

CreateMiddleware is the factory function for rate limit middleware.

func NewMiddleware added in v0.30.0

func NewMiddleware(params MiddlewareParams) (types.Middleware, error)

NewMiddleware creates a Redis-backed rate limit middleware from typed params.

Types

type Decision

type Decision = contract.Decision

Decision is an alias of contract.Decision.

type Limiter

type Limiter = contract.Limiter

Limiter is an alias of contract.Limiter.

func NewLimiter

func NewLimiter(client redis.Cmdable, namespace, name string, crd *v1beta1.RateLimitConfig) (Limiter, error)

NewLimiter constructs a Limiter from CRD configuration. Returns a no-op limiter (always allows) when crd is nil. namespace and name identify the MCP server for Redis key derivation.

func NewRedisLimiter added in v0.33.0

func NewRedisLimiter(params MiddlewareParams) (Limiter, io.Closer, error)

NewRedisLimiter creates a Redis-backed rate limiter from typed params.

type MiddlewareParams added in v0.17.0

type MiddlewareParams struct {
	Namespace  string                   `json:"namespace"`
	ServerName string                   `json:"server_name"`
	Config     *v1beta1.RateLimitConfig `json:"config"`
	RedisAddr  string                   `json:"redis_addr,omitempty"`
	RedisDB    int32                    `json:"redis_db,omitempty"`
	RedisTLS   *redisconfig.TLSConfig   `json:"redis_tls,omitempty"`
}

MiddlewareParams holds the parameters for the rate limit middleware factory.

type RateLimitedError added in v0.33.0

type RateLimitedError = contract.RateLimitedError

RateLimitedError is an alias of contract.RateLimitedError.

Directories

Path Synopsis
Package contract defines the rate limiter contract that enforcement adapters use, without the Redis-backed limiter in pkg/ratelimit.
Package contract defines the rate limiter contract that enforcement adapters use, without the Redis-backed limiter in pkg/ratelimit.
internal
bucket
Package bucket implements the token bucket algorithm backed by Redis.
Package bucket implements the token bucket algorithm backed by Redis.
Package types defines shared rate limit configuration types.
Package types defines shared rate limit configuration types.

Jump to

Keyboard shortcuts

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