RetryTrait
Trait that provides support for calling a method with configurable retries
Tags
Table of Contents
- $retryOptions : array<string|int, mixed>
- Retry options
- callWithRetry() : mixed
- Call a method and retry the call if an exception is thrown
- getBackoffDuration() : int
- Get the delay before a try
- shouldRetry() : bool
- Check if the call needs to be retried
Properties
$retryOptions
Retry options
protected
array<string|int, mixed>
$retryOptions
= [
'retryCount' => 5,
// number of retries (set to 0 to disable)
'firstBackoff' => 0,
// backoff (delay) before first retry
// (milliseconds)
'subsequentBackoff' => 200,
// backoff (delay) before subsequent retries
// (milliseconds)
'exponentialBackoff' => true,
// whether to use exponential backoff
'maximumBackoff' => 1000,
]
Methods
callWithRetry()
Call a method and retry the call if an exception is thrown
protected
callWithRetry(callable $callback[, callable|null $statusCallback = null ][, array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $callback : callable
-
Method to call
- $statusCallback : callable|null = null
-
Status callback called before retry and after a successful retry. The callback gets the attempt number and either an exception if an error occurred or null if the request succeeded after retries.
- $options : array<string|int, mixed> = []
-
Optional options to override defaults in $this->retryOptions. Options can also include a retryableExceptionCallback for a callback that gets the attempt number and exception as parameters and returns true if the call can be retried or false if not.
Return values
mixed —getBackoffDuration()
Get the delay before a try
protected
getBackoffDuration(int $attempt, array<string|int, mixed> $options) : int
Parameters
- $attempt : int
-
Attempt number
- $options : array<string|int, mixed>
-
Current options
Return values
int —milliseconds
shouldRetry()
Check if the call needs to be retried
protected
shouldRetry(int $attempt, array<string|int, mixed> $options) : bool
Parameters
- $attempt : int
-
Failed attempt number
- $options : array<string|int, mixed>
-
Current options