@essentialai/cogent-bridge
Version:
Cogent Bridge — cross-agent comms for Claude Code, OpenAI Codex and Slack. Codex users: install with `curl -fsSL https://cogent.tools/install.sh | sh`, then start with `cogent-codex` for real-time peer wake.
32 lines • 1.01 kB
JavaScript
/**
* Exponential backoff utility for reconnection delays.
* Produces delays from baseMs (default 1s) doubling up to maxMs (default 30s)
* with configurable jitter to prevent thundering herd.
*/
export class ExponentialBackoff {
baseMs;
maxMs;
jitterFactor;
attempt = 0;
constructor(baseMs = 1000, maxMs = 30_000, jitterFactor = 0.1) {
this.baseMs = baseMs;
this.maxMs = maxMs;
this.jitterFactor = jitterFactor;
}
/** Get the next delay in ms and increment attempt counter. */
next() {
const expDelay = Math.min(this.baseMs * 2 ** this.attempt, this.maxMs);
const jitter = expDelay * this.jitterFactor * (Math.random() * 2 - 1);
this.attempt++;
return Math.max(0, expDelay + jitter);
}
/** Reset after successful connection. */
reset() {
this.attempt = 0;
}
/** Current attempt number (0-based). */
get attempts() {
return this.attempt;
}
}
//# sourceMappingURL=backoff.js.map