A platform for community discussion. Free, open, simple.
PERF: Fix memory leak in server-side plugin outlets (#36284)
Osama Sayegh168c1fee94bf
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex cde43666efb..99db0beddb0 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -520,6 +520,8 @@ module ApplicationHelper@all_connectors = Dir.glob("plugins/*/app/views/connectors/**/*.html.erb")end+ PLUGIN_OUTLET_TEMPLATE_CACHE = Concurrent::Map.new+def server_plugin_outlet(name, locals: {})return "" if !GlobalSetting.load_plugins?@@ -527,9 +529,29 @@ module ApplicationHelpererbs = ApplicationHelper.all_connectors.select { |c| c =~ matcher }return "" if erbs.blank?- result = +""- erbs.each { |erb| result << render(inline: File.read(erb), locals: locals) }- result.html_safe+ erbs+ .map do |erb|+ cache_key = [erb, locals.keys.sort]++ template =+ PLUGIN_OUTLET_TEMPLATE_CACHE.compute_if_absent(cache_key) do+ source = File.read(erb)+ handler = ActionView::Template.handler_for_extension("erb")++ ActionView::Template.new(+ source,+ "discourse_plugin_outlet__#{name}",+ handler,+ locals: locals.keys,+ format: :html,+ virtual_path: erb,+ )+ end++ render template: template, locals: locals+ end+ .join+ .html_safeenddef topic_featured_link_domain(link)diff --git a/config/initializers/development.rb b/config/initializers/development.rbnew file mode 100644index 00000000000..6f87eee7d09--- /dev/null+++ b/config/initializers/development.rb@@ -0,0 +1,5 @@+# frozen_string_literal: true++if Rails.env.development?+ Rails.application.reloader.to_prepare { ApplicationHelper::PLUGIN_OUTLET_TEMPLATE_CACHE.clear }+enddiff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 3764f303207..f1667a1a1a6 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -37,6 +37,40 @@ RSpec.describe TopicsController dobefore { SiteSetting.personal_message_enabled_groups = Group::AUTO_GROUPS[:everyone] }+ describe "topic_header plugin outlet" do+ fab!(:another_topic) { Fabricate(:topic, title: "Another topic by me") }++ let(:tmp_dir) { Dir.mktmpdir }+ let(:template_dir) do+ path = File.join(tmp_dir, "connectors", "topic_header")+ FileUtils.mkdir_p(path)+ path+ end+ let(:template_file) do+ file = Tempfile.new(%w[test_template .html.erb], template_dir)+ file.write("Topic title from outlet: <%= @topic_view.topic.title %>")+ file.close+ file+ end++ before do+ global_setting(:load_plugins?, true)+ ApplicationHelper.stubs(:all_connectors).returns([template_file.path])+ end++ after { FileUtils.remove_entry(tmp_dir) if tmp_dir }++ it "doesn't leak state between requests" do+ get "/t/#{topic.slug}/#{topic.id}"+ expect(response.status).to eq(200)+ expect(response.body).to include("Topic title from outlet: #{topic.title}")++ get "/t/#{another_topic.slug}/#{another_topic.id}"+ expect(response.status).to eq(200)+ expect(response.body).to include("Topic title from outlet: #{another_topic.title}")+ end+ end+describe "#wordpress" dobefore { sign_in(moderator) }