From e584f49e867de8355fd455e3429d73c3e245dbc7 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 8 Jul 2015 22:36:29 +0200 Subject: Include the clearing of database connections configuration by default --- lib/action_cable/server.rb | 2 ++ .../server/worker/clear_database_connections.rb | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 lib/action_cable/server/worker/clear_database_connections.rb (limited to 'lib') diff --git a/lib/action_cable/server.rb b/lib/action_cable/server.rb index e7cc70b68d..919ebd94de 100644 --- a/lib/action_cable/server.rb +++ b/lib/action_cable/server.rb @@ -4,6 +4,8 @@ module ActionCable autoload :Broadcasting, 'action_cable/server/broadcasting' autoload :Connections, 'action_cable/server/connections' autoload :Configuration, 'action_cable/server/configuration' + autoload :Worker, 'action_cable/server/worker' + autoload :ClearDatabaseConnections, 'action_cable/server/worker/clear_database_connections' end end diff --git a/lib/action_cable/server/worker/clear_database_connections.rb b/lib/action_cable/server/worker/clear_database_connections.rb new file mode 100644 index 0000000000..722d363a41 --- /dev/null +++ b/lib/action_cable/server/worker/clear_database_connections.rb @@ -0,0 +1,22 @@ +module ActionCable + module Server + class Worker + # Clear active connections between units of work so the long-running channel or connection processes do not hoard connections. + module ClearDatabaseConnections + extend ActiveSupport::Concern + + included do + if defined?(ActiveRecord::Base) + set_callback :work, :around, :with_database_connections + end + end + + def with_database_connections + yield + ensure + ActiveRecord::Base.clear_active_connections! + end + end + end + end +end \ No newline at end of file -- cgit v1.2.3