Skip to content

Commit ec6e553

Browse files
committed
document custom decider
1 parent e3eeba6 commit ec6e553

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ $result = backoff(function() {
3030

3131
If successful `$result` will contain the result of the closure. If max attempts are exceeded the inner exception is re-thrown.
3232

33-
You can of course provide other options via the helper method if needed.
33+
You can of course provide other options via the helper method if needed.
3434

3535
Method parameters are `$callback`, `$maxAttempts`, `$strategy`, `$waitCap`, `$useJitter`.
3636

3737
## Backoff class usage
38-
38+
3939
The Backoff class constructor parameters are `$maxAttempts`, `$strategy`, `$waitCap`, `$useJitter`.
4040

4141
```
@@ -156,7 +156,7 @@ Finally, you can pass in a closure as the strategy if you wish. This closure sho
156156
backoff(function() {
157157
...
158158
}, 10, function($attempt) {
159-
return (100 * $attempt) + 5000;
159+
return (100 * $attempt) + 5000;
160160
});
161161
162162
// OR
@@ -176,11 +176,25 @@ This cap can be provided as the fourth argument to the `backoff` helper function
176176
## Jitter
177177

178178
If you have a lot of clients starting a job at the same time and encountering failures, any of the above backoff strategies could mean the workers continue to collide at each retry.
179-
179+
180180
The solution for this is to add randomness. See here for a good explanation:
181181

182182
https://www.awsarchitectureblog.com/2015/03/backoff.html
183183

184184
You can enable jitter by passing `true` in as the fifth argument to the `backoff` helper function, or by using the `enableJitter()` method on the Backoff class.
185-
185+
186186
We use the "FullJitter" approach outlined in the above article, where a random number between 0 and the sleep time provided by your selected strategy is used.
187+
188+
## Custom retry decider
189+
190+
By default Backoff will retry if an exception is encountered, and if it has not yet hit max retries.
191+
192+
You may provide your own retry decider for more advanced use cases. Perhaps you want to retry based on time rather than number of retries, or perhaps there are scenarios where you would want retry even when an exception was not encountered.
193+
194+
Provide the decider as a callback, or an instance of a class with an `__invoke` method. Backoff will hand it four parameters: the current attempt, max attempts, the last result received, and the exception if one was encountered. Your decider needs to return true or false.
195+
196+
```php
197+
$backoff->setDecider(function($attempt, $maxAttempts, $result, $exception = null) {
198+
return someCustomLogic();
199+
})
200+
```

0 commit comments

Comments
 (0)