Skip to content

graze/transient-fault-handler

Repository files navigation

Transient Fault Handler

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Retries tasks that fail due to transient errors. Well suited to network requests but can retry any callable.

Install

Via Composer

composer require graze/transient-fault-handler

Usage

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);

Detection Strategy

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.

Retry Strategy

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.

Builder

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();

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

make test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Retry tasks that fail due to transient errors

Resources

License

Stars

Watchers

Forks

Packages

No packages published