You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+72-22Lines changed: 72 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,33 +20,36 @@ This package requires PHP 8.1 or later.
20
20
21
21
## Quick usage
22
22
23
-
Configure BackOffand ExceptionClassifier to retry your business logic when an exception will be thrown:
23
+
Configure `Orangesoft\BackOff\Retry\BackOffRetry::class`, any of back-off classes, and `Orangesoft\BackOff\Retry\ExceptionClassifier\ExceptionClassifier::class` to retry a business logic when an exception is thrown:
24
24
25
25
```php
26
26
<?php
27
27
28
28
use Orangesoft\BackOff\ExponentialBackOff;
29
-
use Orangesoft\BackOff\Duration\Seconds;
29
+
use Orangesoft\BackOff\Duration\Microseconds;
30
30
use Orangesoft\BackOff\Retry\ExceptionClassifier\ExceptionClassifier;
31
-
use Orangesoft\BackOff\Retry\Retry;
31
+
use Orangesoft\BackOff\Retry\BackOffRetry;
32
32
33
-
$backOff = new ExponentialBackOff(
33
+
$backOffRetry = new BackOffRetry(
34
34
maxAttempts: 3,
35
-
baseTime: new Seconds(1),
36
-
capTime: new Seconds(60),
35
+
baseTime: new Microseconds(1_000),
36
+
capTime: new Microseconds(10_000),
37
+
backOff: new ExponentialBackOff(
38
+
multiplier: 2.0,
39
+
),
40
+
exceptionClassifier: new ExceptionClassifier(
41
+
classNames: [
42
+
\Exception::class,
43
+
],
44
+
),
37
45
);
38
-
39
-
$exceptionClassifier = new ExceptionClassifier([
40
-
\RuntimeException::class,
41
-
]);
42
-
43
-
$retry = new Retry($backOff, $exceptionClassifier);
44
46
```
45
47
46
-
Put the business logic in a callback function and call it:
48
+
Use the `Orangesoft\BackOff\Retry\BackOffRetry::call(callable $callback): mixed` method to wrap the business logic and call it with retry functionality:
47
49
48
50
```php
49
-
$retry->call(function (): int {
51
+
/** @var int $result */
52
+
$result = $backOffRetry->call(static function (): int {
50
53
$random = mt_rand(5, 10);
51
54
52
55
if (0 === $random % 2) {
@@ -57,14 +60,61 @@ $retry->call(function (): int {
57
60
});
58
61
```
59
62
60
-
After the exception is thrown call will be retried with a back-off time until max attempts has been reached.
Pass the implementation of `Orangesoft\BackOff\Jitter\JitterInterface::class` to the back-off class and jitter will be enabled:
75
+
76
+
```php
77
+
<?php
78
+
79
+
use Orangesoft\BackOff\ExponentialBackOff;
80
+
use Orangesoft\BackOff\Duration\Microseconds;
81
+
use Orangesoft\BackOff\Jitter\EqualJitter;
82
+
83
+
$exponentialBackOff = new ExponentialBackOff(
84
+
multiplier: 2.0,
85
+
jitter: new EqualJitter(),
86
+
);
87
+
88
+
$exponentialBackOff->backOff(
89
+
attempt: 1,
90
+
baseTime: new Microseconds(1_000),
91
+
capTime: new Microseconds(512_000),
92
+
);
93
+
```
94
+
95
+
Below you can see the time intervals in microseconds for exponential back-off with a multiplier of 2.0 and equal jitter, where the base time is 1000 μs and the cap time is 512000 μs:
0 commit comments