@@ -16,10 +16,10 @@ module States
16
16
def initialize ( **options )
17
17
@failure_count = 0
18
18
@total_count = 0
19
- @failure_threshold = options [ :failure_threshold ] || config . failure_threshold
20
- @failure_threshold_percentage = options [ :failure_threshold_percentage ] || config . failure_threshold_percentage
21
- @invocation_timeout = options [ :invocation_timeout ] || config . invocation_timeout
22
- @retry_timeout = options [ :retry_timeout ] || config . retry_timeout
19
+ @failure_threshold = options . fetch ( :failure_threshold , config . failure_threshold )
20
+ @failure_threshold_percentage = options . fetch ( :failure_threshold_percentage , config . failure_threshold_percentage )
21
+ @invocation_timeout = options . fetch ( :invocation_timeout , config . invocation_timeout )
22
+ @retry_timeout = options . fetch ( :retry_timeout , config . retry_timeout )
23
23
@callback = options [ :callback ]
24
24
end
25
25
@@ -28,17 +28,15 @@ def config
28
28
end
29
29
30
30
def update_config! ( options )
31
- ( CircuitBreaker ::Config ::UPDATABLE & options . keys ) . each do |variable |
32
- instance_variable_set ( "@#{ variable } " , options [ variable ] )
33
- end
31
+ CircuitBreaker ::Config . update ( self , options )
34
32
end
35
33
36
34
def protect ( options = { } , &block )
37
35
update_config! ( options )
38
36
39
37
case prev_state = state
40
38
when States ::CLOSED , States ::HALF_OPEN
41
- connect ( prev_state , &block )
39
+ connect ( &block )
42
40
when States ::OPEN
43
41
raise CircuitBreaker ::Open
44
42
end
@@ -47,21 +45,21 @@ def protect(options = {}, &block)
47
45
private
48
46
49
47
def state
50
- if reached_failure_threshold? && reached_retry_timeout?
48
+ if reached_failure_threshold? && reached_failure_threshold_percentage? && reached_retry_timeout?
51
49
States ::HALF_OPEN
52
- elsif reached_failure_threshold?
50
+ elsif reached_failure_threshold? && reached_failure_threshold_percentage?
53
51
States ::OPEN
54
52
else
55
53
States ::CLOSED
56
54
end
57
55
end
58
56
59
57
def reached_failure_threshold?
60
- @_reached_failure_threshold ||= begin
61
- ( failure_count >= failure_threshold ) &&
62
- ( total_count != 0 &&
63
- ( failure_count . to_f / total_count . to_f ) >= failure_threshold_percentage )
64
- end
58
+ ( failure_count >= failure_threshold )
59
+ end
60
+
61
+ def reached_failure_threshold_percentage?
62
+ ( total_count . nonzero? && ( failure_count . to_f / total_count . to_f ) >= failure_threshold_percentage )
65
63
end
66
64
67
65
def reached_retry_timeout?
@@ -74,7 +72,7 @@ def reset
74
72
@state = States ::CLOSED
75
73
end
76
74
77
- def connect ( prev_state , &block )
75
+ def connect ( &block )
78
76
begin
79
77
result = nil
80
78
::Timeout ::timeout ( invocation_timeout ) do
@@ -89,13 +87,13 @@ def connect(prev_state, &block)
89
87
invoke_callback
90
88
raise CircuitBreaker ::TimeoutError
91
89
ensure
92
- increment_total_count ( prev_state )
90
+ increment_total_count
93
91
end
94
92
95
93
result
96
94
end
97
95
98
- def increment_total_count ( state )
96
+ def increment_total_count
99
97
@total_count += 1
100
98
end
101
99
0 commit comments