Skip to content

Commit

Permalink
Handle infinite request retry on connection error
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Klimenko committed Nov 18, 2017
1 parent d7f0e22 commit 0d8101b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/clickhouse/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ def initialize(config)
private

def method_missing(*args, &block)
pond.checkout do |connection|
connection.send(*args, &block)
trys_left = pond.available.count
begin
pond.checkout do |connection|
trys_left -= 1
connection.send(*args, &block)
end
rescue ::Clickhouse::ConnectionError
retry if pond.available.any? && trys_left.positive?
end
rescue ::Clickhouse::ConnectionError
retry if pond.available.any?
end

end
end
10 changes: 10 additions & 0 deletions test/unit/connection/test_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ class TestCluser < MiniTest::Test
), cluster.pond.available.collect(&:url)
end
end

describe "when timeout error gets raised while running the query" do
it "does not repeat request more then cluster's size times" do
Clickhouse::Connection.any_instance.expects(:ping!).at_least_once
Clickhouse::Connection.any_instance.stubs(:get).at_most(3).raises(Clickhouse::RequestTimedOut)

cluster = Clickhouse::Cluster.new :urls => %w(http://localhost:1234 http://localhost:1235 http://localhost:1236)
cluster.get "SELECT 1"
end
end
end

end
Expand Down

0 comments on commit 0d8101b

Please sign in to comment.