Retries tasks that fail due to transient errors. Well suited to network requests but can retry any callable.
Via Composer
composer require graze/transient-fault-handler
The transient fault handler takes two detection strategies and a retry strategy. The builder can be used to quickly create a handler.
$task = function () {
// Task that is prone to transient errors
};
$builder = new TransientFaultHandlerBuilder();
$transientFaultHandler = $builder->build();
$result = $transientFaultHandler->execute($task);
When a task is tried, it will either return some value or throw an exception.
The detection strategies will decide if that value/exception indicates a transient error or not.
If it does, then the fault handler will be told to retry the task. if it does not, then the value/exception either indicates a success or a non-transient error that retrying wouldn't solve.
In these cases, the value is returned to the caller or the exception is rethrown.
FalseyReturnValueDetectionStrategy
: treats return values that evaluate to false as transient.StaticDetectionStrategy
: returns a static value set when constructing the strategy, regardless of the return value or exception.
If the detection strategy decides that the task should be retried, the retry strategy will decide how long to wait before doing so (the backoff period), and optionally impose a maximum number of retries on the task.
ExponentialBackoffStrategy
: the backoff period is chosen randomly between zero and an exponentially increasing maximum.
The builder makes it easier to create a fault handler by automatically injecting dependencies. The default strategies that the builder uses can be overridden by using the setters.
$builder = new TransientFaultHandlerBuilder();
$transientFaultHandler = $builder
->setExceptionDetectionStrategy(new StaticDetectionStrategy())
->setReturnValueDetectionStrategy(new FalseyReturnValueDetectionStrategy())
->setRetryStrategy(new ExponentialBackoffStrategy())
->setLogger(new Logger())
->build();
Please see CHANGELOG for more information what has changed recently.
make test
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.