Skip to content

Commit 22fd4f9

Browse files
author
Aleksandr Denisyuk
committed
Add NullBackOff
1 parent a446d12 commit 22fd4f9

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

docs/index.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ The following back-off decorators are available:
159159
- [ExponentialBackOff](../src/ExponentialBackOff.php)
160160
- [DecorrelatedBackOff](../src/DecorrelatedBackOff.php)
161161
- [ImmediatelyThrowableBackOff](../src/ImmediatelyThrowableBackOff.php)
162+
- [NullBackOff](../src/NullBackOff.php)
162163

163164
## Retry exceptions
164165

@@ -168,6 +169,7 @@ Configure BackOff and ExceptionClassifier to retry your business logic when an e
168169
<?php
169170

170171
use Orangesoft\BackOff\ExponentialBackOff;
172+
use Orangesoft\BackOff\NullBackOff;
171173
use Orangesoft\BackOff\Duration\Seconds;
172174
use Orangesoft\BackOff\Retry\ExceptionClassifier\ExceptionClassifier;
173175
use Orangesoft\BackOff\Retry\Retry;
@@ -184,6 +186,18 @@ $exceptionClassifier = new ExceptionClassifier([
184186
$retry = new Retry($backOff, $exceptionClassifier);
185187
```
186188

189+
If you don't need any back-off at all use NullBackOff which just count attempts:
190+
191+
```php
192+
$backOff = new NullBackOff(maxAttempts: 3);
193+
194+
$exceptionClassifier = new ExceptionClassifier([
195+
\RuntimeException::class,
196+
]);
197+
198+
$retry = new Retry($backOff, $exceptionClassifier);
199+
```
200+
187201
Put the business logic in a callback function and call it:
188202

189203
```php

src/NullBackOff.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Orangesoft\BackOff;
6+
7+
final class NullBackOff implements BackOffInterface
8+
{
9+
public function __construct(
10+
private int|float $maxAttempts = 3,
11+
) {
12+
}
13+
14+
public function backOff(int $attempt, \Throwable $throwable): void
15+
{
16+
if ($attempt > $this->maxAttempts) {
17+
throw $throwable;
18+
}
19+
}
20+
}

tests/NullBackOffTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Orangesoft\BackOff\Tests;
6+
7+
use Orangesoft\BackOff\NullBackOff;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class NullBackOffTest extends TestCase
11+
{
12+
public function testBackOff(): void
13+
{
14+
$nullBackOff = new NullBackOff(maxAttempts: 3);
15+
16+
$throwable = new \Exception();
17+
18+
$this->expectExceptionObject($throwable);
19+
20+
$nullBackOff->backOff(4, $throwable);
21+
}
22+
}

0 commit comments

Comments
 (0)