Skip to content

[Cache][Routing] Add token bucket rate limiting with dynamic request cost#58663

Open
Tresor-Kasenda wants to merge 1 commit intolaravel:12.xfrom
Tresor-Kasenda:feature/token-bucket-rate-limiter
Open

[Cache][Routing] Add token bucket rate limiting with dynamic request cost#58663
Tresor-Kasenda wants to merge 1 commit intolaravel:12.xfrom
Tresor-Kasenda:feature/token-bucket-rate-limiter

Conversation

@Tresor-Kasenda
Copy link
Contributor

This PR introduces native token bucket rate limiting to Laravel’s limiter stack, including HTTP middleware integration.

What’s new

  • Added Limit::tokenBucket($capacity, $refillPerSecond) builder.
  • Added TokenBucketLimit (capacity, refillPerSecond, cost(...)).
  • Added token-bucket runtime APIs to Illuminate\Cache\RateLimiter:
    • tokenBucket(...)
    • remainingTokens(...)
    • availableInTokenBucket(...)
  • Extended ThrottleRequests to support token bucket limits returned from named limiters.
  • Added support for dynamic per-request token cost via closure.
  • Updated ThrottleRequestsWithRedis to delegate to the parent middleware flow when token-bucket or after(...) callbacks are used.
  • Updated Illuminate\Support\Facades\RateLimiter phpdoc accordingly.

Why

Current fixed-window / counter-based limits are sometimes too coarse for heterogeneous workloads.
Token bucket allows smoother traffic shaping and weighted request cost handling (e.g. expensive endpoints consume more tokens).

Example

RateLimiter::for('api', fn ($request) => Limit::tokenBucket(100, 10)
    ->by($request->user()?->id ?: $request->ip())
    ->cost(fn ($request) => $request->is('reports/*') ? 5 : 1));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments