Class: ComplyanceSDK::Retry::CircuitBreaker
- Inherits:
-
Object
- Object
- ComplyanceSDK::Retry::CircuitBreaker
- 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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#failure_count ⇒ Object
readonly
Returns the value of attribute failure_count.
-
#last_failure_time ⇒ Object
readonly
Returns the value of attribute last_failure_time.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
-
#closed? ⇒ Boolean
Check if circuit breaker is closed.
-
#execute { ... } ⇒ Object
Execute a block with circuit breaker protection.
-
#half_open? ⇒ Boolean
Check if circuit breaker is half-open.
-
#initialize(config = {}) ⇒ 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(config = {}) ⇒ CircuitBreaker
Initialize a new circuit breaker
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
#config ⇒ Object (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_count ⇒ Object (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_time ⇒ Object (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 |
#state ⇒ Object (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
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
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
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
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 |
#stats ⇒ Hash
Get circuit breaker statistics
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 |