Skip to content

Rate limiting ​

Every request to a route in the fusion-stack middleware group (in Rust, a route wrapped in the FusionStack middleware) counts against a limit: per client for authenticated requests, and per IP address for guests. Clients see where they stand on every response, so a well-behaved one can slow down before it is refused.

Headers ​

Every response carries the standard rate limit headers, errors included:

http
RateLimit-Limit: 300
RateLimit-Remaining: 297
RateLimit-Reset: 60

RateLimit-Reset is the number of seconds until the window starts again. A request over the limit gets a 429 with Retry-After:

http
HTTP/1.1 429 Too Many Requests
RateLimit-Limit: 3
RateLimit-Remaining: 0
RateLimit-Reset: 60
Retry-After: 60
json
{
  "errors": [
    {
      "status": "429",
      "code": "rate_limited",
      "title": "Too many requests",
      "detail": "This client is limited to 3 requests per 60 seconds. Retry in 60 seconds.",
      "meta": {
        "limit": 3,
        "retry_after": 60,
        "request_id": "req_01m8rcpyh7g7v7j0cj5fdsdy5q"
      }
    }
  ]
}

The TypeScript client waits for Retry-After and retries for you.

Tiers ​

Limits are set per tier, in config/api.php (Config in Rust):

php
// config/api.php
'rate_limit' => [
    'enabled' => true,
    'tiers' => [
        'guest' => ['limit' => 60, 'per_seconds' => 60],
        'default' => ['limit' => 300, 'per_seconds' => 60],
        'partner' => ['limit' => 3000, 'per_seconds' => 60],
        'internal' => ['limit' => null], // unlimited
    ],
],
rust
use fusion_stack::Config;
use fusion_stack::rate_limit::Tier;

Config::new()
    .rate_limit(true)
    .rate_limit_tier("guest", Tier::new(60, 60))
    .rate_limit_tier("default", Tier::new(300, 60))
    .rate_limit_tier("partner", Tier::new(3000, 60))
    .rate_limit_tier("internal", Tier::unlimited())
  • Guests use guest.
  • An API key uses its rate_limit_tier column. Set it when you issue the key: ApiKey::issue('Acme', ['orders:read'], attributes: ['rate_limit_tier' => 'partner']), or in Rust api_keys::issue with rate_limit_tier: Some("partner".to_owned()) in its Issue.
  • Other clients use the attribute named in api.auth.attributes.rate_limit_tier, such as a column on the user or a claim on a token, or whatever their resolver returns. In Rust, that is the tier your resolver sets with ApiClient::rate_limit_tier.
  • A client without a tier, or with one not listed, uses default.

Counts live in the cache store Laravel's cache.limiter config names, or the default store; in Rust, always the default store. Use Redis, or another store every server shares, when you run more than one.

Released under the MIT License.