Class: ComplyanceSDK::Retry::CircuitBreaker

Inherits:
Object
  • Object
show all
Defined in:
lib/complyance_sdk/retry/circuit_breaker.rb

Overview

Circuit breaker pattern implementation Prevents cascading failures by opening the circuit when too many failures occur

Defined Under Namespace

Modules: State

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ CircuitBreaker

Initialize a new circuit breaker

Parameters:

  • config (Hash) (defaults to: {})

    Circuit breaker configuration

Options Hash (config):

  • :failure_threshold (Integer)

    Number of failures before opening (default: 5)

  • :timeout_seconds (Integer)

    Timeout before attempting reset (default: 60)

  • :success_threshold (Integer)

    Successes needed to close from half-open (default: 1)



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/complyance_sdk/retry/circuit_breaker.rb', line 23

def initialize(config = {})
  @config = {
    failure_threshold: 5,
    timeout_seconds: 60,
    success_threshold: 1
  }.merge(config)

  @state = State::CLOSED
  @failure_count = 0
  @last_failure_time = nil
  @success_count = 0
  @mutex = Mutex.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/complyance_sdk/retry/circuit_breaker.rb', line 15

def config
  @config
end

#failure_countObject (readonly)

Returns the value of attribute failure_count.



15
16
17
# File 'lib/complyance_sdk/retry/circuit_breaker.rb', line 15

def failure_count
  @failure_count
end

#last_failure_timeObject (readonly)

Returns the value of attribute last_failure_time.



15
16
17
# File 'lib/complyance_sdk/retry/circuit_breaker.rb', line 15

def last_failure_time
  @last_failure_time
end

#stateObject (readonly)

Returns the value of attribute state.



15
16
17
# File 'lib/complyance_sdk/retry/circuit_breaker.rb', line 15

def state
  @state
end

Instance Method Details

#closed?Boolean

Check if circuit breaker is closed

Returns:

  • (Boolean)

    True if closed



82
83
84
# File 'lib/complyance_sdk/retry/circuit_breaker.rb', line 82

def closed?
  @state == State::CLOSED
end

#execute { ... } ⇒ Object

Execute a block with circuit breaker protection

Yields:

  • The block to execute

Returns:

  • The result of the block

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/complyance_sdk/retry/circuit_breaker.rb', line 42

def execute
  @mutex.synchronize do
    case @state
    when State::OPEN
      if should_attempt_reset?
        @state = State::HALF_OPEN
        @success_count = 0
      else
        raise ComplyanceSDK::Exceptions::CircuitBreakerOpenError.new(
          'Circuit breaker is open',
          context: {
            failure_count: @failure_count,
            last_failure_time: @last_failure_time,
            timeout_seconds: @config[:timeout_seconds]
          }
        )
      end
    end
  end

  begin
    result = yield
    on_success
    result
  rescue => e
    on_failure
    raise e
  end
end

#half_open?Boolean

Check if circuit breaker is half-open

Returns:

  • (Boolean)

    True if half-open



89
90
91
# File 'lib/complyance_sdk/retry/circuit_breaker.rb', line 89

def half_open?
  @state == State::HALF_OPEN
end

#open?Boolean

Check if circuit breaker is open

Returns:

  • (Boolean)

    True if open



75
76
77
# File 'lib/complyance_sdk/retry/circuit_breaker.rb', line 75

def open?
  @state == State::OPEN
end

#reset!Object

Reset the circuit breaker to closed state



107
108
109
110
111
112
113
114
# File 'lib/complyance_sdk/retry/circuit_breaker.rb', line 107

def reset!
  @mutex.synchronize do
    @state = State::CLOSED
    @failure_count = 0
    @last_failure_time = nil
    @success_count = 0
  end
end

#statsHash

Get circuit breaker statistics

Returns:

  • (Hash)

    Statistics hash



96
97
98
99
100
101
102
103
104
# File 'lib/complyance_sdk/retry/circuit_breaker.rb', line 96

def stats
  {
    state: @state,
    failure_count: @failure_count,
    last_failure_time: @last_failure_time,
    success_count: @success_count,
    config: @config
  }
end