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 `logger` - Logger to use - defaults to Rails.logger if defined, otherwise STDOUT
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/circuitbox/circuit_breaker.rb', line 25 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 = .fetch(: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
95 96 97 98 99 |
# File 'lib/circuitbox/circuit_breaker.rb', line 95 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
101 102 103 |
# File 'lib/circuitbox/circuit_breaker.rb', line 101 def failure_count circuit_store.load(stat_storage_key(:failure), raw: true).to_i end |
#open? ⇒ Boolean
85 86 87 88 89 90 91 92 93 |
# File 'lib/circuitbox/circuit_breaker.rb', line 85 def open? if open_flag? true elsif passed_volume_threshold? && passed_rate_threshold? true else false end end |
#option_value(name) ⇒ Object
39 40 41 42 |
# File 'lib/circuitbox/circuit_breaker.rb', line 39 def option_value(name) value = .fetch(name) { DEFAULTS.fetch(name) } value.is_a?(Proc) ? value.call : value end |
#run(run_options = {}) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/circuitbox/circuit_breaker.rb', line 77 def run( = {}) begin run!(, &Proc.new) rescue Circuitbox::Error nil end end |
#run!(run_options = {}) ⇒ Object
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 74 75 |
# File 'lib/circuitbox/circuit_breaker.rb', line 44 def run!( = {}) @partition = .delete(:partition) # sorry for this hack. if open? logger.debug "[CIRCUIT] open: skipping #{service}" open! unless open_flag? skipped! 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
105 106 107 |
# File 'lib/circuitbox/circuit_breaker.rb', line 105 def success_count circuit_store.load(stat_storage_key(:success), raw: true).to_i end |
#try_close_next_time ⇒ Object
109 110 111 |
# File 'lib/circuitbox/circuit_breaker.rb', line 109 def try_close_next_time circuit_store.delete(storage_key(:asleep)) end |