Skip to content

Commit 4b8fe26

Browse files
Updated README.md
1 parent d8df981 commit 4b8fe26

18 files changed

+45
-9
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/.editorconfig export-ignore
88
/.gitattributes export-ignore
99
/.gitignore export-ignore
10+
/images export-ignore
1011
/infection.json.dist export-ignore
1112
/phpcs.xml.dist export-ignore
1213
/phpstan.neon.dist export-ignore

README.md

+43-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
[![Buy The World a Tree](https://img.shields.io/badge/treeware-%F0%9F%8C%B3-lightgreen?style=flat-square)](https://plant.treeware.earth/code-distortion/backoff)
77
[![Contributor Covenant](https://img.shields.io/badge/contributor%20covenant-v2.1%20adopted-ff69b4.svg?style=flat-square)](.github/CODE_OF_CONDUCT.md)
88

9+
![Sequence Backoff](docs/backoff-logo.png)
10+
911
***code-distortion/backoff*** is a PHP library that retries your actions when they fail. It implements various backoff strategies and jitter to avoid overwhelming the resource being accessed.
1012

1113
It's useful when you're working with services that might be temporarily unavailable, such as APIs.
@@ -17,7 +19,7 @@ $result = Backoff::exponential(2)->maxAttempts(10)->maxDelay(30)->attempt($actio
1719
```
1820

1921
> See the [cheatsheet](#cheatsheet) for an overview of what's possible.
20-
22+
2123

2224

2325
## Table of Contents
@@ -361,6 +363,8 @@ A number of backoff algorithms have been included to choose from, and you can al
361363

362364
### Fixed Backoff
363365

366+
![Fixed Backoff](docs/algorithms/fixed-backoff.png)
367+
364368
The fixed backoff algorithm waits the *same* amount of time between each attempt.
365369

366370
```php
@@ -376,6 +380,8 @@ Backoff::fixedUs(2)->attempt($action); // in microseconds
376380

377381
### Linear Backoff
378382

383+
![Linear Backoff](docs/algorithms/linear-backoff.png)
384+
379385
The linear backoff algorithm increases the waiting period by a specific amount each time.
380386

381387
If `$delayIncrease` is not passed, it will increase by `$initalDelay` each time.
@@ -396,6 +402,8 @@ Backoff::linearUs(5)->attempt($action); // in microseconds
396402

397403
### Exponential Backoff
398404

405+
![Exponential Backoff](docs/algorithms/exponential-backoff.png)
406+
399407
The exponential backoff algorithm increases the waiting period exponentially.
400408

401409
By default, the delay is doubled each time, but you can change the factor it multiplies by.
@@ -416,6 +424,8 @@ Backoff::exponentialUs(1)->attempt($action); // in microseconds
416424

417425
### Polynomial Backoff
418426

427+
![Polynomial Backoff](docs/algorithms/polynomial-backoff.png)
428+
419429
The polynomial backoff algorithm increases the waiting period in a polynomial manner.
420430

421431
By default, the retry number is raised to the power of 2, but you can change this.
@@ -436,6 +446,8 @@ Backoff::polynomialUs(1)->attempt($action); // in microseconds
436446

437447
### Fibonacci Backoff
438448

449+
![Fibonacci Backoff](docs/algorithms/fibonacci-backoff.png)
450+
439451
The Fibonacci backoff algorithm increases waiting period by following a Fibonacci sequence. This is where each delay is the sum of the previous two delays.
440452

441453
`Logic: $delay = $previousDelay1 + $previousDelay2`
@@ -461,6 +473,8 @@ Backoff::fibonacci(5, false)->attempt($action); // 5, 10, 15, 25, 40, 65, 105, 1
461473

462474
### Decorrelated Backoff
463475

476+
![Decorrelated Backoff](docs/algorithms/decorrelated-backoff.png)
477+
464478
The decorrelated backoff algorithm is a feedback loop where the previous delay is used as input to help to determine the next delay.
465479

466480
A random delay between the `$baseDelay` and the `previous-delay * 3` is picked.
@@ -485,6 +499,8 @@ Backoff::decorrelatedUs(1)->attempt($action); // in microseconds
485499

486500
### Random Backoff
487501

502+
![Random Backoff](docs/algorithms/random-backoff.png)
503+
488504
The random backoff algorithm waits for a random period of time within the range you specify.
489505

490506
Jitter is not applied to this algorithm.
@@ -504,6 +520,8 @@ Backoff::randomUs(2, 5)->attempt($action); // in microseconds
504520

505521
### Sequence Backoff
506522

523+
![Sequence Backoff](docs/algorithms/sequence-backoff.png)
524+
507525
The sequence backoff algorithm lets you specify the particular delays to use.
508526

509527
An optional fixed delay can be used to continue with, after the sequence finishes. Otherwise, the attempts will stop when the sequence has been exhausted.
@@ -528,6 +546,8 @@ Backoff::sequenceUs([1, 1.25, 1.5, 2, 3])->attempt($action); // in microseconds
528546

529547
### Callback Backoff
530548

549+
![Sequence Backoff](docs/algorithms/callback-backoff.png)
550+
531551
The callback backoff algorithm lets you specify a callback that chooses the period to wait.
532552

533553
Your callback is expected to return an `int` or `float` representing the delay, or `null` to indicate that the attempts should stop.
@@ -551,7 +571,9 @@ Backoff::callbackUs($callback)->attempt($action); // in microseconds
551571
552572

553573

554-
### Custom Backoff Algorithm Class
574+
### Custom Backoff Algorithm Class
575+
576+
![Sequence Backoff](docs/algorithms/custom-backoff.png)
555577

556578
As well as the [callback option above](#callback-backoff), you have the ability to create your own backoff algorithm *class* by extending `BaseBackoffAlgorithm` and implementing the `BackoffAlgorithmInterface`.
557579

@@ -600,9 +622,11 @@ Backoff::customUs($algorithm)->attempt($action); // in microseconds
600622

601623
### Noop Backoff
602624

625+
![Noop Backoff](docs/algorithms/noop-backoff.png)
626+
603627
The "no-op" backoff algorithm is a utility algorithm that doesn't wait at all, retries are attempted straight away.
604628

605-
This might be useful for testing purposes. See [Backoff and Test Suites](#working-with-test-suites) for more options when running tests.
629+
This might be useful for testing purposes. See [Working With Test Suites](#working-with-test-suites) for more options when running tests.
606630

607631
```php
608632
Backoff::noop()->attempt($action); // 0, 0, 0, 0, 0…
@@ -614,7 +638,7 @@ Backoff::noop()->attempt($action); // 0, 0, 0, 0, 0…
614638

615639
The "no backoff" algorithm is a utility algorithm that doesn't allow retries at all. Only the first attempt will be made.
616640

617-
This might be useful for testing purposes. See [Backoff and Test Suites](#working-with-test-suites) for more options when running tests.
641+
This might be useful for testing purposes. See [Working With Test Suites](#working-with-test-suites) for more options when running tests.
618642

619643
```php
620644
Backoff::none()->attempt($action); // (no retries)
@@ -640,7 +664,7 @@ Backoff::exponential(1)
640664

641665
#### Max-Delay
642666

643-
You can specify the maximum length each base-delay can be (which is the delay before [jitter](#jitter) is applied). This is useful for preventing the delays from becoming too long.
667+
You can specify the maximum length each *base-delay* can be (which is the delay before [jitter](#jitter) is applied). This will prevent the delays from becoming too large.
644668

645669
> ***Note:*** You'll need to make sure the max-delay you specify matches the unit-of-measure being used.
646670
@@ -673,16 +697,18 @@ Having a backoff algorithm is a good start but probably isn't enough to prevent
673697

674698
Jitter is used to mitigate this by making random adjustments to the Backoff Algorithm's delays.
675699

676-
For example, if the backoff algorithm generates a delay of 100ms, jitter adjusts this to be somewhere between say, 75ms and 125ms. The actual range is determined by the type of jitter used.
700+
For example, if the backoff algorithm generates a delay of 100ms, jitter could randomly adjust this to be somewhere between say, 75ms and 125ms. The actual range depends on the type of jitter used.
677701

678-
> This library applies [Full Jitter](#full-jitter) by default. You can use [No Jitter](#no-jitter) if you'd like to turn it off.
702+
This library applies [Full Jitter](#full-jitter) by default which is likely to be a good choice. You can turn jitter off altogether by using [No Jitter](#no-jitter).
679703

680704
> The article [Exponential Backoff And Jitter](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/) by Marc Brooker at AWS does a good job of explaining what jitter is, and the reason for its use.
681705
682706

683707

684708
#### Full Jitter
685709

710+
![Noop Backoff](docs/jitter/full-jitter.png)
711+
686712
Full Jitter applies a random adjustment to the delay, within the range of 0 and the *full delay*. That is, between 0% and 100% of the base-delay.
687713

688714
> ***Note:*** This is the type of jitter that is used by default.
@@ -699,6 +725,8 @@ Backoff::exponential(1)
699725

700726
#### Equal Jitter
701727

728+
![Noop Backoff](docs/jitter/equal-jitter.png)
729+
702730
Equal Jitter applies a random adjustment to the delay, within the range of *half* and the *full delay*. That is, between 50% and 100% of the base-delay.
703731

704732
`$delay = rand($delay / 2, $delay)`
@@ -713,20 +741,24 @@ Backoff::exponential(1)
713741

714742
#### Custom Jitter Range
715743

744+
![Noop Backoff](docs/jitter/custom-jitter.png)
745+
716746
If you'd like a different range compared to *full* and *equal* jitter above, jitter-range lets you specify your own custom range.
717747

718748
`$delay = rand($delay * $min, $delay * $max)`
719749

720750
```php
721751
Backoff::exponential(1)
722-
->jitterRange(0.5, 1.5) // <<< between 50% and 150%
752+
->jitterRange(0.75, 1.25) // <<< between 75% and 125%
723753
->attempt($action);
724754
```
725755

726756

727757

728758
#### Jitter Callback
729759

760+
![Noop Backoff](docs/jitter/custom-jitter.png)
761+
730762
Jitter callback lets you specify a callback that applies jitter to the base-delay.
731763

732764
Your callback is expected to return an `int` or `float` representing the updated delay.
@@ -747,6 +779,8 @@ Backoff::exponential(1)
747779

748780
#### Custom Jitter Class
749781

782+
![Noop Backoff](docs/jitter/custom-jitter.png)
783+
750784
As well as customising jitter using the [range](#custom-jitter-range) and [callback](#jitter-callback) options above, you have the ability to create your own Jitter *class* by extending `BaseJitter` and implementing the `JitterInterface`.
751785

752786
```php
@@ -1214,7 +1248,7 @@ This means that you won't be able to use Backoff's functionality to:
12141248

12151249
If your aim is to do one of the following, you could use one of the already available options:
12161250

1217-
> - [Catch and retry exceptions](#managing-exceptions) - If you'd like Backoff to catch *particular* exceptions, you can use [->retryExceptions(…)](#managing-exceptions). This lets you specify which exceptions to retry, or specify a callback to make the decision.
1251+
> - [Catch and retry exceptions](#managing-exceptions) - If you'd like Backoff to catch *particular* exceptions, you can use [->retryExceptions(…)](#retry-when-particular-exceptions-occur). This lets you specify which exceptions to retry, or specify a callback to make the decision.
12181252
> - [Retry based on return values](#managing-invalid-return-values) - If you'd like to selectively retry based on particular *return values*, you can use [->retryWhen(…)](#retry-when) or [->retryUntil(…)](#retry-until). These let you specify values to check for, or specify a callback to make the decision.
12191253
> - [Trigger callbacks](#callbacks) - If you'd like to perform tasks in certain situations (like logging before each retry delay), you could consider using the callback options such as [->exceptionCallback(…)](#exception-callback) or [->invalidResultCallback(…)](#invalid-result-callback).
12201254

docs/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source

docs/algorithms/callback-backoff.png

35.5 KB
Loading

docs/algorithms/custom-backoff.png

34.9 KB
Loading
41.5 KB
Loading
24.7 KB
Loading

docs/algorithms/fibonacci-backoff.png

27.6 KB
Loading

docs/algorithms/fixed-backoff.png

26.2 KB
Loading

docs/algorithms/linear-backoff.png

32 KB
Loading

docs/algorithms/noop-backoff.png

24.2 KB
Loading
33.4 KB
Loading

docs/algorithms/random-backoff.png

41.3 KB
Loading

docs/algorithms/sequence-backoff.png

44 KB
Loading

docs/backoff-logo.png

144 KB
Loading

docs/jitter/custom-jitter.png

10.2 KB
Loading

docs/jitter/equal-jitter.png

11.6 KB
Loading

docs/jitter/full-jitter.png

11.6 KB
Loading

0 commit comments

Comments
 (0)