PHP code example of ob-ivan / throttler
1. Go to this page and download the library: Download ob-ivan/throttler 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/ */
ob-ivan / throttler example snippets
use Ob_Ivan\Throttler\Throttler;
$throttler = new Throttler(
100, // number of requests
60 // number of seconds in a unit of time
);
$throttler->run($job);
use Ob_Ivan\Throttler\JobInterface;
class MyAwesomeJob implements JobInterface {
private $id;
private $requestData;
private $responseData = [];
/**
* Advance the job to the next piece and tell if there is any.
*
* Prefetch the data to be sent on job execution.
* This way execution time will not be wasted on data fetching.
*
* @return bool True to continue execution. False to exit the loop.
*/
public function next(): bool {
++$id;
$row = $this->getRow($id);
if ($row) {
$this->requestData = $this->makeRequestData($row);
return true;
} else {
return false;
}
}
/**
* Perform the action the job is designed for.
*
* No return value is expected from this method.
* We rely on side effects to retrieve the response data.
*/
public function execute() {
$response = $this->requestApi($this->requestData);
$this->responseData[] = $this->extractResponseData($response);
}
// ...
}