UNPKG

@foxglove/ros1

Version:

Standalone TypeScript implementation of the ROS 1 (Robot Operating System) protocol with a pluggable transport layer

33 lines 1.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.retryForever = exports.backoff = exports.backoffTime = void 0; // 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); } exports.backoffTime = backoffTime; // 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) { return await new Promise((resolve) => setTimeout(resolve, backoffTime(retries, maxMs, maxJitterMs, rng))); } exports.backoff = backoff; // Continue to call an async function until it completes without throwing an // exception async function retryForever(fn) { let retries = 0; // eslint-disable-next-line no-constant-condition while (true) { try { return await fn(); } catch (err) { await backoff(++retries); } } } exports.retryForever = retryForever; //# sourceMappingURL=backoff.js.map