Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
}
],
"require": {
"php": ">=7.1"
"php": ">=7.2"
},
"require-dev": {
"symfony/phpunit-bridge": "^5.0@dev"
"symfony/phpunit-bridge": "^7.3"
},
"autoload": {
"psr-4": {
Expand Down
28 changes: 24 additions & 4 deletions src/RetryConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
final class RetryConfigurator
{
/**
* @var string[]
* @var class-string<\Throwable>[]
*/
private $retryableExceptionClasses = [];

/**
* @var int
* @var int<0,max>
*/
private $maxRetries;

/**
* @var int
* @var int<0,max>
*/
private $delayInMs = 0;

Expand All @@ -35,6 +35,8 @@ final class RetryConfigurator
* - The callable is retried twice (i.e. max three executions). If it still fails, the last error is rethrown.
* - Retries have a no delay between them.
* - Every \Throwable will trigger the retry logic, i.e. both \Exception and \Error.
*
* @param int<0,max> $maxRetries
*/
public function __construct(int $maxRetries = 2)
{
Expand All @@ -49,6 +51,9 @@ public function __construct(int $maxRetries = 2)
*
* For example, for handling database deadlocks and timeouts with Doctrine, it makes sense to configure `\Doctrine\DBAL\Exception\RetryableException`.
*
* @param class-string<\Throwable> $exceptionClass
* @param class-string<\Throwable> $moreExceptionClasses
*
* @return $this
*/
public function retryOnSpecificExceptions(string $exceptionClass, string ...$moreExceptionClasses): self
Expand All @@ -62,6 +67,8 @@ public function retryOnSpecificExceptions(string $exceptionClass, string ...$mor
/**
* Sets the maximum number of retries.
*
* @param int<0,max> $maxRetries
*
* @return $this
*/
public function maxRetries(int $maxRetries): self
Expand All @@ -76,6 +83,8 @@ public function maxRetries(int $maxRetries): self
*
* Set to zero to disable delay.
*
* @param int<0,max> $milliseconds
*
* @return $this
*/
public function delayInMs(int $milliseconds): self
Expand All @@ -87,6 +96,12 @@ public function delayInMs(int $milliseconds): self

/**
* Returns a callable that decorates the given operation that should be retried on failure.
*
* @template TResult
*
* @param callable():TResult $operation
*
* @return RetryingCallable<TResult>
*/
public function decorate(callable $operation): RetryingCallable
{
Expand All @@ -109,7 +124,12 @@ public function decorate(callable $operation): RetryingCallable
/**
* Executes the passed callable and its arguments with the configured retry behavior.
*
* @return mixed The return value of the passed callable
* @template TResult
*
* @param callable():TResult $operation
* @param mixed $arguments
*
* @return TResult The return value of the passed callable
*/
public function call(callable $operation, ...$arguments)
{
Expand Down
18 changes: 11 additions & 7 deletions src/RetryingCallable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@
*
* @author Tobias Schultze <http://tobion.de>
* @author Christian Riesen <http://christianriesen.com>
*
* @template TResult
*/
final class RetryingCallable
{
/**
* @var callable
* @var callable():TResult
*/
private $operation;

/**
* @var int
* @var int<0,max>
*/
private $retries = 0;

/**
* @var callable
* @var callable(\Throwable):void
*/
private $exceptionHandler;

/**
* Constructor to wrap a callable operation.
*
* @param callable $operation The operation to execute that should be retried on failure
* @param callable $exceptionHandler A callback to execute when an exception is caught. The callback receives the exception
* as parameter and can then decide what to do.
* @param callable():TResult $operation The operation to execute that should be retried on failure
* @param callable(\Throwable):void $exceptionHandler A callback to execute when an exception is caught. The callback receives the exception
* as parameter and can then decide what to do.
*/
public function __construct(callable $operation, callable $exceptionHandler)
{
Expand All @@ -40,6 +42,8 @@ public function __construct(callable $operation, callable $exceptionHandler)

/**
* Returns the number of retries used.
*
* @return int<0,max>
*/
public function getRetries(): int
{
Expand All @@ -51,7 +55,7 @@ public function getRetries(): int
*
* All arguments given will be passed through to the wrapped callable.
*
* @return mixed The return value of the wrapped callable
* @return TResult The return value of the wrapped callable
*
* @throws \Throwable When the exception handler also throws an exception.
*/
Expand Down