Runs a callable, retrying using exponential back-off if the callable fails.

use Haldayne\Fox\Retry;
$lambda = new Retry(function ($src, $dst) { return copy($src, $dst); });
$lambda->setAttempts(3);
if (false === $lambda('foo', 'bar')) {
    throw new \RuntimeException(sprintf(
         'Failed to copy file after %d tries',
         $lambda->getAttempts()
    ));
}

Network resources may not respond immediately, owing to temporary issues in resource availability. The usual technique for dealing with these error responses is to implement retry logic in the client. This technique increases reliability of the client and reduces operational costs for the developer.

If the given callback returns exactly boolean false, Retry will attempt the callback again after waiting exponentially longer times: 1 second, 2 seconds, 4 seconds, 8 seconds, etc. up to the maximum attempts limit. The default attempt limit is 5. Also, you can change the back-off strategy if you'd like.

package Haldayne

 Methods

__construct()

__construct(callable $code) 

Parameters

$code

__invoke()

__invoke() 

getAttempts()

getAttempts() 

getStrategy()

getStrategy() 

setAttempts()

setAttempts($attempts) 

Parameters

$attempts

setStrategy()

setStrategy(callable $callable) 

Parameters

$callable

 Properties

 

$attempts

$attempts 

Default

null
 

$code

$code 

Default

null
 

$strategy

$strategy 

Default

null