backoff

Exponential backoff for retrying something that failed: how long to wait before the next attempt, and whether a failure is still waiting that long. Pure: callers pass their own clock in, so the same failure state works on any thread and in a test.

Methods

(static) afterFailure(previous, nowMs) → {object}

The failure state after one more failure.

Parameters:
NameTypeDescription
previousdynamic

the state afterFailure returned for the last failure, or invalid for the first

nowMsinteger

the caller's clock, in milliseconds

Returns:

an AA with attempts and retryAt, the time the next attempt may start on the same clock

Type: 
object

(static) isWaiting(failure, nowMs) → {boolean}

Whether failure is still waiting out its delay at nowMs. False for invalid: nothing failed.

Parameters:
NameTypeDescription
failuredynamic
nowMsinteger
Returns:
Type: 
boolean

(static) nextDelayMs(attempt) → {integer}

The delay before the retry that follows a 0-based attempt: 1 s, 2 s, 4 s, 8 s, 16 s, then capped at 30 s.

Parameters:
NameTypeDescription
attemptinteger
Returns:
Type: 
integer