Skip to content

Commit

Permalink
Raise a useful error when reconnecting with a circuit breaker (#145)
Browse files Browse the repository at this point in the history
* Raise a useful error when reconnecting with a circuit breaker

If you have reconnects enabled, and a circuit breaker configured,
it's useful to raise the actual underlying error when we actually
attempted the connection instead of simply the circuit breaker
error. The circuit breaker error will still be raised if no
attempts were tried.

* Use descriptive name for error.

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>

* Tweak preferred_error assignment

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>

---------

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
  • Loading branch information
ccutrer and byroot committed Nov 10, 2023
1 parent 531f125 commit 3a1b4f8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/redis_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ def ensure_connected(retryable: true)
elsif retryable
tries = 0
connection = nil
preferred_error = nil
begin
connection = raw_connection
if block_given?
Expand All @@ -677,13 +678,15 @@ def ensure_connected(retryable: true)
connection
end
rescue ConnectionError, ProtocolError => error
preferred_error ||= error
preferred_error = error unless error.is_a?(CircuitBreaker::OpenCircuitError)
close

if !@disable_reconnection && config.retry_connecting?(tries, error)
tries += 1
retry
else
raise
raise preferred_error
end
end
else
Expand Down
12 changes: 12 additions & 0 deletions test/redis_client/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ def test_circuit_breaker
end
end

def test_circuit_breaker_initial_connection_exception
circuit_breaker = CircuitBreaker.new(error_threshold: 1, success_threshold: 1, error_timeout: 1)
@redis = new_client(circuit_breaker: circuit_breaker, reconnect_attempts: 2)

Toxiproxy[/redis/].down do
error = assert_raises CannotConnectError do
@redis.call("PING")
end
refute_instance_of CircuitBreaker::OpenCircuitError, error
end
end

def test_killed_connection
client = new_client(reconnect_attempts: 1, id: "background")

Expand Down

0 comments on commit 3a1b4f8

Please # to comment.