A platform for community discussion. Free, open, simple.
FIX: Handle rate limited responses across the remaining call sites
Régis Hanol0e6bb06b81d8
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rbindex 7a2a37bdd56..460dff39d6f 100644--- a/app/controllers/users_controller.rb+++ b/app/controllers/users_controller.rb@@ -678,11 +678,7 @@ class UsersController < ApplicationControllerenddef check_email- begin- RateLimiter.new(nil, "check-email-#{request.remote_ip}", 10, 1.minute).performed!- rescue RateLimiter::LimitExceeded- return render json: success_json- end+ RateLimiter.new(nil, "check-email-#{request.remote_ip}", 10, 1.minute).performed!email = Email.downcase((params[:email] || "").strip)diff --git a/frontend/discourse/app/components/modal/bulk-topic-actions.gjs b/frontend/discourse/app/components/modal/bulk-topic-actions.gjsindex 06035c7883b..adc37502b99 100644--- a/frontend/discourse/app/components/modal/bulk-topic-actions.gjs+++ b/frontend/discourse/app/components/modal/bulk-topic-actions.gjs@@ -6,9 +6,9 @@ import { action } from "@ember/object";import { getOwner } from "@ember/owner";import { service } from "@ember/service";import { trustHTML } from "@ember/template";-import { Promise } from "rsvp";import ManageTagsForm from "discourse/components/modal/bulk-topic-actions/manage-tags-form";import BulkPinOptions from "discourse/components/modal/feature-topic/bulk-pin-options";+import { extractError } from "discourse/lib/ajax-error";import { topicLevels } from "discourse/lib/notification-levels";import renderTag from "discourse/lib/render-tag";import { escapeExpression } from "discourse/lib/utilities";@@ -362,7 +362,7 @@ export default class BulkTopicActions extends Component {return chunks;}- _processChunks(operation) {+ async _processChunks(operation) {const allTopics = this.model.bulkSelectHelper.selected;const topicChunks = this._generateTopicChunks(allTopics);const topicIds = [];@@ -382,59 +382,47 @@ export default class BulkTopicActions extends Component {options.asJSON = true;}- const tasks = topicChunks.map((topics) => async () => {- const result = await Topic.bulkOperation(topics, operation, options);- this.processedTopicCount += topics.length;- return result;- });+ for (const topics of topicChunks) {+ try {+ const result = await Topic.bulkOperation(topics, operation, options);+ this.processedTopicCount += topics.length;- return new Promise((resolve, reject) => {- const resolveNextTask = async () => {- if (tasks.length === 0) {- const topics = topicIds.map((id) =>- allTopics.find((value) => value.id === id)- );- const errors = Object.keys(mergedErrors).length ? mergedErrors : null;- const tagCategoryErrors = Object.values(mergedTagCategoryErrors);- return resolve({- topics,- errors,- tagCategoryErrors: tagCategoryErrors.length- ? tagCategoryErrors- : null,- });+ if (result?.topic_ids) {+ topicIds.push(...result.topic_ids);}-- const task = tasks.shift();-- try {- const result = await task();- if (result?.topic_ids) {- topicIds.push(...result.topic_ids);+ if (result?.errors) {+ for (const [msg, count] of Object.entries(result.errors)) {+ mergedErrors[msg] = (mergedErrors[msg] || 0) + count;}- if (result?.errors) {- for (const [msg, count] of Object.entries(result.errors)) {- mergedErrors[msg] = (mergedErrors[msg] || 0) + count;- }- }- if (result?.tag_category_errors) {- for (const error of result.tag_category_errors) {- const key = `${error.category_id}:${error.tag_names.join(",")}`;- if (mergedTagCategoryErrors[key]) {- mergedTagCategoryErrors[key].count += error.count;- } else {- mergedTagCategoryErrors[key] = { ...error };- }+ }+ if (result?.tag_category_errors) {+ for (const error of result.tag_category_errors) {+ const key = `${error.category_id}:${error.tag_names.join(",")}`;+ if (mergedTagCategoryErrors[key]) {+ mergedTagCategoryErrors[key].count += error.count;+ } else {+ mergedTagCategoryErrors[key] = { ...error };}}- resolveNextTask();- } catch {- reject();}- };+ } catch (error) {+ const message = extractError(error);+ mergedErrors[message] = (mergedErrors[message] || 0) + topics.length;+ break;+ }+ }- resolveNextTask();- });+ const topics = topicIds.map((id) =>+ allTopics.find((value) => value.id === id)+ );+ const errors = Object.keys(mergedErrors).length ? mergedErrors : null;+ const tagCategoryErrors = Object.values(mergedTagCategoryErrors);++ return {+ topics,+ errors,+ tagCategoryErrors: tagCategoryErrors.length ? tagCategoryErrors : null,+ };}_buildTagCategoryError(error) {diff --git a/frontend/discourse/app/controllers/topic.js b/frontend/discourse/app/controllers/topic.jsindex 93e1c8bda26..c414211a1cc 100644--- a/frontend/discourse/app/controllers/topic.js+++ b/frontend/discourse/app/controllers/topic.js@@ -23,7 +23,11 @@ import JumpToPost from "discourse/components/modal/jump-to-post";import PermanentlyDeleteConfirmModal from "discourse/components/modal/permanently-delete-confirm";import { MIN_POSTS_COUNT } from "discourse/components/topic-map/topic-map-summary";import { ajax } from "discourse/lib/ajax";-import { popupAjaxError } from "discourse/lib/ajax-error";+import {+ isRateLimitError,+ popupAjaxError,+ rateLimitWaitSeconds,+} from "discourse/lib/ajax-error";import {addUniqueValueToArray,removeValueFromArray,@@ -1604,35 +1608,28 @@ export default class TopicController extends Controller {if (this._retryInProgress) {discourseLater(() => {- this.retryOnRateLimit(times, promise, topicId);+ this.retryOnRateLimit(times, promise, topicId)?.catch(() => {});}, 100);return;}this._retryInProgress = true;- promise()+ return promise().catch((e) => {- const xhr = e.jqXHR;- if (- xhr &&- xhr.status === 429 &&- xhr.responseJSON &&- xhr.responseJSON.extras &&- xhr.responseJSON.extras.wait_seconds- ) {- let waitSeconds = xhr.responseJSON.extras.wait_seconds;- if (waitSeconds < 5) {- waitSeconds = 5;- }+ if (!isRateLimitError(e)) {+ throw e;+ }- this._retryRateLimited = true;+ this._retryRateLimited = true;- discourseLater(() => {+ discourseLater(+ () => {this._retryRateLimited = false;- this.retryOnRateLimit(times - 1, promise, topicId);- }, waitSeconds * 1000);- }+ this.retryOnRateLimit(times - 1, promise, topicId)?.catch(() => {});+ },+ rateLimitWaitSeconds(e) * 1000+ );}).finally(() => {this._retryInProgress = false;@@ -1755,7 +1752,7 @@ export default class TopicController extends Controller {this._newPostsInStream = postIds.concat(this._newPostsInStream);throw e;});- });+ })?.catch(() => {});if (this.get("currentUser.id") !== data.user_id) {this.documentTitle.incrementBackgroundContextCount();diff --git a/frontend/discourse/app/lib/user-search.js b/frontend/discourse/app/lib/user-search.jsindex 48227e743ea..4bba9b03bff 100644--- a/frontend/discourse/app/lib/user-search.js+++ b/frontend/discourse/app/lib/user-search.js@@ -1,6 +1,10 @@import { cancel } from "@ember/runloop";import { Promise } from "rsvp";import { ajax } from "discourse/lib/ajax";+import {+ isRateLimitError,+ rateLimitWaitSeconds,+} from "discourse/lib/ajax-error";import { camelCaseToSnakeCase } from "discourse/lib/case-converter";import discourseDebounce from "discourse/lib/debounce";import { isTesting } from "discourse/lib/environment";@@ -15,7 +19,8 @@ let cache = {},cacheKey,cacheTime,currentTerm,- oldSearch;+ oldSearch,+ rateLimitedUntil = 0;export function resetUserSearchCache() {cache = {};@@ -23,6 +28,7 @@ export function resetUserSearchCache() {cacheTime = null;currentTerm = null;oldSearch = null;+ rateLimitedUntil = 0;}function performSearch(@@ -47,6 +53,11 @@ function performSearch(return;}+ if (rateLimitedUntil > Date.now()) {+ resultsFn(CANCELLED_STATUS);+ return;+ }+const eagerComplete = eagerCompleteSearch(term, topicId || categoryId);if (term === "" && !eagerComplete && !lastSeenUsers) {@@ -105,6 +116,11 @@ function performSearch(returnVal = r;}})+ .catch(function (error) {+ if (isRateLimitError(error)) {+ rateLimitedUntil = Date.now() + rateLimitWaitSeconds(error) * 1000;+ }+ }).finally(function () {oldSearch = null;resultsFn(cloneJSON(returnVal));diff --git a/frontend/discourse/app/services/screen-track.js b/frontend/discourse/app/services/screen-track.jsindex a6a97121b89..8bec655a131 100644--- a/frontend/discourse/app/services/screen-track.js+++ b/frontend/discourse/app/services/screen-track.js@@ -1,6 +1,10 @@import { run } from "@ember/runloop";import Service, { service } from "@ember/service";import { ajax } from "discourse/lib/ajax";+import {+ isRateLimitError,+ rateLimitWaitSeconds,+} from "discourse/lib/ajax-error";import { bind } from "discourse/lib/decorators";import { isTesting } from "discourse/lib/environment";import { disableImplicitInjections } from "discourse/lib/implicit-injections";@@ -208,14 +212,21 @@ export default class ScreenTrack extends Service {this.appEvents.trigger("topic:timings-sent", data);} catch (e) {if (e.jqXHR && ALLOWED_AJAX_FAILURES.includes(e.jqXHR.status)) {- const delay = AJAX_FAILURE_DELAYS[this._ajaxFailures];- this._ajaxFailures += 1;+ let delay;- if (delay) {- this._blockSendingToServerTill = Date.now() + delay;- // we did not send to the server, got to re-queue it- this.consolidateTimings(timings, topicTime, topicId);+ if (isRateLimitError(e)) {+ delay = rateLimitWaitSeconds(e) * 1000;+ } else {+ delay =+ AJAX_FAILURE_DELAYS[+ Math.min(this._ajaxFailures, AJAX_FAILURE_DELAYS.length - 1)+ ];+ this._ajaxFailures += 1;}++ this._blockSendingToServerTill = Date.now() + delay;+ // we did not send to the server, got to re-queue it+ this.consolidateTimings(timings, topicTime, topicId);}if (window.console && window.console.warn && e.jqXHR) {diff --git a/frontend/discourse/tests/integration/components/modal/bulk-topic-actions-test.gjs b/frontend/discourse/tests/integration/components/modal/bulk-topic-actions-test.gjsnew file mode 100644index 00000000000..7d2b68ae271--- /dev/null+++ b/frontend/discourse/tests/integration/components/modal/bulk-topic-actions-test.gjs@@ -0,0 +1,85 @@+import { getOwner } from "@ember/owner";+import { click, render, settled } from "@ember/test-helpers";+import { module, test } from "qunit";+import BulkTopicActions from "discourse/components/modal/bulk-topic-actions";+import ModalContainer from "discourse/components/modal-container";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";+import pretender, {+ middlewareRateLimit,+ response,+} from "discourse/tests/helpers/create-pretender";+import { i18n } from "discourse-i18n";++const CHUNK_SIZE = 30;+const TOPIC_COUNT = 2 * CHUNK_SIZE + 1;++function withoutMarkup(key, count) {+ return i18n(key, { count }).replace(/<\/?b>/g, "");+}++module(+ "Integration | Component | Modal | bulk-topic-actions",+ function (hooks) {+ setupRenderingTest(hooks);++ test("reports what a partially applied bulk action did and did not change", async function (assert) {+ const selected = Array.from({ length: TOPIC_COUNT }, (_, i) => ({+ id: i + 1,+ }));++ let requestCount = 0;+ pretender.put("/topics/bulk", () => {+ requestCount++;+ if (requestCount === 1) {+ return response({+ topic_ids: selected.slice(0, CHUNK_SIZE).map((topic) => topic.id),+ });+ }+ return middlewareRateLimit(60);+ });++ await render(<template><ModalContainer /></template>);++ getOwner(this)+ .lookup("service:modal")+ .show(BulkTopicActions, {+ model: {+ action: "reset-bump-dates",+ title: "Reset bump dates",+ showFooter: true,+ bulkSelectHelper: { selected, toggleBulkSelect: () => {} },+ },+ });+ await settled();++ await click("#bulk-topics-confirm");++ assert.strictEqual(+ requestCount,+ 2,+ "it stops sending chunks after the first failure"+ );+ assert+ .dom(".topic-bulk-actions-modal__errors")+ .includesText(+ withoutMarkup("topics.bulk.completed_count", CHUNK_SIZE),+ "the applied topics are reported"+ );+ assert+ .dom(".topic-bulk-actions-modal__errors")+ .includesText(+ withoutMarkup("topics.bulk.not_completed", CHUNK_SIZE),+ "the failed topics are reported"+ );+ assert+ .dom(".topic-bulk-actions-modal__errors")+ .includesText(+ withoutMarkup(+ "topics.bulk.skipped_count",+ TOPIC_COUNT - 2 * CHUNK_SIZE+ ),+ "the topics that were never tried are reported"+ );+ });+ }+);diff --git a/frontend/discourse/tests/unit/controllers/topic-test.js b/frontend/discourse/tests/unit/controllers/topic-test.jsindex c05bfe4c6f1..828ea70232e 100644--- a/frontend/discourse/tests/unit/controllers/topic-test.js+++ b/frontend/discourse/tests/unit/controllers/topic-test.js@@ -9,7 +9,10 @@ import { forceMobile } from "discourse/lib/mobile";import { registerOptimisticPostUpdate } from "discourse/lib/optimistic-post-updates";import { withPluginApi } from "discourse/lib/plugin-api";import { Placeholder } from "discourse/models/post-stream";-import pretender, { response } from "discourse/tests/helpers/create-pretender";+import pretender, {+ response,+ TOO_MANY_REQUESTS,+} from "discourse/tests/helpers/create-pretender";function topicWithStream(streamDetails) {const topic = this.store.createRecord("topic");@@ -784,6 +787,42 @@ module("Unit | Controller | topic", function (hooks) {);});+ test("retryOnRateLimit retries a 429 that only carries a Retry-After header", async function (assert) {+ const controller = getOwner(this).lookup("controller:topic");+ controller.setProperties({ model: this.store.createRecord("topic") });++ const rateLimited = {+ jqXHR: {+ status: TOO_MANY_REQUESTS,+ getResponseHeader: (name) => (name === "Retry-After" ? "1" : null),+ },+ };++ const promise = sinon.stub();+ promise.onFirstCall().callsFake(() => Promise.reject(rateLimited));+ promise.onSecondCall().resolves();++ controller.retryOnRateLimit(2, promise);+ await settled();++ assert.strictEqual(promise.callCount, 2, "the request is retried");+ });++ test("retryOnRateLimit does not swallow errors that are not rate limits", async function (assert) {+ const controller = getOwner(this).lookup("controller:topic");+ controller.setProperties({ model: this.store.createRecord("topic") });++ const promise = sinon+ .stub()+ .callsFake(() => Promise.reject({ jqXHR: { status: 500 } }));++ await assert.rejects(+ controller.retryOnRateLimit(2, promise),+ "the error reaches the caller"+ );+ assert.strictEqual(promise.callCount, 1, "it is not retried");+ });+test("onMessage live-inserts new posts from regular users", async function (assert) {const topic = topicWithStream.call(this, {posts: [{ id: 1, post_number: 1 }],diff --git a/frontend/discourse/tests/unit/lib/pretty-text/inline-oneboxer-test.js b/frontend/discourse/tests/unit/lib/pretty-text/inline-oneboxer-test.jsindex 27b2b49042b..0569f842792 100644--- a/frontend/discourse/tests/unit/lib/pretty-text/inline-oneboxer-test.js+++ b/frontend/discourse/tests/unit/lib/pretty-text/inline-oneboxer-test.js@@ -3,7 +3,10 @@ import { setupTest } from "ember-qunit";import { applyInlineOneboxes } from "pretty-text/inline-oneboxer";import { module, test } from "qunit";import { ajax } from "discourse/lib/ajax";-import pretender, { response } from "discourse/tests/helpers/create-pretender";+import pretender, {+ middlewareRateLimit,+ response,+} from "discourse/tests/helpers/create-pretender";module("Unit | Pretty Text | Inline Oneboxer", function (hooks) {setupTest(hooks);@@ -44,6 +47,55 @@ module("Unit | Pretty Text | Inline Oneboxer", function (hooks) {assert.dom(link).hasText("PR title");});+ test("stops sending batches once rate limited", async function (assert) {+ const rateLimitedLinks = {};+ for (let i = 0; i < 11; i++) {+ rateLimitedLinks[`http://example.com/limited-${i}`] =+ document.createElement("DIV");+ }++ let requestCount = 0;+ pretender.get("/inline-onebox", () => {+ requestCount++;+ return middlewareRateLimit(60);+ });++ await applyInlineOneboxes(rateLimitedLinks, ajax);++ assert.strictEqual(+ requestCount,+ 1,+ "the second batch of the 11 urls is never sent"+ );+ });++ test("looks a rate limited url up again", async function (assert) {+ const url = "http://example.com/rate-limited";+ const link = document.createElement("a");+ link.classList.add("inline-onebox-loading");+ link.href = url;++ let requestCount = 0;+ pretender.get("/inline-onebox", () => {+ requestCount++;+ if (requestCount === 1) {+ return middlewareRateLimit(60);+ }++ return response({+ "inline-oneboxes": [{ url, title: "Retried title" }],+ });+ });++ await applyInlineOneboxes({ [url]: [link] }, ajax);+ assert.dom(link).hasClass("inline-onebox-loading");++ await applyInlineOneboxes({ [url]: [link] }, ajax);++ assert.strictEqual(requestCount, 2, "the rate limited url is retried");+ assert.dom(link).hasText("Retried title");+ });+test("batches requests when oneboxing more than 10 urls", async function (assert) {const requestedUrls = [];let requestCount = 0;diff --git a/frontend/discourse/tests/unit/lib/user-search-test.js b/frontend/discourse/tests/unit/lib/user-search-test.jsindex 05b676acc4d..6bf22a8cac0 100644--- a/frontend/discourse/tests/unit/lib/user-search-test.js+++ b/frontend/discourse/tests/unit/lib/user-search-test.js@@ -1,7 +1,10 @@import { setupTest } from "ember-qunit";import { module, test } from "qunit";import userSearch from "discourse/lib/user-search";-import pretender, { response } from "discourse/tests/helpers/create-pretender";+import pretender, {+ middlewareRateLimit,+ response,+} from "discourse/tests/helpers/create-pretender";import { CANCELLED_STATUS } from "discourse/ui-kit/modifiers/d-autocomplete";module("Unit | Utility | user-search", function (hooks) {@@ -291,6 +294,29 @@ module("Unit | Utility | user-search", function (hooks) {});});+ test("it stops searching while the advertised rate limit wait is running", async function (assert) {+ let requestCount = 0;+ pretender.get("/u/search/users", () => {+ requestCount++;+ return middlewareRateLimit(60);+ });++ let result = await userSearch({ term: "ratelimited" });+ assert.strictEqual(+ result,+ CANCELLED_STATUS,+ "the rejection is handled instead of bubbling up"+ );++ result = await userSearch({ term: "ratelimitedtoo" });+ assert.strictEqual(result, CANCELLED_STATUS);+ assert.strictEqual(+ requestCount,+ 1,+ "no further request is sent before the wait is over"+ );+ });+test("it protects cached results from mutation by consumers", async function (assert) {pretender.get("/u/search/users", (request) => {if (request.url.includes("term=test_cache")) {diff --git a/frontend/discourse/tests/unit/services/screen-track-test.js b/frontend/discourse/tests/unit/services/screen-track-test.jsindex 766e6045671..a32c8a689b4 100644--- a/frontend/discourse/tests/unit/services/screen-track-test.js+++ b/frontend/discourse/tests/unit/services/screen-track-test.js@@ -1,5 +1,9 @@import { setupTest } from "ember-qunit";import { module, test } from "qunit";+import pretender, {+ middlewareRateLimit,+ response,+} from "discourse/tests/helpers/create-pretender";module("Unit | Service | screen-track", function (hooks) {setupTest(hooks);@@ -29,6 +33,38 @@ module("Unit | Service | screen-track", function (hooks) {);});+ test("keeps re-queuing timings once the failure delays are exhausted", async function (assert) {+ const tracker = this.owner.lookup("service:screen-track");+ pretender.post("/topics/timings", () => response(500, {}));++ tracker.consolidateTimings({ 1: 10 }, 10, 1);++ for (let attempt = 0; attempt < 6; attempt++) {+ tracker._blockSendingToServerTill = null;+ await tracker.sendNextConsolidatedTiming();+ }++ assert.deepEqual(+ tracker._consolidatedTimings,+ [{ timings: { 1: 10 }, topicTime: 10, topicId: 1 }],+ "the read timings are never dropped"+ );+ });++ test("waits the advertised Retry-After before sending timings again", async function (assert) {+ const tracker = this.owner.lookup("service:screen-track");+ pretender.post("/topics/timings", () => middlewareRateLimit(60));++ tracker.consolidateTimings({ 1: 10 }, 10, 1);+ const before = Date.now();+ await tracker.sendNextConsolidatedTiming();++ assert.true(+ tracker._blockSendingToServerTill - before >= 60000,+ "the server's Retry-After beats the local backoff ladder"+ );+ });+test("appEvent topic:timings-sent is triggered after posting consolidated timings", async function (assert) {const tracker = this.owner.lookup("service:screen-track");const appEvents = this.owner.lookup("service:app-events");diff --git a/frontend/pretty-text/addon/inline-oneboxer.js b/frontend/pretty-text/addon/inline-oneboxer.jsindex c6c0b62fbf7..1d59b97252a 100644--- a/frontend/pretty-text/addon/inline-oneboxer.js+++ b/frontend/pretty-text/addon/inline-oneboxer.js@@ -1,3 +1,5 @@+const TOO_MANY_REQUESTS = 429;+const _cache = {};export async function applyInlineOneboxes(inline, ajax, opts) {@@ -42,6 +44,13 @@ export async function applyInlineOneboxes(inline, ajax, opts) {}});} catch (err) {+ const rateLimited = (err.jqXHR ?? err).status === TOO_MANY_REQUESTS;++ if (rateLimited) {+ urls.slice(i).forEach((url) => delete _cache[url]);+ return;+ }+// eslint-disable-next-line no-consoleconsole.error("Inline onebox request failed", err, batch);}diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rbindex daafcf7fe73..a38a2368b49 100644--- a/spec/requests/users_controller_spec.rb+++ b/spec/requests/users_controller_spec.rb@@ -2499,6 +2499,18 @@ RSpec.describe UsersController doget "/u/check_email.json", params: { email: Fabricate(:staged).email }expect(response.parsed_body["success"]).to be_presentend++ it "rate limits requests per IP instead of failing open" do+ RateLimiter.enable++ 10.times { get "/u/check_email.json", params: { email: "[email protected]" } }+ get "/u/check_email.json", params: { email: user1.email }++ expect(response.status).to eq(429)+ expect(response.headers["Retry-After"]).to be_present+ expect(response.parsed_body["success"]).to be_blank+ expect(response.parsed_body["extras"]["wait_seconds"]).to be > 0+ endenddescribe "#invited" do