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
+43-9
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,8 @@
6
6
[](https://plant.treeware.earth/code-distortion/backoff)
***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.
10
12
11
13
It's useful when you're working with services that might be temporarily unavailable, such as APIs.
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.
The sequence backoff algorithm lets you specify the particular delays to use.
508
526
509
527
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.
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`.
557
579
@@ -600,9 +622,11 @@ Backoff::customUs($algorithm)->attempt($action); // in microseconds
600
622
601
623
### Noop Backoff
602
624
625
+

626
+
603
627
The "no-op" backoff algorithm is a utility algorithm that doesn't wait at all, retries are attempted straight away.
604
628
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.
The "no backoff" algorithm is a utility algorithm that doesn't allow retries at all. Only the first attempt will be made.
616
640
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.
618
642
619
643
```php
620
644
Backoff::none()->attempt($action); // (no retries)
@@ -640,7 +664,7 @@ Backoff::exponential(1)
640
664
641
665
#### Max-Delay
642
666
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.
644
668
645
669
> ***Note:*** You'll need to make sure the max-delay you specify matches the unit-of-measure being used.
646
670
@@ -673,16 +697,18 @@ Having a backoff algorithm is a good start but probably isn't enough to prevent
673
697
674
698
Jitter is used to mitigate this by making random adjustments to the Backoff Algorithm's delays.
675
699
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.
677
701
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).
679
703
680
704
> 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.
681
705
682
706
683
707
684
708
#### Full Jitter
685
709
710
+

711
+
686
712
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.
687
713
688
714
> ***Note:*** This is the type of jitter that is used by default.
@@ -699,6 +725,8 @@ Backoff::exponential(1)
699
725
700
726
#### Equal Jitter
701
727
728
+

729
+
702
730
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.
703
731
704
732
`$delay = rand($delay / 2, $delay)`
@@ -713,20 +741,24 @@ Backoff::exponential(1)
713
741
714
742
#### Custom Jitter Range
715
743
744
+

745
+
716
746
If you'd like a different range compared to *full* and *equal* jitter above, jitter-range lets you specify your own custom range.
717
747
718
748
`$delay = rand($delay * $min, $delay * $max)`
719
749
720
750
```php
721
751
Backoff::exponential(1)
722
-
->jitterRange(0.5, 1.5) // <<< between 50% and 150%
752
+
->jitterRange(0.75, 1.25) // <<< between 75% and 125%
723
753
->attempt($action);
724
754
```
725
755
726
756
727
757
728
758
#### Jitter Callback
729
759
760
+

761
+
730
762
Jitter callback lets you specify a callback that applies jitter to the base-delay.
731
763
732
764
Your callback is expected to return an `int` or `float` representing the updated delay.
@@ -747,6 +779,8 @@ Backoff::exponential(1)
747
779
748
780
#### Custom Jitter Class
749
781
782
+

783
+
750
784
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`.
751
785
752
786
```php
@@ -1214,7 +1248,7 @@ This means that you won't be able to use Backoff's functionality to:
1214
1248
1215
1249
If your aim is to do one of the following, you could use one of the already available options:
1216
1250
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.
1218
1252
> -[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.
1219
1253
> -[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).
0 commit comments