2017 © Pedro Peláez
 

library throttler

image

ob-ivan/throttler

  • Friday, February 9, 2018
  • by ob-ivan
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Throttler

Execute tasks in a loop at limited rate., (*1)

This answers a question on StackOverflow: https://stackoverflow.com/questions/48547971/restrict-function-to-maximum-100-executions-per-minute/48551632, (*2)

Other implementations of the same idea you may want to have a look at: - https://packagist.org/packages/franzip/throttler - https://packagist.org/packages/andrey-mashukov/throttler - https://packagist.org/packages/queryyetsimple/throttler, (*3)

Installation

composer require ob-ivan/throttler

Usage

Suppose you want to send requests to an API based on data from DB, but the API only allows 100 requests per minute., (*4)

You can use a throttler to do that like this:, (*5)

use Ob_Ivan\Throttler\Throttler;

$throttler = new Throttler(
    100, // number of requests
    60 // number of seconds in a unit of time
);
$throttler->run($job);

The $job variable here stands for your implementation of JobInterface:, (*6)

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);
    }

    // ...
}

And that's it., (*7)

Development

To install phpunit locally:, (*8)

composer install

To run tests:, (*9)

vendor/bin/phpunit

License

MIT, (*10)

The Versions

09/02 2018

dev-master

9999999-dev

  Sources   Download

The Development Requires

09/02 2018

v1.0

1.0.0.0

  Sources   Download

The Development Requires