adding a max timeout since retry counter can be very high

This commit is contained in:
Sebastian 2022-01-13 01:33:03 -03:00
parent f6ec105b72
commit 4b289cde5d
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1

View File

@ -32,11 +32,13 @@ export interface RetryInfo {
export interface RetryPolicy { export interface RetryPolicy {
readonly backoffDelta: Duration; readonly backoffDelta: Duration;
readonly backoffBase: number; readonly backoffBase: number;
readonly maxTimeout: Duration;
} }
const defaultRetryPolicy: RetryPolicy = { const defaultRetryPolicy: RetryPolicy = {
backoffBase: 1.5, backoffBase: 1.5,
backoffDelta: { d_ms: 200 }, backoffDelta: { d_ms: 200 },
maxTimeout: { d_ms: 6000 },
}; };
export function updateRetryInfoTimeout( export function updateRetryInfoTimeout(
@ -51,8 +53,11 @@ export function updateRetryInfoTimeout(
r.nextRetry = { t_ms: "never" }; r.nextRetry = { t_ms: "never" };
return; return;
} }
const nextIncrement = p.backoffDelta.d_ms * Math.pow(p.backoffBase, r.retryCounter)
const t = const t =
now.t_ms + p.backoffDelta.d_ms * Math.pow(p.backoffBase, r.retryCounter); now.t_ms + (p.maxTimeout.d_ms === "forever" ? nextIncrement : Math.min(p.maxTimeout.d_ms, nextIncrement));
r.nextRetry = { t_ms: t }; r.nextRetry = { t_ms: t };
} }
@ -68,7 +73,7 @@ export function getRetryDuration(
return { d_ms: "forever" }; return { d_ms: "forever" };
} }
const t = p.backoffDelta.d_ms * Math.pow(p.backoffBase, r.retryCounter); const t = p.backoffDelta.d_ms * Math.pow(p.backoffBase, r.retryCounter);
return { d_ms: t }; return { d_ms: p.maxTimeout.d_ms === "forever" ? t : Math.min(p.maxTimeout.d_ms, t) };
} }
export function initRetryInfo( export function initRetryInfo(