How to Rate‑Limit Asynchronous Tasks with ReactPHP‑MQ

ReactPHP‑MQ provides a lightweight, SOLID‑designed queue for managing concurrent asynchronous operations, offering features like configurable concurrency limits, solid test coverage, and advanced methods such as all(), any(), and timeout handling for robust rate‑limiting in PHP applications.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Rate‑Limit Asynchronous Tasks with ReactPHP‑MQ

ReactPHP‑MQ aims to efficiently rate‑limit concurrent operations, preventing resource exhaustion or remote server bans. It acts as an intelligent scheduler that queues and dispatches asynchronous tasks, ensuring that only a limited number run simultaneously—useful for scenarios like high‑volume web crawling.

Key Features

Asynchronous operation execution : Handles any number of async tasks with configurable concurrency and queue size, processing results immediately for faster response.

Lightweight SOLID design : Provides a clean abstraction built on well‑tested components, avoiding unnecessary complexity and letting developers focus on business logic.

Comprehensive test coverage : Includes an automated test suite that has been extensively exercised in real‑world use, ensuring stability and reliability.

Installation

Install via Composer:

composer require clue/mq-react:^1.7

Basic Usage Example

The following PHP script demonstrates how to limit concurrent HTTP requests to three using ReactPHP‑MQ:

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

$browser = new React\Http\Browser();
$urls = file('urls.txt');

$q = new Clue\React\Mq\Queue(3, null, function ($url) use ($browser) {
    return $browser->get($url);
});

foreach ($urls as $url) {
    $q($url)->then(
        function (Psr\Http\Message\ResponseInterface $response) use ($url) {
            echo $url . ': ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;
        },
        function (Exception $e) {
            echo 'Error: ' . $e->getMessage() . PHP_EOL;
        }
    );
}

This example creates a React\Http\Browser for HTTP requests, a Queue limited to three concurrent tasks, and processes each URL from urls.txt, outputting the response size or any errors.

Advanced Usage

all() Method

Queue::all()

runs multiple tasks concurrently and resolves when all succeed; any failure rejects the whole operation.

$browser = new React\Http\Browser();
$promise = Queue::all(3, $urls, function ($url) use ($browser) {
    return $browser->get($url);
});
$promise->then(function (array $responses) {
    echo 'All ' . count($responses) . ' successful!' . PHP_EOL;
});

any() Method

Queue::any()

resolves as soon as the first task succeeds; it rejects only if every task fails.

$browser = new React\Http\Browser();
$promise = Queue::any(3, $urls, function ($url) use ($browser) {
    return $browser->get($url);
});
$promise->then(function (ResponseInterface $response) {
    echo 'First response: ' . $response->getBody() . PHP_EOL;
});

Timeout Handling

By default, the library does not limit individual task duration, but you can add timeouts using react/promise-timer:

use React\Promise\Timer;

$q = new Queue(10, null, function ($uri) use ($browser) {
    return Timer\timeout($browser->get($uri), 2.0);
});

$promise = $q($uri);
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Concurrency ControlPHPrate limitingReactPHPasynchronous queue
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.