Skip to content

Commit

Permalink
Fix bug with non-releasing AR connections
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Mar 6, 2017
1 parent 1fc7829 commit 3322c65
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

## 0.4.4

- Fix bug with ActiveRecord connections (https://github.com/anycable/anycable/issues/9). ([@palkan][])

## 0.4.0

- Initial version. ([@palkan][])
Expand Down
2 changes: 1 addition & 1 deletion anycable-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "rails", "~> 5"
spec.add_dependency "anycable", "~>0.4.2"
spec.add_dependency "anycable", "~>0.4.4"

spec.add_development_dependency "bundler", "~> 1"
spec.add_development_dependency "rake", "~> 10.0"
Expand Down
5 changes: 3 additions & 2 deletions lib/anycable/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "anycable"
require "anycable/rails/version"
require "anycable/rails/actioncable/server"
require "anycable/rails/actioncable/connection"

module Anycable
# Rails handler for AnyCable
module Rails
require "anycable/rails/engine"
require "anycable/rails/actioncable/server"
require "anycable/rails/actioncable/connection"
end
end
28 changes: 28 additions & 0 deletions lib/anycable/rails/activerecord/release_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true
module Anycable
module Rails
module ActiveRecord
# Release ActiveRecord connection after every call (if any)
module ReleaseConnection
def connect(*)
wrap_release_connection { super }
end

def disconnect(*)
wrap_release_connection { super }
end

def command(*)
wrap_release_connection { super }
end

def wrap_release_connection
res = yield
::ActiveRecord::Base.connection_pool.release_connection if
::ActiveRecord::Base.connection_pool.active_connection?
res
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/anycable/rails/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true
module Anycable
module Rails
class Engine < ::Rails::Engine # :nodoc:
initializer "release AR connections in RPC handler" do |_app|
ActiveSupport.on_load(:active_record) do
require "anycable/rails/activerecord/release_connection"
Anycable::RPCHandler.prepend Anycable::Rails::ActiveRecord::ReleaseConnection
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/anycable/rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true
module Anycable
module Rails
VERSION = "0.4.3"
VERSION = "0.4.4"
end
end

0 comments on commit 3322c65

Please sign in to comment.