Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
activity: Use setTimeout instead of setInterval.
Sahil Batraa0965d7335b7
diff --git a/frontend_tests/node_tests/activity.js b/frontend_tests/node_tests/activity.jsindex 0fbd01b5c62..3427ed1fc47 100644--- a/frontend_tests/node_tests/activity.js+++ b/frontend_tests/node_tests/activity.js@@ -615,7 +615,11 @@ test("initialize", ({override, mock_template}) => {let payload;override(channel, "post", (arg) => {- payload = arg;+ if (payload === undefined) {+ // This "if" block is added such that we can execute "success"+ // function when want_redraw is true.+ payload = arg;+ }});function clear() {@@ -636,6 +640,17 @@ test("initialize", ({override, mock_template}) => {activity.mark_client_idle();$(window).off("focus");++ let set_timeout_function_called = false;+ set_global("setTimeout", (func) => {+ if (set_timeout_function_called) {+ // This conditional is needed to avoid indefinite calls.+ return;+ }+ set_timeout_function_called = true;+ func();+ });+activity.initialize();payload.success({zephyr_mirror_active: true,@@ -652,8 +667,8 @@ test("initialize", ({override, mock_template}) => {$(window).idle = (params) => {params.onIdle();};-- set_global("setInterval", (func) => func());+ payload = undefined;+ set_timeout_function_called = false;$(window).off("focus");activity.initialize();diff --git a/static/js/activity.js b/static/js/activity.jsindex 99154c169e5..ee98b5ad7b5 100644--- a/static/js/activity.js+++ b/static/js/activity.js@@ -241,15 +241,37 @@ export function initialize() {buddy_list.start_scroll_handler();- // Let the server know we're here, but pass "false" for- // want_redraw, since we just got all this info in page_params.- send_presence_to_server(false);-function get_full_presence_list_update() {+ // Schedule the next presence update request to the server.+ // This implementation aims to simulate setInterval; in+ // particular, we schedule the next request immediately,+ // rather than waiting for the current request to+ // finish/succeed.+ //+ // We previously used setInterval for this purpose, but+ // empirically observed that after unsuspend, Chrome can end+ // up trying to "catch up" by doing dozens of these requests+ // at once, wasting resources as well as hitting rate limits+ // on the server. We have not been able to reproduce this+ // reliably enough to be certain whether the setInterval+ // requests are those that would have happened while the+ // laptop was suspended or during a window after unsuspend+ // before the user focuses the browser tab.+ //+ // But using setTimeout this instead ensures that we're only+ // scheduling a next request if the browser will actually be+ // calling send_presence_to_server.+ setTimeout(get_full_presence_list_update, ACTIVE_PING_INTERVAL_MS);+send_presence_to_server(true);}- setInterval(get_full_presence_list_update, ACTIVE_PING_INTERVAL_MS);+ // Schedule our first full presence update.+ setTimeout(get_full_presence_list_update, ACTIVE_PING_INTERVAL_MS);++ // Let the server know we're here, but pass "false" for+ // want_redraw, since we just got all this info in page_params.+ send_presence_to_server(false);}export function update_presence_info(user_id, info, server_time) {