aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/server/worker/clear_database_connections.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/action_cable/server/worker/clear_database_connections.rb')
-rw-r--r--lib/action_cable/server/worker/clear_database_connections.rb22
1 files changed, 22 insertions, 0 deletions
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