PHP code example of nevsnode / backoff

1. Go to this page and download the library: Download nevsnode/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/ */

    

nevsnode / backoff example snippets



use nevsnode\Backoff;

$backoff = new Backoff();

$resource = new ExampleResource();
$result = $backoff->retryOnException(5, function () use ($resource) {
    $return = $resource->fetchSomething();
    if (!$return) {
        throw new Exception('Failed to fetch something');
    }
    return $return;
});


$backoff = new nevsnode\Backoff();

// $result will now become the string "Some error"
$result = $backoff->retryOnException(3, function () {
    throw new \Exception('Some error');
}, function ($e) {
    return $e->getMessage();
});

// $result will now become FALSE
$result = $backoff->retryOnException(2, function () {
    throw new \Exception('Some error');
}, false);



// pass settings to constructor
$backoff = new Backoff([
    'min' => 2000,
    'max' => 10000,
    'factor' => M_E,
]);

// define setting through setter
$backoff->setJitter(true);
$backoff->setJitterMax(6000);
$backoff->setMin(3000);

// return setting through getter
$max = $backoff->getMax();