A platform for community discussion. Free, open, simple.
PERF: rate limit search, and add anon cache for search results (#9969)
Jeff Wongde29b4a51111
diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rbindex a2dd66ea5f4..6bfce11fc5f 100644--- a/app/controllers/search_controller.rb+++ b/app/controllers/search_controller.rb@@ -28,6 +28,10 @@ class SearchController < ApplicationControllerraise Discourse::InvalidParameters.new("string contains null byte")end+ rate_limit_errors = rate_limit_search++ discourse_expires_in 1.minute+search_args = {type_filter: 'topic',guardian: guardian,@@ -48,7 +52,10 @@ class SearchController < ApplicationControllersearch_args[:ip_address] = request.remote_ipsearch_args[:user_id] = current_user.id if current_user.present?- if site_overloaded?+ if rate_limit_errors+ result = Search::GroupedSearchResults.new(search_args[:type_filter], @search_term, context, false, 0)+ result.error = I18n.t("rate_limiter.slow_down")+ elsif site_overloaded?result = Search::GroupedSearchResults.new(search_args[:type_filter], @search_term, context, false, 0)result.error = I18n.t("search.extreme_load_error")else@@ -76,6 +83,10 @@ class SearchController < ApplicationControllerraise Discourse::InvalidParameters.new("string contains null byte")end+ rate_limit_errors = rate_limit_search++ discourse_expires_in 1.minute+search_args = { guardian: guardian }search_args[:type_filter] = params[:type_filter] if params[:type_filter].present?@@ -94,7 +105,10 @@ class SearchController < ApplicationControllersearch_args[:user_id] = current_user.id if current_user.present?search_args[:restrict_to_archetype] = params[:restrict_to_archetype] if params[:restrict_to_archetype].present?- if site_overloaded?+ if rate_limit_errors+ result = Search::GroupedSearchResults.new(search_args[:type_filter], @search_term, context, false, 0)+ result.error = I18n.t("rate_limiter.slow_down")+ elsif site_overloaded?result = GroupedSearchResults.new(search_args["type_filter"], params[:term], context, false, 0)elsesearch = Search.new(params[:term], search_args)@@ -140,6 +154,19 @@ class SearchController < ApplicationController(queue_time > GlobalSetting.disable_search_queue_threshold)end+ def rate_limit_search+ begin+ if current_user.present?+ RateLimiter.new(current_user, "search-min", SiteSetting.rate_limit_search_user, 1.minute).performed!+ else+ RateLimiter.new(nil, "search-min-#{request.remote_ip}", SiteSetting.rate_limit_search_anon, 1.minute).performed!+ end+ rescue RateLimiter::LimitExceeded => e+ return e+ end+ false+ end+def cancel_overloaded_searchif site_overloaded?render_json_error I18n.t("search.extreme_load_error"), status: 409diff --git a/config/site_settings.yml b/config/site_settings.ymlindex c658ec727c1..382f2f63a36 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -1549,6 +1549,12 @@ rate_limits:rate_limit_create_post: 5rate_limit_new_user_create_topic: 120rate_limit_new_user_create_post: 30+ rate_limit_search_anon:+ hidden: true+ default: 15+ rate_limit_search_user:+ hidden: true+ default: 30max_topics_per_day: 20max_personal_messages_per_day: 20max_likes_per_day: 50diff --git a/spec/requests/search_controller_spec.rb b/spec/requests/search_controller_spec.rbindex dac11690d3e..044e741d54d 100644--- a/spec/requests/search_controller_spec.rb+++ b/spec/requests/search_controller_spec.rb@@ -179,6 +179,61 @@ describe SearchController doexpect(response.status).to eq(200)expect(SearchLog.where(term: 'wookie')).to be_blankend++ context 'rate limited' do+ before do+ SiteSetting.rate_limit_search_user = 3+ SiteSetting.rate_limit_search_anon = 2+ end++ it 'rate limits searches' do+ RateLimiter.enable+ RateLimiter.clear_all!++ 2.times do+ get "/search/query.json", params: {+ term: 'wookie'+ }++ expect(response.status).to eq(200)+ json = response.parsed_body+ expect(json["grouped_search_result"]["error"]).to eq(nil)+ end++ get "/search/query.json", params: {+ term: 'wookie'+ }+ expect(response.status).to eq(200)+ json = response.parsed_body+ expect(json["grouped_search_result"]["error"]).to eq(I18n.t("rate_limiter.slow_down"))+ end++ context "and a logged in user" do+ before { sign_in(user) }++ it 'rate limits logged in searches' do+ RateLimiter.enable+ RateLimiter.clear_all!++ 3.times do+ get "/search/query.json", params: {+ term: 'wookie'+ }++ expect(response.status).to eq(200)+ json = response.parsed_body+ expect(json["grouped_search_result"]["error"]).to eq(nil)+ end++ get "/search/query.json", params: {+ term: 'wookie'+ }+ expect(response.status).to eq(200)+ json = response.parsed_body+ expect(json["grouped_search_result"]["error"]).to eq(I18n.t("rate_limiter.slow_down"))+ end+ end+ endendcontext "#show" do@@ -217,6 +272,63 @@ describe SearchController doexpect(response.status).to eq(200)expect(SearchLog.where(term: 'bantha')).to be_blankend++ context 'rate limited' do++ before do+ SiteSetting.rate_limit_search_user = 3+ SiteSetting.rate_limit_search_anon = 2+ end++ it 'rate limits searches' do+ RateLimiter.enable+ RateLimiter.clear_all!++ 2.times do+ get "/search.json", params: {+ q: 'bantha'+ }++ expect(response.status).to eq(200)+ json = response.parsed_body+ expect(json["grouped_search_result"]["error"]).to eq(nil)+ end++ get "/search.json", params: {+ q: 'bantha'+ }+ expect(response.status).to eq(200)+ json = response.parsed_body+ expect(json["grouped_search_result"]["error"]).to eq(I18n.t("rate_limiter.slow_down"))++ end++ context "and a logged in user" do+ before { sign_in(user) }++ it 'rate limits searches' do+ RateLimiter.enable+ RateLimiter.clear_all!++ 3.times do+ get "/search.json", params: {+ q: 'bantha'+ }++ expect(response.status).to eq(200)+ json = response.parsed_body+ expect(json["grouped_search_result"]["error"]).to eq(nil)+ end++ get "/search.json", params: {+ q: 'bantha'+ }+ expect(response.status).to eq(200)+ json = response.parsed_body+ expect(json["grouped_search_result"]["error"]).to eq(I18n.t("rate_limiter.slow_down"))+ end+ end+ endendcontext "search priority" do