Class: Circuitbox::CircuitBreaker
- Inherits:
-
Object
- Object
- Circuitbox::CircuitBreaker
- Defined in:
- lib/circuitbox/circuit_breaker.rb
Constant Summary collapse
- DEFAULTS =
{ sleep_window: 300, volume_threshold: 5, error_threshold: 50, timeout_seconds: 1, time_window: 60, }
Instance Attribute Summary collapse
-
#circuit_options ⇒ Object
Returns the value of attribute circuit_options.
-
#circuit_store ⇒ Object
Returns the value of attribute circuit_store.
-
#exceptions ⇒ Object
Returns the value of attribute exceptions.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#notifier ⇒ Object
Returns the value of attribute notifier.
-
#partition ⇒ Object
Returns the value of attribute partition.
-
#service ⇒ Object
Returns the value of attribute service.
Instance Method Summary collapse
- #error_rate(failures = failure_count, success = success_count) ⇒ Object
- #failure_count ⇒ Object
-
#initialize(service, options = {}) ⇒ CircuitBreaker
constructor
Configuration options.
- #open? ⇒ Boolean
- #option_value(name) ⇒ Object
- #run(run_options = {}) ⇒ Object
- #run!(run_options = {}) ⇒ Object
- #success_count ⇒ Object
- #try_close_next_time ⇒ Object
Constructor Details
#initialize(service, options = {}) ⇒ CircuitBreaker
Configuration options
‘sleep_window` - seconds to sleep the circuit `volume_threshold` - number of requests before error rate calculation occurs `error_threshold` - percentage of failed requests needed to trip circuit `timeout_seconds` - seconds until it will timeout the request `exceptions` - exceptions other than Timeout::Error that count as failures `time_window` - interval of time used to calculate error_rate (in seconds) - default is 60s
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/circuitbox/circuit_breaker.rb', line 24 def initialize(service, = {}) @service = service @circuit_options = @circuit_store = .fetch(:cache) { Circuitbox.circuit_store } @notifier = .fetch(:notifier_class) { Notifier } @exceptions = .fetch(:exceptions) { [] } @exceptions = [Timeout::Error] if @exceptions.blank? @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT) @time_class = .fetch(:time_class) { Time } end |
Instance Attribute Details
#circuit_options ⇒ Object
Returns the value of attribute circuit_options.
3 4 5 |
# File 'lib/circuitbox/circuit_breaker.rb', line 3 def @circuit_options end |
#circuit_store ⇒ Object
Returns the value of attribute circuit_store.
3 4 5 |
# File 'lib/circuitbox/circuit_breaker.rb', line 3 def circuit_store @circuit_store end |
#exceptions ⇒ Object
Returns the value of attribute exceptions.
3 4 5 |
# File 'lib/circuitbox/circuit_breaker.rb', line 3 def exceptions @exceptions end |
#logger ⇒ Object
Returns the value of attribute logger.
3 4 5 |
# File 'lib/circuitbox/circuit_breaker.rb', line 3 def logger @logger end |
#notifier ⇒ Object
Returns the value of attribute notifier.
3 4 5 |
# File 'lib/circuitbox/circuit_breaker.rb', line 3 def notifier @notifier end |
#partition ⇒ Object
Returns the value of attribute partition.
3 4 5 |
# File 'lib/circuitbox/circuit_breaker.rb', line 3 def partition @partition end |
#service ⇒ Object
Returns the value of attribute service.
3 4 5 |
# File 'lib/circuitbox/circuit_breaker.rb', line 3 def service @service end |
Instance Method Details
#error_rate(failures = failure_count, success = success_count) ⇒ Object
93 94 95 96 97 |
# File 'lib/circuitbox/circuit_breaker.rb', line 93 def error_rate(failures = failure_count, success = success_count) all_count = failures + success return 0.0 unless all_count > 0 failure_count.to_f / all_count.to_f * 100 end |
#failure_count ⇒ Object
99 100 101 |
# File 'lib/circuitbox/circuit_breaker.rb', line 99 def failure_count circuit_store.load(stat_storage_key(:failure), raw: true).to_i end |
#open? ⇒ Boolean
83 84 85 86 87 88 89 90 91 |
# File 'lib/circuitbox/circuit_breaker.rb', line 83 def open? if open_flag? true elsif passed_volume_threshold? && passed_rate_threshold? true else false end end |
#option_value(name) ⇒ Object
38 39 40 41 |
# File 'lib/circuitbox/circuit_breaker.rb', line 38 def option_value(name) value = .fetch(name) { DEFAULTS.fetch(name) } value.is_a?(Proc) ? value.call : value end |
#run(run_options = {}) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/circuitbox/circuit_breaker.rb', line 75 def run( = {}) begin run!(, &Proc.new) rescue Circuitbox::Error nil end end |
#run!(run_options = {}) ⇒ Object
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 71 72 73 |
# File 'lib/circuitbox/circuit_breaker.rb', line 43 def run!( = {}) @partition = .delete(:partition) # sorry for this hack. if open? logger.debug "[CIRCUIT] open: skipping #{service}" open! unless open_flag? raise Circuitbox::OpenCircuitError.new(service) else close! if was_open? logger.debug "[CIRCUIT] closed: querying #{service}" begin response = if exceptions.include? Timeout::Error timeout_seconds = .fetch(:timeout_seconds) { option_value(:timeout_seconds) } timeout (timeout_seconds) { yield } else yield end logger.debug "[CIRCUIT] closed: #{service} querie success" success! rescue *exceptions => exception logger.debug "[CIRCUIT] closed: detected #{service} failure" failure! open! if half_open? raise Circuitbox::ServiceFailureError.new(service, exception) end end return response end |
#success_count ⇒ Object
103 104 105 |
# File 'lib/circuitbox/circuit_breaker.rb', line 103 def success_count circuit_store.load(stat_storage_key(:success), raw: true).to_i end |
#try_close_next_time ⇒ Object
107 108 109 |
# File 'lib/circuitbox/circuit_breaker.rb', line 107 def try_close_next_time circuit_store.delete(storage_key(:asleep)) end |