twinh/rate-limiter

There is no license information available for the latest version (v1.0.0) of this package.

A flexible and scalable Rate Limiter, supporting various common strategies and storage methods.

v1.0.0 2024-10-23 01:30 UTC

This package is auto-updated.

Last update: 2024-12-23 01:57:31 UTC


README

GitHub Workflow Status Coverage Status License

A flexible and scalable Rate Limiter, supporting various common strategies and storage methods.

Getting started

use RateLimiter\FixedWindow;
use RateLimiter\FixedWindow\MemoryStorage;

$limiter = new FixedWindow(new MemoryStorage(), windowSize: 10, limit: 1);
if (!$limiter->attempt('user_id')) {
    echo 'Too many requests';
}

Feature

  • ✨ Supports multiple strategies: FixedWindow, SlidingWindow, LeakyBucket, and TokenBucket (not yet).
  • 🖥️ Compatible with Redis, Memory, and Database (not yet) storage methods.
  • 💪 Robust and scalable architecture with atomic operations for high traffic and large user bases.
  • ⚙️ Flexible rate limit and window size configuration.
  • 🚪 Clear API for attempting, getting remaining attempts, and clearing rate limits.
  • 🌐 Supports global, and user-specific rate limits.

Install

composer require twinh/rate-limiter

Document

Initialize the FixedWindow Rate Limiter

use RateLimiter\FixedWindow;
use RateLimiter\FixedWindow\MemoryStorage;

$limiter = new FixedWindow(new MemoryStorage(), windowSize: 60, limit: 1);

Initialize the SlidingWindow Rate Limiter

use RateLimiter\SlidingWindow;
use RateLimiter\SlidingWindow\MemoryStorage;

$limiter = new SlidingWindow(new MemoryStorage(), windowSize: 60, limit: 1);

Initialize the LeakyBucket Rate Limiter

use RateLimiter\LeakyBucket;
use RateLimiter\LeakyBucket\MemoryStorage;

$limiter = new LeakyBucket(new LeakyMemoryStorage(), rate: 10, limit: 1);

Set Redis Storage

use RateLimiter\FixedWindow;
use RateLimiter\FixedWindow\RedisStorage;

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$limiter = new FixedWindow(new RedisStorage($redis), windowSize: 10, limit: 1);

Attempts to execute a rate limited operation

if (!$limiter->attempt('user_id')) {
    echo 'Too many requests';
}

Attempts to execute a rate limited operation with global scope

$limiter->attempt();

Attempts to execute a rate limited operation with client IP

$limiter->attempt($_SERVER['REMOTE_ADDR'] ?? 'unknown');

Attempts to execute a rate limited operation with server IP

$limiter->attempt($_SERVER['SERVER_ADDR'] ?? 'unknown');

Get the remaining attempts

$remaining = $limiter->getRemaining('user_id');

Clear the request limit

$limiter->clear('user_id');

Extend more storages and rate limiters

Add Storage

  1. Create a new class that implements the RateLimiter\Xxx\StorageInterface interface of the rate limiter.
  2. Implement the methods of the storage class.
  3. Create the rate limiter object with the new storage object.

Add Rate Limiter

  1. Create a new class that implements the RateLimiter\RateLimiterInterface.
  2. Implement the methods of the rate limiter class.
  3. Create a StorageInterface for the rate limiter class.
  4. Implement storage object.

Class Diagram

Class Diagram