PHP Classes

Reactive Asynchronous Rate Limiter PHP: Limit the handling of requests to a given rate

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-02-20 (7 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
reactive-asynchronou 1.0GNU General Publi...7Performance and optimization, PHP 7
Description 

Author

This package can limit the handling of requests to a given rate.

It provides a class that can handle requests to execute tasks started from the command line and count the number of requests received in a given time.

The class can handle POSIX USR1 signals to trigger the handling of requests.

Picture of Nitesh Apte
  Performance   Level  
Name: Nitesh Apte <contact>
Classes: 20 packages by
Country: India India
Innovation award
Innovation award
Nominee: 6x

Winner: 1x

Example

<?php
include 'ReactiveRateLimiter.php';

// Allow 5 requests every 5 seconds
$rateLimiter = new ReactiveRateLimiter(5, 5.0);

// Simulate periodic requests
while (true) {
   
$rateLimiter->sendRequest();
   
usleep(200000); // 0.2 seconds between requests
}
?>


Details

Reactive and Asynchronous Rate Limiter in PHP

A reactive and asynchronous rate limiter in PHP, designed to handle high-frequency requests efficiently, this implementation leverages pcntl signals to provide non-blocking and lock-free request handling. It is a lightweight solution ideal for applications requiring precise and dynamic control over incoming request rates.

Features

  • Reactive and Asynchronous: - Utilizes PHP's `pcntl` library to process incoming requests reactively. - Ensures non-blocking behavior, making it suitable for high-concurrency environments.
  • No Locking Required: - Eliminates the need for semaphores, file locks, or databases. - Tracks requests entirely in memory for lightweight operation.
  • Dynamic Rate Management: - Configurable maximum requests per time window. - Handles blocked requests reactively with customizable timeout durations.

Code Overview

Core Class

The implementation includes the ReactiveRateLimiter class, which: 1. Dynamically tracks requests in memory. 2. Reacts to incoming signals using pcntl_signal for efficient concurrency. 3. Implements rate-limiting logic to allow or block requests based on defined thresholds.

Example Usage

<?php
include 'ReactiveRateLimiter.php';

// Allow 5 requests every 5 seconds
$rateLimiter = new ReactiveRateLimiter(5, 5.0); 	

// Simulate periodic requests
while (true) {
    $rateLimiter->sendRequest();
    usleep(200000); // 0.2 seconds between requests
}
?>

  Files folder image Files (4)  
File Role Description
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Plain text file ReactiveRateLimiter.php Class Class source
Accessible without login Plain text file README.md Doc. Read me script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0