Skip to content

Commit 4d38b9c

Browse files
authored
Merge pull request #247 from izuzak/izuzak/circuit-breaker-fixes
Two fixes for circuit breaker logic
2 parents a80505d + 8161a38 commit 4d38b9c

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/redis_client/circuit_breaker.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def refresh_state
7979

8080
def record_error
8181
now = RedisClient.now
82-
expiry = now - @error_timeout
82+
expiry = now - @error_threshold_timeout
8383
@lock.synchronize do
8484
if @state == :closed
8585
@errors.reject! { |t| t < expiry }
@@ -100,6 +100,7 @@ def record_success
100100

101101
@successes += 1
102102
if @successes >= @success_threshold
103+
@errors.clear
103104
@state = :closed
104105
end
105106
end

test/redis_client/circuit_breaker_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ def test_open_circuit_after_consecutive_errors
2121
assert_open @circuit_breaker
2222
end
2323

24+
def test_track_errors_during_error_threshold_window
25+
assert_closed @circuit_breaker
26+
(@circuit_breaker.error_threshold - 1).times do
27+
record_error @circuit_breaker
28+
end
29+
30+
travel(@circuit_breaker.error_threshold_timeout - 0.01) do
31+
record_error @circuit_breaker
32+
assert_open @circuit_breaker
33+
end
34+
end
35+
2436
def test_allow_use_after_the_errors_timedout
2537
open_circuit @circuit_breaker
2638
assert_open @circuit_breaker

0 commit comments

Comments
 (0)