nestjs-resilience
Version:
A module for improving the reliability and fault-tolerance of your NestJS applications
46 lines (45 loc) • 1.63 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.RetryStrategy = void 0;
const base_strategy_1 = require("./base.strategy");
const helpers_1 = require("../helpers");
const rxjs_1 = require("rxjs");
class RetryStrategy extends base_strategy_1.Strategy {
get backoff() {
if (this.options.backoff instanceof helpers_1.Backoff) {
return this.options.backoff;
}
return new this.options.backoff();
}
constructor(options = {}) {
super(Object.assign(Object.assign({}, RetryStrategy.DEFAULT_OPTIONS), options));
if (this.options.scaleFactor <= 0) {
throw new RangeError('Scale factor must be greater than 0, got: ' + this.options.scaleFactor);
}
}
process(observable) {
const generator = this.backoff.getGenerator(this.options.maxRetries);
return observable.pipe((0, rxjs_1.retry)({
count: this.options.maxRetries,
delay: error => {
if (!this.options.retryable(error)) {
throw error;
}
const { value, done } = generator.next();
if (done) {
throw error;
}
const delay = value * this.options.scaleFactor;
return (0, rxjs_1.timer)(Math.max(0, Math.min(delay, this.options.maxDelay)));
}
}));
}
}
exports.RetryStrategy = RetryStrategy;
RetryStrategy.DEFAULT_OPTIONS = {
maxRetries: 5,
maxDelay: 30000,
scaleFactor: 1,
backoff: helpers_1.FixedBackoff,
retryable: () => true
};
;