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

Don’t try to connect to blacklisted resources #202

Open
wants to merge 3 commits 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
20 changes: 9 additions & 11 deletions lib/makara/pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down