Search by

raxos / rate-limit

basmilius

Rate Limit for raxos/router.

3.2.0 2026-10-02 17:11 UTC

README

Bas Milius

Raxos Rate Limit

Quotas stored in Redis for application operations and Raxos Router endpoints.

Documentation | Packagist | Raxos

  • Quotas per second, minute, hour or day.
  • Atomic counters with expiration and usage snapshots.
  • Router middleware for HTTP 429 responses and rate-limit headers.

Installation

Requires PHP 8.5 or later. Composer checks the remaining package and extension dependencies declared in composer.json.

composer require "raxos/rate-limit:^3.2"

Usage

<?php
declare(strict_types=1);

use Raxos\Cache\Redis\RedisCache;
use Raxos\RateLimit\Rate;
use Raxos\RateLimit\RateLimiter;
use Raxos\RateLimit\Store\RedisRateLimiterStore;

require __DIR__ . '/vendor/autoload.php';

$redis = new RedisCache(prefix: 'app:', host: '127.0.0.1', port: 6379);
$store = new RedisRateLimiterStore($redis, keyBase: 'app:ratelimit:');
$limiter = new RateLimiter(Rate::minute(60), $store);

$status = $limiter->getStatus('user:42');
echo json_encode(['limited' => $status->exceeded, 'remainingSeconds' => $status->ttl]);

This example needs a running Redis server and the Redis PHP extension through raxos/cache. getStatus() counts the operation by default; use increment: false to inspect usage. checkLimited() counts an operation and throws when the quota is exceeded, so choose one counting method per operation. RedisRateLimiterStore requires a plain RedisCache; configure its key namespace with keyBase.

Documentation

Testing

Run this library's Pest suite from the Raxos workspace:

git clone --recurse-submodules https://github.com/basmilius/raxos.git
cd raxos
composer install
vendor/bin/pest --testsuite=rate-limit

See Testing Raxos for PHP extensions, integration services and coverage commands. The library's Tests workflow also runs in GitHub Actions.

License

MIT. Copyright (c) 2017 - present Bas Milius.