PHP code example of aportela / simple-throttle
1. Go to this page and download the library: Download aportela/simple-throttle library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
aportela / simple-throttle example snippets
er = 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);
}
}