Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
retry_backoff: Extract get_retry_backoff_seconds to a leaf module.
Aman Agrawal35cc47055426
diff --git a/web/src/retry_backoff.ts b/web/src/retry_backoff.tsnew file mode 100644index 00000000000..3290eb2f7bb--- /dev/null+++ b/web/src/retry_backoff.ts@@ -0,0 +1,41 @@+import * as z from "zod/mini";++export let get_retry_backoff_seconds = (+ xhr: JQuery.jqXHR<unknown> | undefined,+ attempts: number,+ tighter_backoff = false,+): number => {+ // We need to respect the server's rate-limiting headers, but beyond+ // that, we also want to avoid contributing to a thundering herd if+ // the server is giving us 500/502 responses.+ //+ // We do the maximum of the retry-after header and an exponential+ // backoff.+ let backoff_scale: number;+ if (tighter_backoff) {+ // Starts at 1-2s and ends at 16-32s after enough failures.+ backoff_scale = Math.min(2 ** attempts, 32);+ } else {+ // Starts at 1-2s and ends at 45-90s after enough failures.+ backoff_scale = Math.min(2 ** ((attempts + 1) / 2), 90);+ }+ // Add a bit jitter to backoff scale.+ const backoff_delay_secs = ((1 + Math.random()) / 2) * backoff_scale;+ let rate_limit_delay_secs = 0;+ const rate_limited_error_schema = z.object({+ "retry-after": z.number(),+ code: z.literal("RATE_LIMIT_HIT"),+ });+ const parsed = rate_limited_error_schema.safeParse(xhr?.responseJSON);+ if (xhr?.status === 429 && parsed?.success && parsed?.data) {+ // Add a bit of jitter to the required delay suggested by the+ // server, because we may be racing with other copies of the web+ // app.+ rate_limit_delay_secs = parsed.data["retry-after"] + Math.random() * 0.5;+ }+ return Math.max(backoff_delay_secs, rate_limit_delay_secs);+};++export function rewire_get_retry_backoff_seconds(value: typeof get_retry_backoff_seconds): void {+ get_retry_backoff_seconds = value;+}diff --git a/web/src/util.ts b/web/src/util.tsindex 1ca7e1ebe7b..2b253a08caa 100644--- a/web/src/util.ts+++ b/web/src/util.ts@@ -1,5 +1,4 @@import _ from "lodash";-import * as z from "zod/mini";import * as blueslip from "./blueslip.ts";import {$t} from "./i18n.ts";@@ -547,45 +546,7 @@ export function is_topic_name_considered_empty(topic: string): boolean {return false;}-export let get_retry_backoff_seconds = (- xhr: JQuery.jqXHR<unknown> | undefined,- attempts: number,- tighter_backoff = false,-): number => {- // We need to respect the server's rate-limiting headers, but beyond- // that, we also want to avoid contributing to a thundering herd if- // the server is giving us 500/502 responses.- //- // We do the maximum of the retry-after header and an exponential- // backoff.- let backoff_scale: number;- if (tighter_backoff) {- // Starts at 1-2s and ends at 16-32s after enough failures.- backoff_scale = Math.min(2 ** attempts, 32);- } else {- // Starts at 1-2s and ends at 45-90s after enough failures.- backoff_scale = Math.min(2 ** ((attempts + 1) / 2), 90);- }- // Add a bit jitter to backoff scale.- const backoff_delay_secs = ((1 + Math.random()) / 2) * backoff_scale;- let rate_limit_delay_secs = 0;- const rate_limited_error_schema = z.object({- "retry-after": z.number(),- code: z.literal("RATE_LIMIT_HIT"),- });- const parsed = rate_limited_error_schema.safeParse(xhr?.responseJSON);- if (xhr?.status === 429 && parsed?.success && parsed?.data) {- // Add a bit of jitter to the required delay suggested by the- // server, because we may be racing with other copies of the web- // app.- rate_limit_delay_secs = parsed.data["retry-after"] + Math.random() * 0.5;- }- return Math.max(backoff_delay_secs, rate_limit_delay_secs);-};--export function rewire_get_retry_backoff_seconds(value: typeof get_retry_backoff_seconds): void {- get_retry_backoff_seconds = value;-}+export {get_retry_backoff_seconds, rewire_get_retry_backoff_seconds} from "./retry_backoff.ts";export async function sha256_hash(text: string): Promise<string | undefined> {// The Web Crypto API is only available in secure contexts (HTTPS or localhost).