backoff-rxjs
Version:
A collection of helpful RxJS operators to deal with backoff strategies (like exponential backoff)
21 lines (20 loc) • 1.3 kB
JavaScript
import { defer, iif, throwError, timer, concatMap, retryWhen, tap } from 'rxjs';
import { exponentialBackoffDelay, getDelay } from '../utils';
export function retryBackoff(config) {
var _a = typeof config === 'number' ? { initialInterval: config } : config, initialInterval = _a.initialInterval, _b = _a.maxRetries, maxRetries = _b === void 0 ? Infinity : _b, _c = _a.maxInterval, maxInterval = _c === void 0 ? Infinity : _c, _d = _a.shouldRetry, shouldRetry = _d === void 0 ? function () { return true; } : _d, _e = _a.resetOnSuccess, resetOnSuccess = _e === void 0 ? false : _e, _f = _a.backoffDelay, backoffDelay = _f === void 0 ? exponentialBackoffDelay : _f;
return function (source) {
return defer(function () {
var index = 0;
return source.pipe(retryWhen(function (errors) {
return errors.pipe(concatMap(function (error) {
var attempt = index++;
return iif(function () { return attempt < maxRetries && shouldRetry(error); }, timer(getDelay(backoffDelay(attempt, initialInterval), maxInterval)), throwError(error));
}));
}), tap(function () {
if (resetOnSuccess) {
index = 0;
}
}));
});
};
}