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.

web: Respect rate-limiting headers in main APIs.

Tim Abbotte91748022fbe
diff --git a/web/src/message_fetch.js b/web/src/message_fetch.js
index 88067fbdac7..d91205d73ef 100644
--- a/web/src/message_fetch.js
+++ b/web/src/message_fetch.js
@@ -370,15 +370,29 @@ export function load_messages(opts, attempt = 1) {
return;
}
- // Backoff on retries, with full jitter: up to 2s, 4s, 8s, 16s, 32s
- let delay = Math.random() * 2 ** attempt * 2000;
+ ui_report.show_error($("#connection-error"));
+
+ // 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 500s/502s.
+ //
+ // So we do the maximum of the retry-after header and an exponential
+ // backoff with full jitter: up to 2s, 4s, 8s, 16s, 32s
+ let backoff_delay_secs = Math.random() * 2 ** attempt * 2;
if (attempt >= 5) {
- delay = 30000;
+ backoff_delay_secs = 30;
}
- ui_report.show_error($("#connection-error"));
+ let rate_limit_delay_secs = 0;
+ if (xhr.status === 429 && xhr.responseJSON?.code === "RATE_LIMIT_HIT") {
+ // 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 = xhr.responseJSON["retry-after"] + Math.random() * 0.5;
+ }
+ const delay_secs = Math.max(backoff_delay_secs, rate_limit_delay_secs);
setTimeout(() => {
load_messages(opts, attempt + 1);
- }, delay);
+ }, delay_secs * 1000);
},
});
}
diff --git a/web/src/server_events.js b/web/src/server_events.js
index b319c7e73ef..cd5d7587e9f 100644
--- a/web/src/server_events.js
+++ b/web/src/server_events.js
@@ -239,8 +239,21 @@ function get_events({dont_block = false} = {}) {
} catch (error) {
blueslip.error("Failed to handle get_events error", undefined, error);
}
- const retry_sec = Math.min(90, Math.exp(get_events_failures / 2));
- get_events_timeout = setTimeout(get_events, retry_sec * 1000);
+
+ // 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 500s/502s.
+ const backoff_delay_secs = Math.min(90, Math.exp(get_events_failures / 2));
+ let rate_limit_delay_secs = 0;
+ if (xhr.status === 429 && xhr.responseJSON?.code === "RATE_LIMIT_HIT") {
+ // 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 = xhr.responseJSON["retry-after"] + Math.random() * 0.5;
+ }
+
+ const retry_delay_secs = Math.max(backoff_delay_secs, rate_limit_delay_secs);
+ get_events_timeout = setTimeout(get_events, retry_delay_secs * 1000);
},
});
}