Skip to content

Commit

Permalink
Switch API search to RO replica only during actual search (thewca#8490)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorbg authored Nov 5, 2023
1 parent 4fc3e2b commit eca9fd4
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions WcaOnRails/app/controllers/api/v0/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,23 @@ def help
end

def search(*models)
ActiveRecord::Base.connected_to(role: :read_replica) do
query = params[:q]&.slice(0...SearchResultsController::SEARCH_QUERY_LIMIT)
unless query
render status: :bad_request, json: { error: "No query specified" }
return
end
concise_results_date = ComputeAuxiliaryData.end_date || Date.current
cache_key = ["search", *models, concise_results_date.iso8601, query]
result = Rails.cache.fetch(cache_key) do
query = params[:q]&.slice(0...SearchResultsController::SEARCH_QUERY_LIMIT)

unless query
render status: :bad_request, json: { error: "No query specified" }
return
end

concise_results_date = ComputeAuxiliaryData.end_date || Date.current
cache_key = ["search", *models, concise_results_date.iso8601, query]

result = Rails.cache.fetch(cache_key) do
ActiveRecord::Base.connected_to(role: :read_replica) do
models.flat_map { |model| model.search(query, params: params).limit(DEFAULT_API_RESULT_LIMIT) }
end
render status: :ok, json: { result: result }
end

render status: :ok, json: { result: result }
end

def posts_search
Expand Down

0 comments on commit eca9fd4

Please sign in to comment.