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



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/circuitbox/circuit_breaker.rb', line 24

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     = 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



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_countObject



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

Returns:

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



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_timeObject



107
108
109
# File 'lib/circuitbox/circuit_breaker.rb', line 107

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