Backoff generators usable as simple iterables
npm install iterable-backoff> Backoff generators usable as simple iterables
Installation of the npm package:
``
> yarn add iterable-backoff
> npm install --save iterable-backoff
`
`js
import { fibonacci } from "iterable-backoff";
async function fetch(url) {
for (const delay of fibonacci()
.toMs()
.take(5)) {
try {
return await got(url); // or any promise-returning HTTP lib
} catch (error) {
console.warn(error);
await Bluebird.delay(delay); // or any promise-returning timer
}
}
throw new Error("too many tries");
}
`
#### linear(slope = 1, intersect = 1)
#### power(power = 2)
#### fibonacci()
#### exponential(base = 2)
#### addNoise(factor = 0.1)
Add a noise to the sequence, proportional to the value (default is
10%).
Particularly useful when the backoff is used to wait access for a
shared resource and you don't want multiple consumer retrying at the
same time.
`js`
for (const delay of power().addNoise()) {
// ...
}
#### clamp(min, max)
Clamps the value within inclusive min and max bounds.
`js`
for (const delay of exponential().clamp(null, 10)) {
// ...
}
#### map(fn)
Applies a custom function to each value of the sequence.
Clamps the value within inclusive min and max bounds.
`js`
for (const delay of fibonacci().map(x => x / 2)) {
// ...
}
#### take(n)
Limits the sequence to at most n values.
You usually want to use this if you do not want to keep retrying for
ever.
`js`
for (const delay of power().take(10)) {
// ...
}
#### toMs()
Converts the sequence's values to milliseconds (from seconds).
`js`
for (const delay of exponential(3)
.take(10)
.toMs()) {
// ...
}
`Install dependencies
> npm ci
Contributions are _very_ welcomed, either on the documentation or on
the code.
You may:
- report any issue
you've encountered;
- fork and create a pull request.
ISC © Julien Fontanet