export function getExponentialBackoffDuration()

in src/common/backoffUtils.ts [21:37]


export function getExponentialBackoffDuration(failedAttempts: number): number {
    if (failedAttempts <= 1) {
        return MIN_BACKOFF_DURATION;
    }

    // exponential: minBackoff * 2 ^ (failedAttempts - 1)
    // The right shift operator is not used in order to avoid potential overflow. Bitwise operations in JavaScript are limited to 32 bits.
    let calculatedBackoffDuration = MIN_BACKOFF_DURATION * Math.pow(2, failedAttempts - 1);
    if (calculatedBackoffDuration > MAX_BACKOFF_DURATION) {
        calculatedBackoffDuration = MAX_BACKOFF_DURATION;
    }

    // jitter: random value between [-1, 1) * jitterRatio * calculatedBackoffMs
    const jitter = JITTER_RATIO * (Math.random() * 2 - 1);

    return calculatedBackoffDuration * (1 + jitter);
}