Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
stream_topic_history_util: Add retry mechanism to `get_server_history`.
Prakhar Pratyush43249cb4312d
diff --git a/web/src/stream_topic_history_util.ts b/web/src/stream_topic_history_util.tsindex e9d17fcce56..2272eac3829 100644--- a/web/src/stream_topic_history_util.ts+++ b/web/src/stream_topic_history_util.ts@@ -3,6 +3,7 @@ import {z} from "zod";import * as channel from "./channel.ts";import * as stream_topic_history from "./stream_topic_history.ts";+import * as util from "./util.ts";const stream_topic_history_response_schema = z.object({topics: z.array(@@ -15,21 +16,15 @@ const stream_topic_history_response_schema = z.object({const pending_on_success_callbacks = new Map<number, (() => void)[]>();-export function get_server_history(stream_id: number, on_success: () => void): void {- if (stream_topic_history.has_history_for(stream_id)) {- on_success();- return;- }- if (stream_topic_history.is_request_pending_for(stream_id)) {- const callbacks = pending_on_success_callbacks.get(stream_id) ?? [];- callbacks.push(on_success);- pending_on_success_callbacks.set(stream_id, callbacks);+export const MAX_RETRIES = 5;++function fetch_channel_history_with_retry(stream_id: number, attempt = 1): void {+ if (attempt > MAX_RETRIES) {+ pending_on_success_callbacks.delete(stream_id);+ stream_topic_history.remove_request_pending_for(stream_id);return;}- stream_topic_history.add_request_pending_for(stream_id);- pending_on_success_callbacks.set(stream_id, [on_success]);-const url = "/json/users/me/" + stream_id + "/topics";void channel.get({url,@@ -44,15 +39,33 @@ export function get_server_history(stream_id: number, on_success: () => void): v}pending_on_success_callbacks.delete(stream_id);},- error() {- // TODO: Implement some sort of retry logic,- // before giving up on the first failure.- pending_on_success_callbacks.delete(stream_id);- stream_topic_history.remove_request_pending_for(stream_id);+ error(xhr) {+ const retry_delay_secs = util.get_retry_backoff_seconds(xhr, attempt);+ setTimeout(() => {+ fetch_channel_history_with_retry(stream_id, attempt + 1);+ }, retry_delay_secs * 1000);},});}+export function get_server_history(stream_id: number, on_success: () => void): void {+ if (stream_topic_history.has_history_for(stream_id)) {+ on_success();+ return;+ }+ if (stream_topic_history.is_request_pending_for(stream_id)) {+ const callbacks = pending_on_success_callbacks.get(stream_id) ?? [];+ callbacks.push(on_success);+ pending_on_success_callbacks.set(stream_id, callbacks);+ return;+ }++ stream_topic_history.add_request_pending_for(stream_id);+ pending_on_success_callbacks.set(stream_id, [on_success]);++ fetch_channel_history_with_retry(stream_id);+}+export function update_topic_last_message_id(stream_id: number,topic_name: string,diff --git a/web/tests/stream_topic_history.test.cjs b/web/tests/stream_topic_history.test.cjsindex 8d696b1ad5b..1b6b3af2d24 100644--- a/web/tests/stream_topic_history.test.cjs+++ b/web/tests/stream_topic_history.test.cjs@@ -2,7 +2,7 @@const assert = require("node:assert/strict");-const {mock_esm, zrequire} = require("./lib/namespace.cjs");+const {mock_esm, set_global, zrequire} = require("./lib/namespace.cjs");const {run_test, noop} = require("./lib/test.cjs");const channel = mock_esm("../src/channel");@@ -291,7 +291,6 @@ test("server_history_end_to_end", () => {];let get_success_callback;- let get_error_callback;let on_success_called;channel.get = (opts) => {@@ -299,7 +298,6 @@ test("server_history_end_to_end", () => {assert.deepEqual(opts.data, {allow_empty_topic_name: true});assert.ok(stream_topic_history.is_request_pending_for(stream_id));get_success_callback = opts.success;- get_error_callback = opts.error;};stream_topic_history_util.get_server_history(stream_id, noop);@@ -309,9 +307,6 @@ test("server_history_end_to_end", () => {stream_topic_history_util.get_server_history(stream_id, noop);assert.ok(stream_topic_history.is_request_pending_for(stream_id));- get_error_callback();- assert.ok(!stream_topic_history.is_request_pending_for(stream_id));-stream_topic_history_util.get_server_history(stream_id, () => {on_success_called = true;});@@ -345,6 +340,29 @@ test("server_history_end_to_end", () => {assert.ok(on_success_called);});+test("server_history_error", () => {+ set_global("setTimeout", (f) => {+ f();+ });+ stream_topic_history.reset();++ const channel_id = 99;++ let total_attempts = 0;+ channel.get = (opts) => {+ assert.equal(opts.url, "/json/users/me/99/topics");+ assert.ok(stream_topic_history.is_request_pending_for(channel_id));+ // This mocks error on each GET request.+ opts.error();+ total_attempts += 1;+ };++ stream_topic_history_util.get_server_history(channel_id, noop);+ // Verify that we stop after MAX_RETRIES attempt.+ assert.deepEqual(total_attempts, stream_topic_history_util.MAX_RETRIES);+ assert.ok(!stream_topic_history.is_request_pending_for(channel_id));+});+test("ask_server_for_latest_topic_data", () => {stream_topic_history.set_update_topic_last_message_id((stream_id, topic_name) => {stream_topic_history_util.update_topic_last_message_id(stream_id, topic_name, noop);