Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: define connection holder more thread-safe #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lib/standby/connection_holder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ def activate(target)
end

class << self
MUTEX = Mutex.new

def connection_holder(target)
klass_name = "Standby#{target.to_s.camelize}ConnectionHolder"
standby_connections[klass_name] ||= begin
klass = Class.new(Standby::ConnectionHolder) do
self.abstract_class = true
standby_connections[klass_name] || MUTEX.synchronize do
standby_connections[klass_name] ||= begin
klass = Class.new(Standby::ConnectionHolder) do
self.abstract_class = true
end
Object.const_set(klass_name, klass)
klass.activate(target)
klass
end
Object.const_set(klass_name, klass)
klass.activate(target)
klass
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/slavery_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,27 @@ class ActiveRecord::Relation
expect(User.on_standby(:two).where(nil).to_a.size).to be 0
expect(User.on_standby.where(nil).to_a.size).to be 1
end

it 'works concurrently when connection is not established beforehand' do
threads = 50.times.map do |i|
Thread.new do
begin
# Wrapping with "with_connection" as on_standby checks if we are
# inside transaction using default connection of ActiveRecord::Base.
ActiveRecord::Base.connection_pool.with_connection do
Standby.on_standby(:three) do
ActiveRecord::Base.connection.exec_query "SELECT 1;"
end
end
i
ensure
# Checking the used connection back into connection_pool.
StandbyStandbyThreeConnectionHolder.connection_pool.release_connection if StandbyStandbyThreeConnectionHolder.connection_pool.active_connection?
end
end
end

threads.map(&:join)
expect(threads.map(&:value)).to match_array threads.size.times.to_a
end
end
7 changes: 4 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
require 'standby'

ActiveRecord::Base.configurations = {
'test' => { 'adapter' => 'sqlite3', 'database' => 'test_db' },
'test_standby' => { 'adapter' => 'sqlite3', 'database' => 'test_standby_one' },
'test_standby_two' => { 'adapter' => 'sqlite3', 'database' => 'test_standby_two'},
'test' => { 'adapter' => 'sqlite3', 'database' => 'test_db', 'pool' => 30 },
'test_standby' => { 'adapter' => 'sqlite3', 'database' => 'test_standby_one', 'pool' => 30 },
'test_standby_two' => { 'adapter' => 'sqlite3', 'database' => 'test_standby_two', 'pool' => 30 },
'test_standby_three' => { 'adapter' => 'postgresql', 'database' => 'test_standby_three', 'pool' => 10, 'checkout_timeout' => 1 },
'test_standby_url' => 'postgres://root:@localhost:5432/test_standby'
}

Expand Down
3 changes: 2 additions & 1 deletion standby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'activerecord', '>= 3.0.0'

gem.add_development_dependency 'rspec'
gem.add_development_dependency 'sqlite3'
gem.add_development_dependency 'sqlite3', '~> 1.3', '< 1.4'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to update: test environment doesn't see sqlite adapter with sqlite 1.4.

gem.add_development_dependency 'pg'
end