aportela / simple-throttle
Custom php throttle system
Installs: 78
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/aportela/simple-throttle
Requires
- php: >=8.4
- psr/log: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.88
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.5
- rector/rector: ^2.2
README
This is a simple approach to implement exponential backoff for retrying operations that may fail intermittently, such as network requests (API calls?) or file access. While there are more robust and optimized solutions out there, this is just a lightweight strategy I use in some of my personal projects when I need to handle temporary failures without overcomplicating things. It’s not meant for production-grade systems, just a basic solution for my use cases.
Requirements
- mininum php version 8.4
Install (composer) dependencies:
composer require aportela/simple-throttle
Code example:
<?php require "vendor/autoload.php"; $logger = new \Psr\Log\NullLogger(""); $throttle = new \aportela\SimpleThrottle\Throttle($logger, 500, 5000); for ($i = 0; $i < 32; $i++) { // throttle default time between iterations $throttle->throttle(); // instead of this dummy code block, could you make a call to a remote API here that could fail due to a rate limit $failed = (bool) mt_rand(0, 1); if (! $failed) { echo "Success... reseting throttle to default" . PHP_EOL; // on interation success reset throttle to default time $throttle->reset(); } else { echo "Error... incrementing throttle" . PHP_EOL; // on interation error increment throttle ( ms = ms + 500 ms) $throttle->increment(\aportela\SimpleThrottle\ThrottleDelayIncrementType::INCREMENT_500_MILLISECONDS); } }