Class: AgentHarness::Orchestration::CircuitBreaker

Inherits:
Object
  • Object
show all
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

Examples:

breaker = CircuitBreaker.new(failure_threshold: 5, timeout: 300)
breaker.record_failure
breaker.open? # => false (still below threshold)

Constant Summary collapse

STATES =
[:closed, :open, :half_open].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, failure_threshold: nil, timeout: nil, half_open_max_calls: nil) ⇒ CircuitBreaker

Create a new circuit breaker

Parameters:

  • config (CircuitBreakerConfig, nil) (defaults to: nil)

    configuration object

  • failure_threshold (Integer) (defaults to: nil)

    failures before opening

  • timeout (Integer) (defaults to: nil)

    seconds before half-open transition

  • half_open_max_calls (Integer) (defaults to: nil)

    successful calls to close



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_countObject (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

#stateObject (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_countObject (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)

Returns:

  • (Boolean)

    true if closed



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)

Returns:

  • (Boolean)

    true if half-open



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)

Returns:

  • (Boolean)

    true if open



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_failurevoid

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_successvoid

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

#statusHash

Get circuit status

Returns:

  • (Hash)

    status information



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_recoveryInteger?

Get time until circuit attempts recovery

Returns:

  • (Integer, nil)

    seconds until half-open, or nil if not open



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