diff --git a/lib/makara/pool.rb b/lib/makara/pool.rb index a9df9d1d..145bfdc8 100644 --- a/lib/makara/pool.rb +++ b/lib/makara/pool.rb @@ -93,9 +93,13 @@ def send_to_all(method, *args, &block) # Provide a connection that is not blacklisted and connected. Handle any errors # that may occur within the block. def provide - provided_connection = self.next + if all_connections_blacklisted? + err = Makara::Errors::AllConnectionsBlacklisted.new(self, @blacklist_errors) + @blacklist_errors = [] + raise err + end - # nil implies that it's blacklisted + provided_connection = self.next if provided_connection value = @proxy.error_handler.handle(provided_connection) do @@ -105,12 +109,6 @@ def provide @blacklist_errors = [] value - - # if we've made any connections within this pool, we should report the blackout. - elsif connection_made? - err = Makara::Errors::AllConnectionsBlacklisted.new(self, @blacklist_errors) - @blacklist_errors = [] - raise err else raise Makara::Errors::NoConnectionsAvailable.new(@role) unless @disabled end @@ -122,16 +120,16 @@ def provide retry end - - protected - # have we connected to any of the underlying connections. def connection_made? @connections.any?(&:_makara_connected?) end + def all_connections_blacklisted? + @connections.all?(&:_makara_blacklisted?) + end # Get the next non-blacklisted connection. If the proxy is setup # to be sticky, provide back the current connection assuming it is diff --git a/spec/active_record/connection_adapters/makara_mysql2_adapter_spec.rb b/spec/active_record/connection_adapters/makara_mysql2_adapter_spec.rb index 27731e8e..80be8379 100644 --- a/spec/active_record/connection_adapters/makara_mysql2_adapter_spec.rb +++ b/spec/active_record/connection_adapters/makara_mysql2_adapter_spec.rb @@ -163,7 +163,7 @@ allow_any_instance_of(Makara::Strategies::RoundRobin).to receive(:single_one?){ true } Test::User.exists? # flush other (schema) things that need to happen - + con = connection.slave_pool.connections.first if (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR <= 0) expect(con).to receive(:execute).with(/SELECT\s+1\s*(AS one)?\s+FROM .?users.?\s+LIMIT\s+.?1/, any_args).once.and_call_original diff --git a/spec/active_record/connection_adapters/makara_postgis_adapter_spec.rb b/spec/active_record/connection_adapters/makara_postgis_adapter_spec.rb index b701bd86..30baa20b 100644 --- a/spec/active_record/connection_adapters/makara_postgis_adapter_spec.rb +++ b/spec/active_record/connection_adapters/makara_postgis_adapter_spec.rb @@ -118,8 +118,8 @@ allow(ActiveRecord::Base).to receive(:postgis_connection).and_raise(StandardError.new('could not connect to server: Connection refused')) ActiveRecord::Base.establish_connection(config) - expect { connection.execute('SELECT * FROM users') }.to raise_error(Makara::Errors::NoConnectionsAvailable) - expect { connection.execute('INSERT INTO users (name) VALUES (\'John\')') }.to raise_error(Makara::Errors::NoConnectionsAvailable) + expect { connection.execute('SELECT * FROM users') }.to raise_error(Makara::Errors::AllConnectionsBlacklisted) + expect { connection.execute('INSERT INTO users (name) VALUES (\'John\')') }.to raise_error(Makara::Errors::AllConnectionsBlacklisted) end end @@ -148,7 +148,7 @@ ActiveRecord::Base.establish_connection(custom_config) connection.execute('SELECT * FROM users') - expect { connection.execute('INSERT INTO users (name) VALUES (\'John\')') }.to raise_error(Makara::Errors::NoConnectionsAvailable) + expect { connection.execute('INSERT INTO users (name) VALUES (\'John\')') }.to raise_error(Makara::Errors::AllConnectionsBlacklisted) end end end diff --git a/spec/active_record/connection_adapters/makara_postgresql_adapter_spec.rb b/spec/active_record/connection_adapters/makara_postgresql_adapter_spec.rb index 6702c465..45f981d0 100644 --- a/spec/active_record/connection_adapters/makara_postgresql_adapter_spec.rb +++ b/spec/active_record/connection_adapters/makara_postgresql_adapter_spec.rb @@ -77,7 +77,7 @@ allow_any_instance_of(Makara::Strategies::RoundRobin).to receive(:single_one?){ true } Test::User.exists? # flush other (schema) things that need to happen - + con = connection.slave_pool.connections.first if (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 2) || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR <= 0) @@ -100,8 +100,8 @@ allow(ActiveRecord::Base).to receive(:postgresql_connection).and_raise(StandardError.new('could not connect to server: Connection refused')) ActiveRecord::Base.establish_connection(config) - expect { connection.execute('SELECT * FROM users') }.to raise_error(Makara::Errors::NoConnectionsAvailable) - expect { connection.execute('INSERT INTO users (name) VALUES (\'John\')') }.to raise_error(Makara::Errors::NoConnectionsAvailable) + expect { connection.execute('SELECT * FROM users') }.to raise_error(Makara::Errors::AllConnectionsBlacklisted) + expect { connection.execute('INSERT INTO users (name) VALUES (\'John\')') }.to raise_error(Makara::Errors::AllConnectionsBlacklisted) end end @@ -130,7 +130,7 @@ establish_connection(custom_config) connection.execute('SELECT * FROM users') - expect { connection.execute('INSERT INTO users (name) VALUES (\'John\')') }.to raise_error(Makara::Errors::NoConnectionsAvailable) + expect { connection.execute('INSERT INTO users (name) VALUES (\'John\')') }.to raise_error(Makara::Errors::AllConnectionsBlacklisted) end end