Class: Fractor::Workflow::CircuitBreaker
- Inherits:
-
Object
- Object
- Fractor::Workflow::CircuitBreaker
- Defined in:
- lib/fractor/workflow/circuit_breaker.rb
Overview
Circuit breaker implementation for fault tolerance
The circuit breaker has three states:
- Closed: Normal operation, requests pass through
- Open: Failure threshold exceeded, requests fail fast
- Half-Open: Testing if service recovered, limited requests allowed
Constant Summary collapse
- STATE_CLOSED =
Circuit breaker states
:closed- STATE_OPEN =
:open- STATE_HALF_OPEN =
:half_open
Instance Attribute Summary collapse
-
#failure_count ⇒ Object
readonly
Returns the value of attribute failure_count.
-
#half_open_calls ⇒ Object
readonly
Returns the value of attribute half_open_calls.
-
#last_failure_time ⇒ Object
readonly
Returns the value of attribute last_failure_time.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#threshold ⇒ Object
readonly
Returns the value of attribute threshold.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#call { ... } ⇒ Object
Execute a block with circuit breaker protection.
-
#closed? ⇒ Boolean
Check if circuit breaker is closed.
-
#half_open? ⇒ Boolean
Check if circuit breaker is half-open.
-
#initialize(threshold: 5, timeout: 60, half_open_calls: 3) ⇒ CircuitBreaker
constructor
Initialize a new circuit breaker.
-
#open? ⇒ Boolean
Check if circuit breaker is open.
-
#reset ⇒ Object
Reset the circuit breaker to closed state.
-
#stats ⇒ Hash
Get circuit breaker statistics.
Constructor Details
#initialize(threshold: 5, timeout: 60, half_open_calls: 3) ⇒ CircuitBreaker
Initialize a new circuit breaker
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/fractor/workflow/circuit_breaker.rb', line 31 def initialize(threshold: 5, timeout: 60, half_open_calls: 3) @threshold = threshold @timeout = timeout @half_open_calls = half_open_calls @state = STATE_CLOSED @failure_count = 0 @success_count = 0 @last_failure_time = nil @mutex = Mutex.new @just_transitioned_to_half_open = false end |
Instance Attribute Details
#failure_count ⇒ Object (readonly)
Returns the value of attribute failure_count.
23 24 25 |
# File 'lib/fractor/workflow/circuit_breaker.rb', line 23 def failure_count @failure_count end |
#half_open_calls ⇒ Object (readonly)
Returns the value of attribute half_open_calls.
23 24 25 |
# File 'lib/fractor/workflow/circuit_breaker.rb', line 23 def half_open_calls @half_open_calls end |
#last_failure_time ⇒ Object (readonly)
Returns the value of attribute last_failure_time.
23 24 25 |
# File 'lib/fractor/workflow/circuit_breaker.rb', line 23 def last_failure_time @last_failure_time end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
23 24 25 |
# File 'lib/fractor/workflow/circuit_breaker.rb', line 23 def state @state end |
#threshold ⇒ Object (readonly)
Returns the value of attribute threshold.
23 24 25 |
# File 'lib/fractor/workflow/circuit_breaker.rb', line 23 def threshold @threshold end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
23 24 25 |
# File 'lib/fractor/workflow/circuit_breaker.rb', line 23 def timeout @timeout end |
Instance Method Details
#call { ... } ⇒ Object
Execute a block with circuit breaker protection
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/fractor/workflow/circuit_breaker.rb', line 48 def call(&) check_state if open? raise CircuitOpenError, "Circuit breaker is open (#{failure_count} failures)" end execute_with_breaker(&) end |
#closed? ⇒ Boolean
Check if circuit breaker is closed
62 63 64 |
# File 'lib/fractor/workflow/circuit_breaker.rb', line 62 def closed? state == STATE_CLOSED end |
#half_open? ⇒ Boolean
Check if circuit breaker is half-open
76 77 78 |
# File 'lib/fractor/workflow/circuit_breaker.rb', line 76 def half_open? state == STATE_HALF_OPEN end |
#open? ⇒ Boolean
Check if circuit breaker is open
69 70 71 |
# File 'lib/fractor/workflow/circuit_breaker.rb', line 69 def open? state == STATE_OPEN end |
#reset ⇒ Object
Reset the circuit breaker to closed state
81 82 83 84 85 86 87 88 |
# File 'lib/fractor/workflow/circuit_breaker.rb', line 81 def reset @mutex.synchronize do @state = STATE_CLOSED @failure_count = 0 @success_count = 0 @last_failure_time = nil end end |
#stats ⇒ Hash
Get circuit breaker statistics
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/fractor/workflow/circuit_breaker.rb', line 93 def stats { state: state, failure_count: failure_count, success_count: @success_count, last_failure_time: last_failure_time, threshold: threshold, timeout: timeout, } end |