Class: CircuitBreaker::Shield
- Inherits:
-
Object
- Object
- CircuitBreaker::Shield
- Defined in:
- lib/circuit_breaker-ruby/shield.rb
Defined Under Namespace
Modules: States
Instance Attribute Summary collapse
-
#failure_count ⇒ Object
readonly
Returns the value of attribute failure_count.
-
#failure_threshold ⇒ Object
readonly
Returns the value of attribute failure_threshold.
-
#failure_threshold_percentage ⇒ Object
readonly
Returns the value of attribute failure_threshold_percentage.
-
#invocation_timeout ⇒ Object
readonly
Returns the value of attribute invocation_timeout.
-
#retry_timeout ⇒ Object
readonly
Returns the value of attribute retry_timeout.
-
#total_count ⇒ Object
readonly
Returns the value of attribute total_count.
Instance Method Summary collapse
- #config ⇒ Object
-
#initialize(**options) ⇒ Shield
constructor
A new instance of Shield.
- #protect(options = {}, &block) ⇒ Object
- #update_config!(options) ⇒ Object
Constructor Details
#initialize(**options) ⇒ Shield
Returns a new instance of Shield.
16 17 18 19 20 21 22 23 24 |
# File 'lib/circuit_breaker-ruby/shield.rb', line 16 def initialize(**) @failure_count = 0 @total_count = 0 @failure_threshold = .fetch(:failure_threshold, config.failure_threshold) @failure_threshold_percentage = .fetch(:failure_threshold_percentage, config.failure_threshold_percentage) @invocation_timeout = .fetch(:invocation_timeout, config.invocation_timeout) @retry_timeout = .fetch(:retry_timeout, config.retry_timeout) @callback = [:callback] end |
Instance Attribute Details
#failure_count ⇒ Object (readonly)
Returns the value of attribute failure_count.
9 10 11 |
# File 'lib/circuit_breaker-ruby/shield.rb', line 9 def failure_count @failure_count end |
#failure_threshold ⇒ Object (readonly)
Returns the value of attribute failure_threshold.
9 10 11 |
# File 'lib/circuit_breaker-ruby/shield.rb', line 9 def failure_threshold @failure_threshold end |
#failure_threshold_percentage ⇒ Object (readonly)
Returns the value of attribute failure_threshold_percentage.
9 10 11 |
# File 'lib/circuit_breaker-ruby/shield.rb', line 9 def failure_threshold_percentage @failure_threshold_percentage end |
#invocation_timeout ⇒ Object (readonly)
Returns the value of attribute invocation_timeout.
9 10 11 |
# File 'lib/circuit_breaker-ruby/shield.rb', line 9 def invocation_timeout @invocation_timeout end |
#retry_timeout ⇒ Object (readonly)
Returns the value of attribute retry_timeout.
9 10 11 |
# File 'lib/circuit_breaker-ruby/shield.rb', line 9 def retry_timeout @retry_timeout end |
#total_count ⇒ Object (readonly)
Returns the value of attribute total_count.
9 10 11 |
# File 'lib/circuit_breaker-ruby/shield.rb', line 9 def total_count @total_count end |
Instance Method Details
#config ⇒ Object
26 27 28 |
# File 'lib/circuit_breaker-ruby/shield.rb', line 26 def config CircuitBreaker.config end |
#protect(options = {}, &block) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/circuit_breaker-ruby/shield.rb', line 34 def protect( = {}, &block) update_config!() case prev_state = state when States::CLOSED, States::HALF_OPEN connect(&block) when States::OPEN raise CircuitBreaker::Open end end |
#update_config!(options) ⇒ Object
30 31 32 |
# File 'lib/circuit_breaker-ruby/shield.rb', line 30 def update_config!() CircuitBreaker::Config.update(self, ) end |