Class: Fractor::Workflow::CircuitBreaker

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

Examples:

Basic usage

breaker = CircuitBreaker.new(threshold: 5, timeout: 60)
breaker.call do
  # Risky operation
end

Constant Summary collapse

STATE_CLOSED =

Circuit breaker states

:closed
STATE_OPEN =
:open
STATE_HALF_OPEN =
:half_open

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(threshold: 5, timeout: 60, half_open_calls: 3) ⇒ CircuitBreaker

Initialize a new circuit breaker

Parameters:

  • threshold (Integer) (defaults to: 5)

    Number of failures before opening circuit

  • timeout (Integer) (defaults to: 60)

    Seconds to wait before trying half-open

  • half_open_calls (Integer) (defaults to: 3)

    Number of test calls in half-open



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

#stateObject (readonly)

Returns the value of attribute state.



23
24
25
# File 'lib/fractor/workflow/circuit_breaker.rb', line 23

def state
  @state
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



23
24
25
# File 'lib/fractor/workflow/circuit_breaker.rb', line 23

def threshold
  @threshold
end

#timeoutObject (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

Yields:

  • Block to execute

Returns:

  • (Object)

    Result of the block

Raises:



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

Returns:

  • (Boolean)

    True if 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

Returns:

  • (Boolean)

    True if 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

Returns:

  • (Boolean)

    True if open



69
70
71
# File 'lib/fractor/workflow/circuit_breaker.rb', line 69

def open?
  state == STATE_OPEN
end

#resetObject

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

#statsHash

Get circuit breaker statistics

Returns:

  • (Hash)

    Statistics including state, counts, and timing



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