Class: AgentHarness::Orchestration::CircuitBreaker
- Inherits:
-
Object
- Object
- AgentHarness::Orchestration::CircuitBreaker
- Defined in:
- lib/agent_harness/orchestration/circuit_breaker.rb
Overview
Circuit breaker for provider fault tolerance
Implements the circuit breaker pattern to prevent cascading failures. The circuit has three states:
-
:closed - Normal operation, requests pass through
-
:open - Failures exceeded threshold, requests are blocked
-
:half_open - After timeout, limited requests allowed to test recovery
Constant Summary collapse
- STATES =
[:closed, :open, :half_open].freeze
Instance Attribute Summary collapse
-
#failure_count ⇒ Object
readonly
Returns the value of attribute failure_count.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#success_count ⇒ Object
readonly
Returns the value of attribute success_count.
Instance Method Summary collapse
-
#closed? ⇒ Boolean
Check if circuit is closed (allowing requests).
-
#half_open? ⇒ Boolean
Check if circuit is half-open (testing recovery).
-
#initialize(config = nil, failure_threshold: nil, timeout: nil, half_open_max_calls: nil) ⇒ CircuitBreaker
constructor
Create a new circuit breaker.
-
#open? ⇒ Boolean
Check if circuit is open (blocking requests).
-
#record_failure ⇒ void
Record a failed call.
-
#record_success ⇒ void
Record a successful call.
-
#reset! ⇒ void
Reset the circuit breaker to initial state.
-
#status ⇒ Hash
Get circuit status.
-
#time_until_recovery ⇒ Integer?
Get time until circuit attempts recovery.
Constructor Details
#initialize(config = nil, failure_threshold: nil, timeout: nil, half_open_max_calls: nil) ⇒ CircuitBreaker
Create a new circuit breaker
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/agent_harness/orchestration/circuit_breaker.rb', line 28 def initialize(config = nil, failure_threshold: nil, timeout: nil, half_open_max_calls: nil) if config @enabled = config.enabled @failure_threshold = config.failure_threshold @timeout = config.timeout @half_open_max_calls = config.half_open_max_calls else @enabled = true @failure_threshold = failure_threshold || 5 @timeout = timeout || 300 @half_open_max_calls = half_open_max_calls || 3 end reset! end |
Instance Attribute Details
#failure_count ⇒ Object (readonly)
Returns the value of attribute failure_count.
20 21 22 |
# File 'lib/agent_harness/orchestration/circuit_breaker.rb', line 20 def failure_count @failure_count end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
20 21 22 |
# File 'lib/agent_harness/orchestration/circuit_breaker.rb', line 20 def state @state end |
#success_count ⇒ Object (readonly)
Returns the value of attribute success_count.
20 21 22 |
# File 'lib/agent_harness/orchestration/circuit_breaker.rb', line 20 def success_count @success_count end |
Instance Method Details
#closed? ⇒ Boolean
Check if circuit is closed (allowing requests)
60 61 62 |
# File 'lib/agent_harness/orchestration/circuit_breaker.rb', line 60 def closed? @state == :closed end |
#half_open? ⇒ Boolean
Check if circuit is half-open (testing recovery)
67 68 69 |
# File 'lib/agent_harness/orchestration/circuit_breaker.rb', line 67 def half_open? @state == :half_open end |
#open? ⇒ Boolean
Check if circuit is open (blocking requests)
47 48 49 50 51 52 53 54 55 |
# File 'lib/agent_harness/orchestration/circuit_breaker.rb', line 47 def open? return false unless @enabled if @state == :open && timeout_elapsed? transition_to(:half_open) end @state == :open end |
#record_failure ⇒ void
This method returns an undefined value.
Record a failed call
87 88 89 90 91 92 93 94 95 |
# File 'lib/agent_harness/orchestration/circuit_breaker.rb', line 87 def record_failure @mutex.synchronize do @failure_count += 1 if @failure_count >= @failure_threshold transition_to(:open) end end end |
#record_success ⇒ void
This method returns an undefined value.
Record a successful call
74 75 76 77 78 79 80 81 82 |
# File 'lib/agent_harness/orchestration/circuit_breaker.rb', line 74 def record_success @mutex.synchronize do @success_count += 1 if @state == :half_open && @success_count >= @half_open_max_calls transition_to(:closed) end end end |
#reset! ⇒ void
This method returns an undefined value.
Reset the circuit breaker to initial state
100 101 102 103 104 105 106 |
# File 'lib/agent_harness/orchestration/circuit_breaker.rb', line 100 def reset! @mutex = Mutex.new @state = :closed @failure_count = 0 @success_count = 0 @opened_at = nil end |
#status ⇒ Hash
Get circuit status
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/agent_harness/orchestration/circuit_breaker.rb', line 121 def status { state: @state, failure_count: @failure_count, success_count: @success_count, failure_threshold: @failure_threshold, timeout: @timeout, time_until_recovery: time_until_recovery, enabled: @enabled } end |
#time_until_recovery ⇒ Integer?
Get time until circuit attempts recovery
111 112 113 114 115 116 |
# File 'lib/agent_harness/orchestration/circuit_breaker.rb', line 111 def time_until_recovery return nil unless @state == :open && @opened_at remaining = @timeout - (Time.now - @opened_at) remaining.positive? ? remaining.to_i : 0 end |