-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfailover.ts
More file actions
39 lines (33 loc) · 1.68 KB
/
failover.ts
File metadata and controls
39 lines (33 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
const MIN_BACKOFF_DURATION = 30_000; // 30 seconds in milliseconds
const MAX_BACKOFF_DURATION = 10 * 60 * 1000; // 10 minutes in milliseconds
const MAX_SAFE_EXPONENTIAL = 30; // Used to avoid overflow. bitwise operations in JavaScript are limited to 32 bits. It overflows at 2^31 - 1.
const JITTER_RATIO = 0.25;
// Reference: https://github.com/Azure/AppConfiguration-DotnetProvider/blob/main/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/Extensions/TimeSpanExtensions.cs#L14
export function getFixedBackoffDuration(timeElapsed: number): number | undefined {
if (timeElapsed <= 100_000) { // 100 seconds in milliseconds
return 5_000; // 5 seconds in milliseconds
}
if (timeElapsed <= 200_000) { // 200 seconds in milliseconds
return 10_000; // 10 seconds in milliseconds
}
if (timeElapsed <= 10 * 60 * 1000) { // 10 minutes in milliseconds
return MIN_BACKOFF_DURATION;
}
return undefined;
}
export function calculateDynamicBackoffDuration(failedAttempts: number) {
if (failedAttempts <= 1) {
return MIN_BACKOFF_DURATION;
}
// exponential: minBackoff * 2 ^ (failedAttempts - 1)
const exponential = Math.min(failedAttempts - 1, MAX_SAFE_EXPONENTIAL);
let calculatedBackoffDuration = MIN_BACKOFF_DURATION * (1 << exponential);
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);
}