@foxglove/ros1
Version:
Standalone TypeScript implementation of the ROS 1 (Robot Operating System) protocol with a pluggable transport layer
32 lines • 1.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.backoffTime = backoffTime;
exports.backoff = backoff;
exports.retryForever = retryForever;
// Implements truncated exponential backoff with jitter. Each retry, the backoff
// time is increased by 2^retries plus a random amount of jitter. The maximum
// time returned by this method is capped at `maxMs`.
function backoffTime(retries, maxMs = 10000, maxJitterMs = 1000, rng = Math.random) {
const randomMs = rng() * maxJitterMs;
return Math.min(Math.pow(2, retries) + randomMs, maxMs);
}
// Wait for a period of time determined by the truncated exponential backoff
// with jitter algorithm implemented in `backoffTime()`.
async function backoff(retries, maxMs = 10000, maxJitterMs = 1000, rng = Math.random) {
await new Promise((resolve) => setTimeout(resolve, backoffTime(retries, maxMs, maxJitterMs, rng)));
}
// Continue to call an async function until it completes without throwing an
// exception
async function retryForever(fn) {
let retries = 0;
for (;;) {
try {
return await fn();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
}
catch (_err) {
await backoff(++retries);
}
}
}
//# sourceMappingURL=backoff.js.map