Class: BreakerMachines::Storage::BackendState
- Inherits:
-
Object
- Object
- BreakerMachines::Storage::BackendState
- Defined in:
- lib/breaker_machines/storage/backend_state.rb
Overview
Manages the health state of a single storage backend using a state machine.
Instance Attribute Summary collapse
-
#failure_count ⇒ Object
readonly
Returns the value of attribute failure_count.
-
#health ⇒ Object
Returns the value of attribute health.
-
#last_failure_at ⇒ Object
readonly
Returns the value of attribute last_failure_at.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name, threshold:, timeout:) ⇒ BackendState
constructor
A new instance of BackendState.
- #record_failure ⇒ Object
- #threshold_reached? ⇒ Boolean
- #unhealthy_due_to_timeout? ⇒ Boolean
Constructor Details
#initialize(name, threshold:, timeout:) ⇒ BackendState
Returns a new instance of BackendState.
10 11 12 13 14 15 16 17 |
# File 'lib/breaker_machines/storage/backend_state.rb', line 10 def initialize(name, threshold:, timeout:) @name = name @threshold = threshold @timeout = timeout @failure_count = 0 @last_failure_at = nil @health = :healthy end |
Instance Attribute Details
#failure_count ⇒ Object (readonly)
Returns the value of attribute failure_count.
7 8 9 |
# File 'lib/breaker_machines/storage/backend_state.rb', line 7 def failure_count @failure_count end |
#health ⇒ Object
Returns the value of attribute health.
8 9 10 |
# File 'lib/breaker_machines/storage/backend_state.rb', line 8 def health @health end |
#last_failure_at ⇒ Object (readonly)
Returns the value of attribute last_failure_at.
7 8 9 |
# File 'lib/breaker_machines/storage/backend_state.rb', line 7 def last_failure_at @last_failure_at end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/breaker_machines/storage/backend_state.rb', line 7 def name @name end |
Instance Method Details
#record_failure ⇒ Object
44 45 46 47 48 |
# File 'lib/breaker_machines/storage/backend_state.rb', line 44 def record_failure @failure_count += 1 @last_failure_at = BreakerMachines.monotonic_time trip end |
#threshold_reached? ⇒ Boolean
50 51 52 |
# File 'lib/breaker_machines/storage/backend_state.rb', line 50 def threshold_reached? @failure_count >= @threshold end |
#unhealthy_due_to_timeout? ⇒ Boolean
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/breaker_machines/storage/backend_state.rb', line 54 def unhealthy_due_to_timeout? return false unless unhealthy? unhealthy_until = instance_variable_get(:@unhealthy_until) return false unless unhealthy_until if BreakerMachines.monotonic_time > unhealthy_until recover false else true end end |