Class: CB2::Breaker
- Inherits:
-
Object
- Object
- CB2::Breaker
- Defined in:
- lib/cb2/breaker.rb
Instance Attribute Summary collapse
-
#service ⇒ Object
Returns the value of attribute service.
-
#strategy ⇒ Object
Returns the value of attribute strategy.
Instance Method Summary collapse
-
#initialize(options) ⇒ Breaker
constructor
A new instance of Breaker.
- #initialize_strategy(options) ⇒ Object
- #open? ⇒ Boolean
- #process_count ⇒ Object
- #process_error ⇒ Object
- #process_success ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(options) ⇒ Breaker
Returns a new instance of Breaker.
4 5 6 7 |
# File 'lib/cb2/breaker.rb', line 4 def initialize() @service = [:service] || "default" @strategy = initialize_strategy() end |
Instance Attribute Details
#service ⇒ Object
Returns the value of attribute service.
2 3 4 |
# File 'lib/cb2/breaker.rb', line 2 def service @service end |
#strategy ⇒ Object
Returns the value of attribute strategy.
2 3 4 |
# File 'lib/cb2/breaker.rb', line 2 def strategy @strategy end |
Instance Method Details
#initialize_strategy(options) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/cb2/breaker.rb', line 47 def initialize_strategy() = .dup.merge(service: self.service) if [:strategy].respond_to?(:open?) return [:strategy].new() end case [:strategy].to_s when "", "percentage" CB2::Percentage.new() when "rolling_window" CB2::RollingWindow.new() when "stub" CB2::Stub.new() end end |
#open? ⇒ Boolean
26 27 28 29 30 |
# File 'lib/cb2/breaker.rb', line 26 def open? strategy.open? rescue Redis::BaseError false end |
#process_count ⇒ Object
32 33 34 35 |
# File 'lib/cb2/breaker.rb', line 32 def process_count strategy.count if strategy.respond_to?(:count) rescue Redis::BaseError end |
#process_error ⇒ Object
42 43 44 45 |
# File 'lib/cb2/breaker.rb', line 42 def process_error strategy.error if strategy.respond_to?(:error) rescue Redis::BaseError end |
#process_success ⇒ Object
37 38 39 40 |
# File 'lib/cb2/breaker.rb', line 37 def process_success strategy.success if strategy.respond_to?(:success) rescue Redis::BaseError end |
#run ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/cb2/breaker.rb', line 9 def run if open? raise CB2::BreakerOpen.new("#{service} breaker open") end begin process_count ret = yield process_success rescue => e process_error raise e end return ret end |