@somnia-chain/markets-sdk


@somnia-chain/markets-sdk / index / nextBackoffDelayMs

Function: nextBackoffDelayMs()

nextBackoffDelayMs(attempt, random, limits?): number

Defined in: packages/sdk/src/config.ts:393

Delay before retry attempt (0-based): exponential backoff with FULL jitter, random scaled onto [0, base * 2 ** attempt], capped at maxDelayMs.

Details

Full jitter, not a fixed interval and not bare doubling — both leave clients retrying in lockstep, which is how a slow endpoint becomes a storm. The caller supplies the draw (Math.random() in production), so the function is pure: the same inputs give the same delay, and a poll loop outside the SDK can pace itself on the same schedule.

Example (Pacing a poll loop on the SDK schedule)

ts
import { nextBackoffDelayMs } from "@somnia-chain/markets-sdk";

let failures = 0;
for (;;) {
  const ok = await readOnce();
  failures = ok ? 0 : failures + 1;
  await new Promise((r) => setTimeout(r, failures ? nextBackoffDelayMs(failures - 1, Math.random()) : 60_000));
}

Parameters

attempt

number

random

number

limits?

IndexerLimits = {}

Returns

number

Throws

InvalidInputError If attempt is not a finite number >= 0, random is outside [0, 1), or limits.baseDelayMs/limits.maxDelayMs is not a finite number > 0.