diff --git a/config/application.rb b/config/application.rb index 225efb3dd..08dfc07f9 100644 --- a/config/application.rb +++ b/config/application.rb @@ -14,6 +14,11 @@ module Rosa class Application < Rails::Application config.i18n.enforce_available_locales = true + unless Rails.env.test? + require 'close_ar_connections_middleware' + config.middleware.insert_after('Rack::Sendfile', CloseArConnectionsMiddleware) + end + # Rate limit config.middleware.insert_before Rack::Runtime, ApiDefender diff --git a/lib/close_ar_connections_middleware.rb b/lib/close_ar_connections_middleware.rb new file mode 100644 index 000000000..e491a8a23 --- /dev/null +++ b/lib/close_ar_connections_middleware.rb @@ -0,0 +1,12 @@ +# "Coffee's for closers" +class CloseArConnectionsMiddleware + def initialize(app) + @app = app + end + + def call(env) + @app.call(env) + ensure + ActiveRecord::Base.clear_active_connections! unless env.key?('rack.test') + end +end