From 686f6a762fec59dcda30ed24b12bd9ba9e029d64 Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Wed, 4 Jan 2017 21:50:15 -0500 Subject: Action Cable owns database connection, not Active Record Before this commit, the database connection used in Action Cable's PostgreSQL adapter was "owned" by `ActiveRecord::Base.connection_pool`. This meant that if, for example, `#clear_reloadable_connections!` was called on the pool, Active Record would "steal" the database connection from Action Cable, and would cause all sorts of issues. This became evident during file reloads; despite Action Cable trying its hardest to return its borrowed database connection to Active Record via `@pubsub.shutdown`, Active Record calls `#clear_reloadable_connections!` on the connection pool, and due to the order of callbacks, Active Record's callback was being executed first. This meant that if you tried to rerender a view after a file was reloaded, you would have to wait through Active Record's timeout and such. Now, Action Cable takes direct ownership of the database connection it uses. It removes the connection from the pool to avoid the situation described above. Action Cable also makes sure to call `#disconnect!` on the connection when appropriate, to match the previous behavior of Active Record. [ Jon Moss & Matthew Draper] --- .../test/subscription_adapter/postgresql_test.rb | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'actioncable/test') diff --git a/actioncable/test/subscription_adapter/postgresql_test.rb b/actioncable/test/subscription_adapter/postgresql_test.rb index beb6efab28..7c2608a7b8 100644 --- a/actioncable/test/subscription_adapter/postgresql_test.rb +++ b/actioncable/test/subscription_adapter/postgresql_test.rb @@ -37,4 +37,27 @@ class PostgresqlAdapterTest < ActionCable::TestCase def cable_config { adapter: "postgresql" } end + + def test_clear_active_record_connections_adapter_still_works + server = ActionCable::Server::Base.new + server.config.cable = cable_config.with_indifferent_access + server.config.logger = Logger.new(StringIO.new).tap { |l| l.level = Logger::UNKNOWN } + + adapter_klass = Class.new(server.config.pubsub_adapter) do + def active? + !@listener.nil? + end + end + + adapter = adapter_klass.new(server) + + subscribe_as_queue("channel", adapter) do |queue| + adapter.broadcast("channel", "hello world") + assert_equal "hello world", queue.pop + end + + ActiveRecord::Base.clear_reloadable_connections! + + assert adapter.active? + end end -- cgit v1.2.3