Class: Vmpooler::CircuitBreaker
- Inherits:
-
Object
- Object
- Vmpooler::CircuitBreaker
- Defined in:
- lib/vmpooler/circuit_breaker.rb
Overview
Circuit breaker pattern implementation to prevent cascading failures when a provider becomes unresponsive or experiences repeated failures.
States:
- CLOSED: Normal operation, requests flow through
- OPEN: Provider is failing, reject requests immediately (fail fast)
- HALF_OPEN: Testing if provider has recovered with limited requests
Defined Under Namespace
Classes: CircuitOpenError
Constant Summary collapse
- STATES =
i[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
-
#allow_request? ⇒ Boolean
Check if circuit allows requests.
-
#call { ... } ⇒ Object
Execute a block with circuit breaker protection.
-
#initialize(name:, logger:, metrics:, failure_threshold: 5, timeout: 30, half_open_attempts: 3) ⇒ CircuitBreaker
constructor
Initialize a new circuit breaker.
-
#status ⇒ Hash
Get current circuit breaker status.
Constructor Details
#initialize(name:, logger:, metrics:, failure_threshold: 5, timeout: 30, half_open_attempts: 3) ⇒ CircuitBreaker
Initialize a new circuit breaker
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/vmpooler/circuit_breaker.rb', line 26 def initialize(name:, logger:, metrics:, failure_threshold: 5, timeout: 30, half_open_attempts: 3) @name = name @logger = logger @metrics = metrics @failure_threshold = failure_threshold @timeout = timeout @half_open_attempts = half_open_attempts @state = :closed @failure_count = 0 @success_count = 0 @last_failure_time = nil @mutex = Mutex.new end |
Instance Attribute Details
#failure_count ⇒ Object (readonly)
Returns the value of attribute failure_count.
16 17 18 |
# File 'lib/vmpooler/circuit_breaker.rb', line 16 def failure_count @failure_count end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
16 17 18 |
# File 'lib/vmpooler/circuit_breaker.rb', line 16 def state @state end |
#success_count ⇒ Object (readonly)
Returns the value of attribute success_count.
16 17 18 |
# File 'lib/vmpooler/circuit_breaker.rb', line 16 def success_count @success_count end |
Instance Method Details
#allow_request? ⇒ Boolean
Check if circuit allows requests
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/vmpooler/circuit_breaker.rb', line 61 def allow_request? @mutex.synchronize do case @state when :closed true when :half_open true when :open if should_attempt_reset? true else false end end end end |
#call { ... } ⇒ Object
Execute a block with circuit breaker protection
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/vmpooler/circuit_breaker.rb', line 46 def call check_state begin result = yield on_success result rescue StandardError => e on_failure(e) raise end end |
#status ⇒ Hash
Get current circuit breaker status
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/vmpooler/circuit_breaker.rb', line 80 def status @mutex.synchronize do { name: @name, state: @state, failure_count: @failure_count, success_count: @success_count, last_failure_time: @last_failure_time, next_retry_time: next_retry_time } end end |