resilient 0.4.0.beta2 → 0.4.0.beta3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61b08291053b49bf75e2b32eef05a5ab4fe4b4f2
4
- data.tar.gz: fbf4f792be70a627f6c71b587a3efc30f7ff9527
3
+ metadata.gz: 17c72fc869fd44e4732ac9f438a8303489ca6bed
4
+ data.tar.gz: 1dcf39dc5da88e0a4d2c62d9d506118e2d807773
5
5
  SHA512:
6
- metadata.gz: 9ddd1cb3aa4018a638df2cb1c6b4fa75fe3b69e78b0d927f5aee0c7e5d0be9f3a217b65d11c2929420c1ef68ac77da8c824bcce7b8e57e283398bd853994ccb7
7
- data.tar.gz: b60e16a13ae90cba9a2a76e779d50ff3167d069c98fd4cf717cd4141751d7119ce50bec1bf697b1666949ada4cfda4538c7fef87ac20b6b9449588da53838daf
6
+ metadata.gz: cc8a77ebfedd6e9e46d1d12055c989583c1bf9274b646d3d6df4cd85f5095a8c9de72af980fbcdc0e9556c141095a47e71defaf84540ffefbb245b17484eb40a
7
+ data.tar.gz: 93efab2978a8e1562d835af261a362172ec708a1f211d22faf0bf78acea3f89d2f2020b4ecf45a760ae90a57c2d4ec81da360026afa67abbcdaa9530582f9be3
@@ -2,20 +2,23 @@ require "resilient/key"
2
2
  require "resilient/circuit_breaker/metrics"
3
3
  require "resilient/circuit_breaker/properties"
4
4
  require "resilient/circuit_breaker/registry"
5
+ require "forwardable"
5
6
 
6
7
  module Resilient
7
8
  class CircuitBreaker
9
+ extend Forwardable
10
+
8
11
  # Public: Returns an instance of circuit breaker based on key and registry.
9
12
  # Default registry is used if none is provided. If key does not exist, it is
10
13
  # registered. If key does exist, it returns registered instance instead of
11
14
  # allocating a new instance in order to ensure that state/metrics are the
12
15
  # same per key.
13
16
  #
14
- # See #initialize for docs on key, properties and metrics.
15
- def self.get(key, properties = nil, metrics = nil, registry = nil)
17
+ # See #initialize for docs on key and properties.
18
+ def self.get(key, properties = nil, registry = nil)
16
19
  key = Key.wrap(key)
17
20
  (registry || Registry.default).fetch(key) {
18
- new(key, properties, metrics)
21
+ new(key, properties)
19
22
  }
20
23
  end
21
24
 
@@ -26,10 +29,11 @@ module Resilient
26
29
  end
27
30
  end
28
31
 
32
+ def_delegator :@properties, :metrics
33
+
29
34
  attr_reader :key
30
35
  attr_reader :open
31
36
  attr_reader :opened_or_last_checked_at_epoch
32
- attr_reader :metrics
33
37
  attr_reader :properties
34
38
 
35
39
  # Private: Builds new instance of a CircuitBreaker.
@@ -41,32 +45,14 @@ module Resilient
41
45
  # circuit breaker should behave. Optional. Defaults to new
42
46
  # Resilient::CircuitBreaker::Properties instance.
43
47
  #
44
- # metrics - The object that stores successes and failures. Optional.
45
- # Defaults to new Resilient::CircuitBreaker::Metrics instance
46
- # based on window size and bucket size properties.
47
- #
48
48
  # Returns CircuitBreaker instance.
49
- def initialize(key, properties = nil, metrics = nil)
49
+ def initialize(key, properties = nil)
50
50
  raise ArgumentError, "key argument is required" if key.nil?
51
51
 
52
52
  @key = Key.wrap(key)
53
+ @properties = Properties.wrap(properties)
53
54
  @open = false
54
55
  @opened_or_last_checked_at_epoch = 0
55
-
56
- @properties = if properties
57
- Properties.wrap(properties)
58
- else
59
- Properties.new
60
- end
61
-
62
- @metrics = if metrics
63
- metrics
64
- else
65
- Metrics.new({
66
- window_size_in_seconds: @properties.window_size_in_seconds,
67
- bucket_size_in_seconds: @properties.bucket_size_in_seconds,
68
- })
69
- end
70
56
  end
71
57
 
72
58
  def allow_request?
@@ -95,7 +81,7 @@ module Resilient
95
81
  payload[:closed_the_circuit] = true
96
82
  close_circuit
97
83
  else
98
- @metrics.success
84
+ metrics.success
99
85
  end
100
86
  nil
101
87
  }
@@ -103,7 +89,7 @@ module Resilient
103
89
 
104
90
  def failure
105
91
  instrument("resilient.circuit_breaker.failure", key: @key) { |payload|
106
- @metrics.failure
92
+ metrics.failure
107
93
  nil
108
94
  }
109
95
  end
@@ -112,7 +98,7 @@ module Resilient
112
98
  instrument("resilient.circuit_breaker.reset", key: @key) { |payload|
113
99
  @open = false
114
100
  @opened_or_last_checked_at_epoch = 0
115
- @metrics.reset
101
+ metrics.reset
116
102
  nil
117
103
  }
118
104
  end
@@ -127,15 +113,15 @@ module Resilient
127
113
  def close_circuit
128
114
  @open = false
129
115
  @opened_or_last_checked_at_epoch = 0
130
- @metrics.reset
116
+ metrics.reset
131
117
  end
132
118
 
133
119
  def under_request_volume_threshold?
134
- @metrics.requests < @properties.request_volume_threshold
120
+ metrics.requests < @properties.request_volume_threshold
135
121
  end
136
122
 
137
123
  def under_error_threshold_percentage?
138
- @metrics.error_percentage < @properties.error_threshold_percentage
124
+ metrics.error_percentage < @properties.error_threshold_percentage
139
125
  end
140
126
 
141
127
  def open?
@@ -12,6 +12,8 @@ module Resilient
12
12
  hash_or_instance
13
13
  when Hash
14
14
  new(hash_or_instance)
15
+ when NilClass
16
+ new
15
17
  else
16
18
  raise TypeError, "properties must be Hash or Resilient::Properties instance"
17
19
  end
@@ -45,6 +47,9 @@ module Resilient
45
47
  # size of buckets in statistical window
46
48
  attr_reader :bucket_size_in_seconds
47
49
 
50
+ # metrics instance used to keep track of success and failure
51
+ attr_reader :metrics
52
+
48
53
  def initialize(options = {})
49
54
  @force_open = options.fetch(:force_open, false)
50
55
  @force_closed = options.fetch(:force_closed, false)
@@ -64,6 +69,13 @@ module Resilient
64
69
  "window_size_in_seconds must be perfectly divisible by" +
65
70
  " bucket_size_in_seconds in order to evenly partition the buckets"
66
71
  end
72
+
73
+ @metrics = options.fetch(:metrics) {
74
+ Metrics.new({
75
+ window_size_in_seconds: @window_size_in_seconds,
76
+ bucket_size_in_seconds: @bucket_size_in_seconds,
77
+ })
78
+ }
67
79
  end
68
80
  end
69
81
  end
@@ -1,3 +1,3 @@
1
1
  module Resilient
2
- VERSION = "0.4.0.beta2"
2
+ VERSION = "0.4.0.beta3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resilient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.beta2
4
+ version: 0.4.0.beta3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-04 00:00:00.000000000 Z
11
+ date: 2016-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler