Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
denisyukphp committed Jul 1, 2024
1 parent 2558ebd commit ba3a517
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,37 @@ This package requires PHP 8.1 or later.

## Quick usage

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:
Configure `Orangesoft\BackOff\Retry\Retry::class`, any of back-off classes, and `Orangesoft\BackOff\Retry\ExceptionClassifier\ExceptionClassifier::class` to retry a business logic when an exception is thrown:

```php
<?php

use Orangesoft\BackOff\ExponentialBackOff;
use Orangesoft\BackOff\Duration\Microseconds;
use Orangesoft\BackOff\Retry\ExceptionClassifier\ExceptionClassifier;
use Orangesoft\BackOff\Retry\BackOffRetry;
use Orangesoft\BackOff\Retry\Retry;

$backOffRetry = new BackOffRetry(
$retry = new Retry(
maxAttempts: 3,
baseTime: new Microseconds(1_000),
capTime: new Microseconds(10_000),
backOff: new ExponentialBackOff(
multiplier: 2.0,
baseTime: new Microseconds(1_000),
capTime: new Microseconds(512_000),
factor: 2.0,
),
exceptionClassifier: new ExceptionClassifier(
classNames: [
\Exception::class,
\RuntimeException::class,
],
),
);
```

Use the `Orangesoft\BackOff\Retry\BackOffRetry::call(callable $callback): mixed` method to wrap the business logic and call it with retry functionality:
Use the `Orangesoft\BackOff\Retry\Retry::call(callable $callback): mixed` method to wrap the business logic and call it with retry functionality:

```php
/** @var int $result */
$result = $backOffRetry->call(static function (): int {
$random = mt_rand(5, 10);
$result = $retry->call(static function (): int {
$random = mt_rand(0, 1);

if (0 === $random % 2) {
throw new \RuntimeException();
Expand All @@ -63,11 +63,11 @@ $result = $backOffRetry->call(static function (): int {
The following back-off strategies are available:

- [Orangesoft\BackOff\CallbackBackOff](./src/CallbackBackOff.php)
- [Orangesoft\BackOff\ConstantBackOff](./src/ConstantBackOff.php)
- [Orangesoft\BackOff\DecorrelatedJitterBackOff](./src/DecorrelatedJitterBackOff.php)
- [Orangesoft\BackOff\ExponentialBackOff](./src/ExponentialBackOff.php)
- [Orangesoft\BackOff\FibonacciBackOff](./src/FibonacciBackOff.php)
- [Orangesoft\BackOff\LinearBackOff](./src/LinearBackOff.php)
- [Orangesoft\BackOff\PermanentBackOff](./src/PermanentBackOff.php)

## Enable Jitter

Expand All @@ -80,19 +80,21 @@ use Orangesoft\BackOff\ExponentialBackOff;
use Orangesoft\BackOff\Duration\Microseconds;
use Orangesoft\BackOff\Jitter\EqualJitter;

$exponentialBackOff = new ExponentialBackOff(
multiplier: 2.0,
jitter: new EqualJitter(),
);

$exponentialBackOff->backOff(
attempt: 1,
$backOff = new ExponentialBackOff(
baseTime: new Microseconds(1_000),
capTime: new Microseconds(512_000),
factor: 2.0,
jitter: new EqualJitter(),
);

for ($i = 1; $i <= 10; $i++) {
$backOff->backOff(
attempt: $i,
);
}
```

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:
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 `1_000` μs and the cap time is `512_000` μs:

```text
+---------+---------------------------+--------------------+
Expand Down

0 comments on commit ba3a517

Please sign in to comment.