Integrations

Once you’ve chosen your rate limit strategy you can either use it directly or integrates it with some of the existing components of Tolerance.

  • Operation Runner is an Operation Runner that will run your operations based on the rate limit.

Operation Runner

The Rate Limited Operation Runner is the integration of rate limiting with operation runners. That way you can ensure that all the operations you want to run will actually run at the given time rate.

1
2
3
4
5
6
7
8
9
$rateLimit = /* The implementation you wants */;

$operationRunner = new RateLimitedOperationRunner(
    new SimpleOperationRunner(),
    $rateLimit,
    new SleepWaiter()
);

$operationRunner->run($operation);

By default, the identifier given to the rate limit is an empty string. The optional fourth parameter is an object implementing the ThrottlingIdentifierStrategy interface that will returns the identifier of the operation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class ThrottlingIdentifierStrategy implements ThrottlingIdentifierStrategy
{
    /**
     * {@inheritdoc}
     */
    public function getOperationIdentifier(Operation $operation)
    {
        if ($operation instanceof MyClientOperation) {
            return sprintf(
                'client-%s',
                $operation->getClient()->getIdentifier()
            );
        }

        return 'unknown-client';
    }
}