kafka-ts
Version:
**KafkaTS** is a Apache Kafka client library for Node.js. It provides both a low-level API for communicating directly with the Apache Kafka cluster and high-level APIs for publishing and subscribing to Kafka topics.
26 lines (25 loc) • 878 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.exponentialBackoff = exports.withRetry = void 0;
const delay_1 = require("./delay");
const logger_1 = require("./logger");
const withRetry = (handleError, maxRetries = 15) => async (func) => {
let lastError;
for (let i = 0; i < maxRetries; i++) {
try {
return await func();
}
catch (error) {
await handleError(error, i + 1);
lastError = error;
}
}
logger_1.log.warn('Retries exhausted', { lastError });
throw lastError;
};
exports.withRetry = withRetry;
const exponentialBackoff = (initialDelayMs, maxDelayMs = 5_000) => async (_, retry) => {
const delayMs = Math.min(maxDelayMs, initialDelayMs * 2 ** retry);
await (0, delay_1.delay)(delayMs);
};
exports.exponentialBackoff = exponentialBackoff;