1. Go to this page and download the library: Download eventsauce/backoff 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/ */
eventsauce / backoff example snippets
use EventSauce\BackOff\BackOffStrategy;
class BusinessLogic
{
public function __construct(
private ExternalDependency $dependency,
private BackOffStrategy $backOff,
) {}
public function performAction(): void
{
$tries = 0;
start:
try {
++$tries;
$this->dependency->actionThatMayFail();
} catch (Throwable $throwable) {
$this->backOff->backOff($tries, $throwable);
goto start;
}
}
}
use EventSauce\BackOff\ExponentialBackOffStrategy;
$backOff = new ExponentialBackOffStrategy(
100000, // initial delay in microseconds, 0.1 seconds
15, // max number of tries
2500000, // (optional) max delay in microseconds, default 2.5 seconds
2.0, // (optional) base to control the growth factor, default 2.0
);
$businessLogic = new BusinessLogic(new ExternalDependency(), $backOff);
try {
$businessLogic->performAction();
} catch (Throwable $throwable) {
// handle the throwable
}
use EventSauce\BackOff\FibonacciBackOffStrategy;
$backOff = new FibonacciBackOffStrategy(
100000, // initial delay in microseconds, 0.1 seconds
15, // max number of tries
2500000, // (optional) max delay in microseconds, default 2.5 seconds
);
$businessLogic = new BusinessLogic(new ExternalDependency(), $backOff);
try {
$businessLogic->performAction();
} catch (Throwable $throwable) {
// handle the throwable
}
use EventSauce\BackOff\LinearBackOffStrategy;
$backOff = new LinearBackOffStrategy(
100000, // initial delay in microseconds, 0.1 seconds
15, // max number of tries
2500000, // (optional) max delay in microseconds, default 2.5 seconds
);
$businessLogic = new BusinessLogic(new ExternalDependency(), $backOff);
try {
$businessLogic->performAction();
} catch (Throwable $throwable) {
// handle the throwable
}
use EventSauce\BackOff\ExponentialBackOffStrategy;
use EventSauce\BackOff\FibonacciBackOffStrategy;
use EventSauce\BackOff\LinearBackOffStrategy;
$exponential = new ExponentialBackOffStrategy(100000, 25, jitter: $jitter);
$fibonacci = new FibonacciBackOffStrategy(100000, 25, jitter: $jitter);
$linear = new LinearBackOffStrategy(100000, 25, jitter: $jitter);
use EventSauce\BackOff\Jitter\FullJitter;
$jitter = new FullJitter();
use EventSauce\BackOff\Jitter\HalfJitter;
$jitter = new HalfJitter();
use EventSauce\BackOff\Jitter\ScatteredJitter;
$jitter = new ScatteredJitter($range = 0.5);
use EventSauce\BackOff\BackOffStrategy;
function action(Client $client, BackOffStrategy $backOff): void
{
$tries = 0;
start:
$tries++;
$response = $client->doSomething();
if ($response == SomeParticular::VALUE) {
$backOff->backOff($tries, new LogicException('Exhausted back-off'));
goto start;
}
}
use EventSauce\BackOff\BackOffRunner;
use EventSauce\BackOff\ExponentialBackOffStrategy;
$strategy = new ExponentialBackOffStrategy(initialDelayMs: 100, maxTries: 5);
$runner = new BackOffRunner($strategy);
$runner->run(function () {
// Do something that might throw an exception!
});
use EventSauce\BackOff\BackOffRunner;
use EventSauce\BackOff\ExponentialBackOffStrategy;
$strategy = new ExponentialBackOffStrategy(initialDelayMs: 100, maxTries: 5);
$runner = new BackOffRunner($strategy, LogicException::class);
$runner->run(function () {
// Only LogicException is retried, the rest is not!
});