Class: Resilient::CircuitBreaker::Properties

Inherits:
Object
  • Object
show all
Defined in:
lib/resilient/circuit_breaker/properties.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Properties

Returns a new instance of Properties.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/resilient/circuit_breaker/properties.rb', line 53

def initialize(options = {})
  @force_open = options.fetch(:force_open, false)
  @force_closed = options.fetch(:force_closed, false)
  @instrumenter = options.fetch(:instrumenter, Instrumenters::Noop)
  @sleep_window_seconds = options.fetch(:sleep_window_seconds, 5)
  @request_volume_threshold = options.fetch(:request_volume_threshold, 20)
  @error_threshold_percentage = options.fetch(:error_threshold_percentage, 50)
  @window_size_in_seconds = options.fetch(:window_size_in_seconds, 60)
  @bucket_size_in_seconds = options.fetch(:bucket_size_in_seconds, 10)

  if @bucket_size_in_seconds >= @window_size_in_seconds
    raise ArgumentError, "bucket_size_in_seconds must be smaller than window_size_in_seconds"
  end

  if @window_size_in_seconds % @bucket_size_in_seconds != 0
    raise ArgumentError,
      "window_size_in_seconds must be perfectly divisible by" +
      " bucket_size_in_seconds in order to evenly partition the buckets"
  end

  @metrics = options.fetch(:metrics) {
    Metrics.new({
      window_size_in_seconds: @window_size_in_seconds,
      bucket_size_in_seconds: @bucket_size_in_seconds,
    })
  }
end

Instance Attribute Details

#bucket_size_in_secondsObject (readonly)

size of buckets in statistical window



48
49
50
# File 'lib/resilient/circuit_breaker/properties.rb', line 48

def bucket_size_in_seconds
  @bucket_size_in_seconds
end

#error_threshold_percentageObject (readonly)

% of “marks” that must be failed to trip the circuit



42
43
44
# File 'lib/resilient/circuit_breaker/properties.rb', line 42

def error_threshold_percentage
  @error_threshold_percentage
end

#force_closedObject (readonly)

allows ignoring errors and therefore never trip “open” (ie. allow all traffic through); normal instrumentation will still happen, thus allowing you to “test” configuration live without impact



28
29
30
# File 'lib/resilient/circuit_breaker/properties.rb', line 28

def force_closed
  @force_closed
end

#force_openObject (readonly)

allows forcing the circuit open (stopping all requests)



23
24
25
# File 'lib/resilient/circuit_breaker/properties.rb', line 23

def force_open
  @force_open
end

#instrumenterObject (readonly)

what to use to instrument all events that happen (ie: ActiveSupport::Notifications)



32
33
34
# File 'lib/resilient/circuit_breaker/properties.rb', line 32

def instrumenter
  @instrumenter
end

#metricsObject (readonly)

metrics instance used to keep track of success and failure



51
52
53
# File 'lib/resilient/circuit_breaker/properties.rb', line 51

def metrics
  @metrics
end

#request_volume_thresholdObject (readonly)

number of requests that must be made within a statistical window before open/close decisions are made using stats



39
40
41
# File 'lib/resilient/circuit_breaker/properties.rb', line 39

def request_volume_threshold
  @request_volume_threshold
end

#sleep_window_secondsObject (readonly)

seconds after tripping circuit before allowing retry



35
36
37
# File 'lib/resilient/circuit_breaker/properties.rb', line 35

def sleep_window_seconds
  @sleep_window_seconds
end

#window_size_in_secondsObject (readonly)

number of seconds in the statistical window



45
46
47
# File 'lib/resilient/circuit_breaker/properties.rb', line 45

def window_size_in_seconds
  @window_size_in_seconds
end

Class Method Details

.wrap(hash_or_instance) ⇒ Object

Internal: Takes a string name or instance of a Key and always returns a Key instance.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/resilient/circuit_breaker/properties.rb', line 9

def self.wrap(hash_or_instance)
  case hash_or_instance
  when self
    hash_or_instance
  when Hash
    new(hash_or_instance)
  when NilClass
    new
  else
    raise TypeError, "properties must be Hash or Resilient::Properties instance"
  end
end