mirror of
https://github.com/discourse/discourse.git
synced 2026-08-12 03:37:13 +08:00
Previously, AdminDashboardSectionLoader worker threads leased a database connection on their first query and held it while idling in the shared thread pool, which exhausted the connection pool (sized 5 in development) and made subsequent requests fail with "could not obtain a connection from the pool within 5.000 seconds". This change wraps each section build in ActiveRecord::Base.with_connection(prevent_permanent_checkout: true) so the connection is checked back in as soon as the section finishes building - the flag is required because sections use ActiveRecord::Base.connection internally, which would otherwise mark the connection as permanently checked out for the thread. The regression spec lives in a separate describe block with use_transactional_tests = false because both transactional tests and fab!'s before(:all) transaction pin a single shared connection across threads, which masks the leak.
84 lines
2.3 KiB
Ruby
Vendored
84 lines
2.3 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
class AdminDashboardSectionLoader
|
|
def self.build(section_ids:, current_user:, start_date:, end_date:)
|
|
new(
|
|
section_ids: section_ids,
|
|
current_user: current_user,
|
|
start_date: start_date,
|
|
end_date: end_date,
|
|
).build
|
|
end
|
|
|
|
def self.pool_size
|
|
desired =
|
|
AdminDashboardSectionConfiguration::KNOWN_SECTIONS.size +
|
|
DiscoursePluginRegistry.admin_dashboard_sections.size
|
|
|
|
available = [ActiveRecord::Base.connection_pool.size - 1, 1].max
|
|
[desired, available].min
|
|
end
|
|
|
|
def self.thread_pool
|
|
@thread_pool ||=
|
|
Scheduler::ThreadPool.new(min_threads: 0, max_threads: pool_size, idle_time: 30)
|
|
end
|
|
|
|
def initialize(section_ids:, current_user:, start_date:, end_date:)
|
|
@section_ids = section_ids
|
|
@current_user = current_user
|
|
@start_date = start_date
|
|
@end_date = end_date
|
|
end
|
|
|
|
def build
|
|
results = Queue.new
|
|
|
|
section_ids.each do |id|
|
|
self.class.thread_pool.post do
|
|
ActiveRecord::Base.with_connection(prevent_permanent_checkout: true) do
|
|
results << { id: id, data: section_data(id, current_user) }
|
|
end
|
|
rescue StandardError => e
|
|
results << { id: id, error: e }
|
|
end
|
|
end
|
|
|
|
results_by_id = {}
|
|
|
|
section_ids.size.times do
|
|
result = results.pop
|
|
raise result[:error] if result[:error]
|
|
|
|
results_by_id[result[:id]] = result
|
|
end
|
|
|
|
section_ids.map { |id| results_by_id.fetch(id) }
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :section_ids, :current_user, :start_date, :end_date
|
|
|
|
def section_data(id, user)
|
|
case id
|
|
when "highlights"
|
|
AdminDashboardHighlights.build(start_date: start_date, end_date: end_date)
|
|
when "traffic"
|
|
AdminDashboardSiteTraffic.build(
|
|
start_date: start_date,
|
|
end_date: end_date,
|
|
guardian: user.guardian,
|
|
)
|
|
when "engagement"
|
|
AdminDashboardEngagement.build(start_date: start_date, end_date: end_date, current_user: user)
|
|
when "reports"
|
|
AdminDashboard::Reports::Section.build(guardian: user.guardian)
|
|
when "search"
|
|
AdminDashboardSearch.build(start_date: start_date, end_date: end_date)
|
|
else
|
|
section = DiscoursePluginRegistry.admin_dashboard_sections.find { |s| s[:id] == id }
|
|
section&.dig(:loader)&.call(start_date: start_date, end_date: end_date, current_user: user)
|
|
end
|
|
end
|
|
end
|