Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/redis_client/circuit_breaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def refresh_state

def record_error
now = RedisClient.now
expiry = now - @error_timeout
expiry = now - @error_threshold_timeout
@lock.synchronize do
if @state == :closed
@errors.reject! { |t| t < expiry }
Expand All @@ -100,6 +100,7 @@ def record_success

@successes += 1
if @successes >= @success_threshold
@errors.clear
@state = :closed
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/redis_client/circuit_breaker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ def test_open_circuit_after_consecutive_errors
assert_open @circuit_breaker
end

def test_track_errors_during_error_threshold_window
assert_closed @circuit_breaker
(@circuit_breaker.error_threshold - 1).times do
record_error @circuit_breaker
end

travel(@circuit_breaker.error_threshold_timeout - 0.01) do
record_error @circuit_breaker
assert_open @circuit_breaker
end
end

def test_allow_use_after_the_errors_timedout
open_circuit @circuit_breaker
assert_open @circuit_breaker
Expand Down