Skip to content

Commit

Permalink
Add NullBackOff
Browse files Browse the repository at this point in the history
  • Loading branch information
denisyukphp committed Jan 24, 2022
1 parent a446d12 commit 22fd4f9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ The following back-off decorators are available:
- [ExponentialBackOff](../src/ExponentialBackOff.php)
- [DecorrelatedBackOff](../src/DecorrelatedBackOff.php)
- [ImmediatelyThrowableBackOff](../src/ImmediatelyThrowableBackOff.php)
- [NullBackOff](../src/NullBackOff.php)

## Retry exceptions

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

use Orangesoft\BackOff\ExponentialBackOff;
use Orangesoft\BackOff\NullBackOff;
use Orangesoft\BackOff\Duration\Seconds;
use Orangesoft\BackOff\Retry\ExceptionClassifier\ExceptionClassifier;
use Orangesoft\BackOff\Retry\Retry;
Expand All @@ -184,6 +186,18 @@ $exceptionClassifier = new ExceptionClassifier([
$retry = new Retry($backOff, $exceptionClassifier);
```

If you don't need any back-off at all use NullBackOff which just count attempts:

```php
$backOff = new NullBackOff(maxAttempts: 3);

$exceptionClassifier = new ExceptionClassifier([
\RuntimeException::class,
]);

$retry = new Retry($backOff, $exceptionClassifier);
```

Put the business logic in a callback function and call it:

```php
Expand Down
20 changes: 20 additions & 0 deletions src/NullBackOff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Orangesoft\BackOff;

final class NullBackOff implements BackOffInterface
{
public function __construct(
private int|float $maxAttempts = 3,
) {
}

public function backOff(int $attempt, \Throwable $throwable): void
{
if ($attempt > $this->maxAttempts) {
throw $throwable;
}
}
}
22 changes: 22 additions & 0 deletions tests/NullBackOffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Orangesoft\BackOff\Tests;

use Orangesoft\BackOff\NullBackOff;
use PHPUnit\Framework\TestCase;

class NullBackOffTest extends TestCase
{
public function testBackOff(): void
{
$nullBackOff = new NullBackOff(maxAttempts: 3);

$throwable = new \Exception();

$this->expectExceptionObject($throwable);

$nullBackOff->backOff(4, $throwable);
}
}

0 comments on commit 22fd4f9

Please sign in to comment.