Class: Circuitbox::CircuitBreaker

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @service = service
  @circuit_options = options
  @circuit_store   = options.fetch(:cache) { Circuitbox.circuit_store }
  @notifier        = options.fetch(:notifier_class) { Notifier }

  @exceptions = options.fetch(:exceptions) { [] }
  @exceptions = [Timeout::Error] if @exceptions.blank?

  @logger     = options.fetch(:logger) { defined?(Rails) ? Rails.logger : Logger.new(STDOUT) }
  @time_class   = options.fetch(:time_class) { Time }
  sanitize_options
end

Instance Attribute Details

#circuit_optionsObject

Returns the value of attribute circuit_options.



3
4
5
# File 'lib/circuitbox/circuit_breaker.rb', line 3

def circuit_options
  @circuit_options
end

#circuit_storeObject

Returns the value of attribute circuit_store.



3
4
5
# File 'lib/circuitbox/circuit_breaker.rb', line 3

def circuit_store
  @circuit_store
end

#exceptionsObject

Returns the value of attribute exceptions.



3
4
5
# File 'lib/circuitbox/circuit_breaker.rb', line 3

def exceptions
  @exceptions
end

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/circuitbox/circuit_breaker.rb', line 3

def logger
  @logger
end

#notifierObject

Returns the value of attribute notifier.



3
4
5
# File 'lib/circuitbox/circuit_breaker.rb', line 3

def notifier
  @notifier
end

#partitionObject

Returns the value of attribute partition.



3
4
5
# File 'lib/circuitbox/circuit_breaker.rb', line 3

def partition
  @partition
end

#serviceObject

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_countObject



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

Returns:

  • (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 = circuit_options.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(run_options = {})
  begin
    run!(run_options, &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!(run_options = {})
  @partition = run_options.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 = run_options.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_countObject



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_timeObject



109
110
111
# File 'lib/circuitbox/circuit_breaker.rb', line 109

def try_close_next_time
  circuit_store.delete(storage_key(:asleep))
end