Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

server_history: Prevent concurrent requests for the same stream_id.

Prakhar Pratyush5b7014e7d685
diff --git a/web/src/stream_topic_history.js b/web/src/stream_topic_history.js
index 2340b99b365..6c408369f95 100644
--- a/web/src/stream_topic_history.js
+++ b/web/src/stream_topic_history.js
@@ -6,6 +6,7 @@ import * as unread from "./unread";
const stream_dict = new Map(); // stream_id -> PerStreamHistory object
const fetched_stream_ids = new Set();
+const request_pending_stream_ids = new Set();
export function all_topics_in_cache(sub) {
// Checks whether this browser's cache of contiguous messages
@@ -312,4 +313,17 @@ export function reset() {
// This is only used by tests.
stream_dict.clear();
fetched_stream_ids.clear();
+ request_pending_stream_ids.clear();
+}
+
+export function is_request_pending_for(stream_id) {
+ return request_pending_stream_ids.has(stream_id);
+}
+
+export function add_request_pending_for(stream_id) {
+ request_pending_stream_ids.add(stream_id);
+}
+
+export function remove_request_pending_for(stream_id) {
+ request_pending_stream_ids.delete(stream_id);
}
diff --git a/web/src/stream_topic_history_util.js b/web/src/stream_topic_history_util.js
index c6d37758f5a..377af8e7d72 100644
--- a/web/src/stream_topic_history_util.js
+++ b/web/src/stream_topic_history_util.js
@@ -6,7 +6,11 @@ export function get_server_history(stream_id, on_success) {
on_success();
return;
}
+ if (stream_topic_history.is_request_pending_for(stream_id)) {
+ return;
+ }
+ stream_topic_history.add_request_pending_for(stream_id);
const url = "/json/users/me/" + stream_id + "/topics";
channel.get({
@@ -15,7 +19,11 @@ export function get_server_history(stream_id, on_success) {
success(data) {
const server_history = data.topics;
stream_topic_history.add_history(stream_id, server_history);
+ stream_topic_history.remove_request_pending_for(stream_id);
on_success();
},
+ error() {
+ stream_topic_history.remove_request_pending_for(stream_id);
+ },
});
}
diff --git a/web/tests/stream_topic_history.test.js b/web/tests/stream_topic_history.test.js
index e7ac9e98272..530f86207bb 100644
--- a/web/tests/stream_topic_history.test.js
+++ b/web/tests/stream_topic_history.test.js
@@ -309,21 +309,34 @@ test("server_history_end_to_end", () => {
];
let get_success_callback;
+ let get_error_callback;
let on_success_called;
channel.get = (opts) => {
assert.equal(opts.url, "/json/users/me/99/topics");
assert.deepEqual(opts.data, {});
+ 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, () => {});
+
+ // Another call. Early return because a request is already in progress
+ // for stream_id = 99. This function call adds coverage.
+ stream_topic_history_util.get_server_history(stream_id, () => {});
+ 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;
});
get_success_callback({topics});
-
assert.ok(on_success_called);
+ assert.ok(!stream_topic_history.is_request_pending_for(stream_id));
const history = stream_topic_history.get_recent_topic_names(stream_id);
assert.deepEqual(history, ["topic3", "topic2", "topic1"]);