circuitbox 1.1.0 → 2.0.0.pre4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +56 -187
  3. data/lib/circuitbox.rb +14 -57
  4. data/lib/circuitbox/circuit_breaker.rb +137 -161
  5. data/lib/circuitbox/circuit_breaker/logger_messages.rb +31 -0
  6. data/lib/circuitbox/configuration.rb +51 -0
  7. data/lib/circuitbox/errors/error.rb +3 -2
  8. data/lib/circuitbox/errors/open_circuit_error.rb +3 -1
  9. data/lib/circuitbox/errors/service_failure_error.rb +5 -1
  10. data/lib/circuitbox/excon_middleware.rb +23 -30
  11. data/lib/circuitbox/faraday_middleware.rb +43 -63
  12. data/lib/circuitbox/memory_store.rb +85 -0
  13. data/lib/circuitbox/memory_store/container.rb +30 -0
  14. data/lib/circuitbox/memory_store/monotonic_time.rb +13 -0
  15. data/lib/circuitbox/notifier/active_support.rb +19 -0
  16. data/lib/circuitbox/notifier/null.rb +13 -0
  17. data/lib/circuitbox/timer.rb +51 -0
  18. data/lib/circuitbox/version.rb +3 -1
  19. metadata +106 -118
  20. data/.gitignore +0 -20
  21. data/.ruby-version +0 -1
  22. data/.travis.yml +0 -9
  23. data/Gemfile +0 -6
  24. data/Rakefile +0 -18
  25. data/benchmark/circuit_store_benchmark.rb +0 -114
  26. data/circuitbox.gemspec +0 -48
  27. data/lib/circuitbox/memcache_store.rb +0 -31
  28. data/lib/circuitbox/notifier.rb +0 -34
  29. data/test/circuit_breaker_test.rb +0 -428
  30. data/test/circuitbox_test.rb +0 -45
  31. data/test/excon_middleware_test.rb +0 -131
  32. data/test/faraday_middleware_test.rb +0 -175
  33. data/test/integration/circuitbox_cross_process_open_test.rb +0 -56
  34. data/test/integration/faraday_middleware_test.rb +0 -78
  35. data/test/integration_helper.rb +0 -48
  36. data/test/notifier_test.rb +0 -21
  37. data/test/service_failure_error_test.rb +0 -23
  38. data/test/test_helper.rb +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: bb9f064a8178b7bfef8be4552f91410d0783acf5
4
- data.tar.gz: ca1e8126ff01603f881c351c28c47396a8fa7d97
2
+ SHA256:
3
+ metadata.gz: fb89d6ef46d73dfa35e7c43181112309aa524979ec1285688cc8cebf8d781bd4
4
+ data.tar.gz: 735c23115ea5127923c05b84d5b6cfe64193521bcd751639cc6ceab32c6b97ce
5
5
  SHA512:
6
- metadata.gz: f9e3f74178e1cf6200d5dbbf99925ddb1f235ebf3d287e3c82bd0894d1430b69a4ed062bbaf9afb9cacd5b5707c32cf2dcbd10cd78293b5f438f3b738b956837
7
- data.tar.gz: fe2a7be07392bd92a0a9e731550f82f1b4e3c6d2ed21759c25d91776e5734d538bb4e7d5bc33da09edd8de5ea2ccdb731e6e8baa36946ea02a7e908415b5c17e
6
+ metadata.gz: a29d10725e8a1a36eec12985681cd66b7c2b3303cb996bfe72226674230d7c8ef38b02bb5731665edd49c7922b2a9f0072434a2ab33da7f64713875415071420
7
+ data.tar.gz: 9a165096e2b0e5fe484c433284a3c0b6658f4b880552602cb57b22fcf3ea540302b3d3a0910b31b8f3e1938259ca6394faefd3bffffc6c40bf3104d8d02564ec
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Circuitbox
2
2
 
3
- [![Build Status](https://travis-ci.org/yammer/circuitbox.svg?branch=master)](https://travis-ci.org/yammer/circuitbox) [![Gem Version](https://badge.fury.io/rb/circuitbox.svg)](https://badge.fury.io/rb/circuitbox)
3
+ ![Tests](https://github.com/yammer/circuitbox/workflows/Tests/badge.svg) [![Gem Version](https://badge.fury.io/rb/circuitbox.svg)](https://badge.fury.io/rb/circuitbox)
4
4
 
5
- Circuitbox is a Ruby circuit breaker gem. It protects your application from failures of it's service dependencies. It wraps calls to external services and monitors for failures in one minute intervals. Once more than 10 requests have been made with a 50% failure rate, Circuitbox stops sending requests to that failing service for one minute. This helps your application gracefully degrade.
5
+ Circuitbox is a Ruby circuit breaker gem. It protects your application from failures of its service dependencies. It wraps calls to external services and monitors for failures in one minute intervals. Once more than 10 requests have been made with a 50% failure rate, Circuitbox stops sending requests to that failing service for one minute. This helps your application gracefully degrade.
6
6
  Resources about the circuit breaker pattern:
7
7
  * [http://martinfowler.com/bliki/CircuitBreaker.html](http://martinfowler.com/bliki/CircuitBreaker.html)
8
8
  * [https://github.com/Netflix/Hystrix/wiki/How-it-Works#CircuitBreaker](https://github.com/Netflix/Hystrix/wiki/How-it-Works#CircuitBreaker)
@@ -10,13 +10,13 @@ Resources about the circuit breaker pattern:
10
10
  ## Usage
11
11
 
12
12
  ```ruby
13
- Circuitbox[:your_service] do
13
+ Circuitbox.circuit(:your_service, exceptions: [Net::ReadTimeout]) do
14
14
  Net::HTTP.get URI('http://example.com/api/messages')
15
15
  end
16
16
  ```
17
17
 
18
18
  Circuitbox will return nil for failed requests and open circuits.
19
- If your HTTP client has it's own conditions for failure, you can pass an `exceptions` option.
19
+ If your HTTP client has its own conditions for failure, you can pass an `exceptions` option.
20
20
 
21
21
  ```ruby
22
22
  class ExampleServiceClient
@@ -25,50 +25,75 @@ class ExampleServiceClient
25
25
  end
26
26
 
27
27
  def http_get
28
- circuit.run do
28
+ circuit.run(circuitbox_exceptions: false) do
29
29
  Zephyr.new("http://example.com").get(200, 1000, "/api/messages")
30
30
  end
31
31
  end
32
32
  end
33
33
  ```
34
34
 
35
- Using the `run!` method will throw an exception when the circuit is open or the underlying service fails.
35
+ Using the `run` method will throw an exception when the circuit is open or the underlying service fails.
36
36
 
37
37
  ```ruby
38
38
  def http_get
39
- circuit.run! do
39
+ circuit.run do
40
40
  Zephyr.new("http://example.com").get(200, 1000, "/api/messages")
41
41
  end
42
42
  end
43
43
  ```
44
44
 
45
+ ## Global Configuration
46
+ Circuitbox has defaults for circuit_store, notifier, and logger.
47
+ This can be configured through ```Circuitbox.configure```.
48
+ The circuit cache used by ```Circuitbox.circuit``` will be cleared after running ```Circuitbox.configure```.
49
+ This means when accessing the circuit through ```Circuitbox.circuit``` any custom configuration options should always be given.
50
+
51
+ Any circuit created manually through ```Circuitbox::CircuitBreaker``` before updating the configuration
52
+ will need to be recreated to pick up the new defaults.
53
+
54
+ ```ruby
55
+ Circuitbox.configure do |config|
56
+ config.default_circuit_store = Circuitbox::MemoryStore.new
57
+ config.default_notifier = Circuitbox::Notifier::Null.new
58
+ config.default_logger = Rails.logger
59
+ end
60
+ ```
61
+
45
62
 
46
- ## Configuration
63
+ ## Per-Circuit Configuration
47
64
 
48
65
  ```ruby
49
66
  class ExampleServiceClient
50
67
  def circuit
51
68
  Circuitbox.circuit(:your_service, {
69
+ # exceptions circuitbox tracks for counting failures (required)
52
70
  exceptions: [YourCustomException],
53
71
 
54
72
  # seconds the circuit stays open once it has passed the error threshold
55
73
  sleep_window: 300,
56
74
 
57
- # number of requests within 1 minute before it calculates error rates
75
+ # length of interval (in seconds) over which it calculates the error rate
76
+ time_window: 60,
77
+
78
+ # number of requests within `time_window` seconds before it calculates error rates (checked on failures)
58
79
  volume_threshold: 10,
59
80
 
60
81
  # the store you want to use to save the circuit state so it can be
61
82
  # tracked, this needs to be Moneta compatible, and support increment
62
- cache: Moneta.new(:Memory)
83
+ # this overrides what is set in the global configuration
84
+ cache: Circuitbox::MemoryStore.new,
63
85
 
64
- # exceeding this rate will open the circuit
86
+ # exceeding this rate will open the circuit (checked on failures)
65
87
  error_threshold: 50,
66
88
 
67
- # seconds before the circuit times out
68
- timeout_seconds: 1
69
-
70
89
  # Logger to use
71
- logger: Logger.new(STDOUT)
90
+ # This overrides what is set in the global configuration
91
+ logger: Logger.new(STDOUT),
92
+
93
+ # Customized notifier
94
+ # overrides the default
95
+ # this overrides what is set in the global configuration
96
+ notifier: Notifier.new
72
97
  })
73
98
  end
74
99
  end
@@ -78,29 +103,26 @@ You can also pass a Proc as an option value which will evaluate each time the ci
78
103
 
79
104
  ```ruby
80
105
  Circuitbox.circuit(:yammer, {
81
- sleep_window: Proc.new { Configuration.get(:sleep_window) }
106
+ sleep_window: Proc.new { Configuration.get(:sleep_window) },
107
+ exceptions: [Net::ReadTimeout]
82
108
  })
83
109
  ```
84
110
 
85
111
  ## Circuit Store (:cache)
86
112
 
87
113
  Holds all the relevant data to trip the circuit if a given number of requests
88
- fail in a specified period of time. The store is based on
89
- [Moneta](https://github.com/minad/moneta) so there are a lot of stores to choose
90
- from. There are some pre-requisits they need to satisfy so:
91
-
92
- - Need to support increment, this is true for most but not all available stores.
93
- - Need to support concurrent access if you share them. For example sharing a
114
+ fail in a specified period of time. Circuitbox also supports
115
+ [Moneta](https://github.com/minad/moneta). As moneta is not a dependency of circuitbox
116
+ it needs to be loaded prior to use. There are a lot of moneta stores to choose from but
117
+ some pre-requisits need to be satisfied first:
118
+
119
+ - Needs to support increment, this is true for most but not all available stores.
120
+ - Needs to support expiry.
121
+ - Needs to support concurrent access if you share them. For example sharing a
94
122
  KyotoCabinet store across process fails because the store is single writer
95
123
  multiple readers, and all circuits sharing the store need to be able to write.
96
124
 
97
125
 
98
- ## Monitoring & Statistics
99
-
100
- You can also run `rake circuits:stats SERVICE={service_name}` to see successes, failures and opened circuits.
101
- Add `PARTITION={partition_key}` to see the circuit for a particular partition.
102
- The stats are aggregated into 1 minute intervals.
103
-
104
126
  ## Notifications
105
127
 
106
128
  circuitbox use ActiveSupport Notifications.
@@ -120,7 +142,7 @@ ActiveSupport::Notifications.subscribe('circuit_close') do |name, start, finish,
120
142
  circuit_name = payload[:circuit]
121
143
  Rails.logger.info("Close circuit for: #{circuit_name}")
122
144
  end
123
- ````
145
+ ```
124
146
 
125
147
  **generate metrics:**
126
148
 
@@ -139,9 +161,7 @@ end
139
161
 
140
162
  `payload[:gauge]` can be:
141
163
 
142
- - `failure_count`
143
- - `success_count`
144
- - `error_rate`
164
+ - `runtime` # runtime will only be notified when circuit is closed and block is successfully executed.
145
165
 
146
166
  **warnings:**
147
167
  in case of misconfiguration, circuitbox will fire a circuitbox_warning
@@ -156,77 +176,6 @@ end
156
176
 
157
177
  ```
158
178
 
159
- ### Multi process Circuits
160
-
161
- `circuit_store` is backed by [Moneta](https://github.com/minad/moneta) which
162
- supports multiple backends. This can be configured by passing `cache:
163
- Moneta.new(:PStore, file: "myfile.store")` to use for example the built in
164
- PStore ruby library for persisted store, which can be shared cross process.
165
-
166
- Depending on your requirements different stores can make sense, see the
167
- benchmarks and [moneta
168
- feature](https://github.com/minad/moneta#backend-feature-matrix) matrix for
169
- details.
170
-
171
- ```
172
- user system total real
173
- memory: 1.440000 0.140000 1.580000 ( 1.579244)
174
- lmdb: 4.330000 3.280000 7.610000 ( 13.086398)
175
- pstore: 23.680000 4.350000 28.030000 ( 28.094312)
176
- daybreak: 2.270000 0.450000 2.720000 ( 2.626748)
177
- ```
178
-
179
- You can run the benchmarks yourself by running `rake benchmark`.
180
-
181
- ### Memory
182
-
183
- An in memory store, which is local to the process. This is not threadsafe so it
184
- is not useable with multithreaded webservers for example. It is always going to
185
- be the fastest option if no multi-process or thread is required, like in
186
- development on Webbrick.
187
-
188
- This is the default.
189
-
190
- ```ruby
191
- Circuitbox.circuit :identifier, cache: Moneta.new(:Memory)
192
- ```
193
-
194
- ### LMDB
195
-
196
- An persisted directory backed store, which is thread and multi process safe.
197
- depends on the `lmdb` gem. It is slower than Memory or Daybreak, but can be
198
- used in multi thread and multi process environments like like Puma.
199
-
200
- ```ruby
201
- require "lmdb"
202
- Circuitbox.circuit :identifier, cache: Moneta.new(:LMDB, dir: "./", db: "mydb")
203
- ```
204
-
205
- ### PStore
206
-
207
- An persisted file backed store, which comes with the ruby
208
- [stdlib](http://ruby-doc.org/stdlib-2.3.0/libdoc/pstore/rdoc/PStore.html). It
209
- has no external dependecies and works on every ruby implementation. Due to it
210
- being file backed it is multi process safe, good for development using Unicorn.
211
-
212
- ```ruby
213
- Circuitbox.circuit :identifier, cache: Moneta.new(:PStore, file: "db.pstore")
214
- ```
215
-
216
- ### Daybreak
217
-
218
- Persisted, file backed key value store in pure ruby. It is process safe and
219
- outperforms most other stores in circuitbox. This is recommended for production
220
- use with Unicorn. It depends on the `daybreak` gem.
221
-
222
- ```ruby
223
- require "daybreak"
224
- Circuitbox.circuit :identifier, cache: Moneta.new(:Daybreak, file: "db.daybreak", expires: true)
225
- ```
226
-
227
- It is important for the store to have
228
- [expires](https://github.com/minad/moneta#backend-feature-matrix) support.
229
-
230
179
  ## Faraday
231
180
 
232
181
  Circuitbox ships with [Faraday HTTP client](https://github.com/lostisland/faraday) middleware.
@@ -250,24 +199,15 @@ end
250
199
  By default the Faraday middleware returns a `503` response when the circuit is
251
200
  open, but this as many other things can be configured via middleware options
252
201
 
253
- * `exceptions` pass a list of exceptions for the Circuitbreaker to catch,
254
- defaults to Timeout and Request failures
255
-
256
- ```ruby
257
- c.use Circuitbox::FaradayMiddleware, exceptions: [Faraday::Error::TimeoutError]
258
- ```
259
-
260
202
  * `default_value` value to return for open circuits, defaults to 503 response
261
203
  wrapping the original response given by the service and stored as
262
204
  `original_response` property of the returned 503, this can be overwritten
263
205
  with either
264
206
  * a static value
265
207
  * a `lambda` which is passed the `original_response` and `original_error`.
266
- `original_response` will be populated if Faraday returne an error response,
208
+ `original_response` will be populated if Faraday returns an error response,
267
209
  `original_error` will be populated if an error was thrown before Faraday
268
- returned a response. (It will also accept a lambda with arity 1 that is
269
- only passed `original_response`. This use is deprecated and will be removed
270
- in the next major version.)
210
+ returned a response.
271
211
 
272
212
  ```ruby
273
213
  c.use Circuitbox::FaradayMiddleware, default_value: lambda { |response, error| ... }
@@ -279,15 +219,9 @@ c.use Circuitbox::FaradayMiddleware, default_value: lambda { |response, error| .
279
219
  c.use Circuitbox::FaradayMiddleware, identifier: "service_name_circuit"
280
220
  ```
281
221
 
282
- * `circuit_breaker_run_options` options passed to the circuit run method, see
283
- the main circuitbreaker for those.
284
-
285
- ```ruby
286
- conn.get("/api", circuit_breaker_run_options: {})
287
- ```
288
-
289
222
  * `circuit_breaker_options` options to initialize the circuit with defaults to
290
- `{ volume_threshold: 10, exceptions: Circuitbox::FaradayMiddleware::DEFAULT_EXCEPTIONS }`
223
+ `{ exceptions: Circuitbox::FaradayMiddleware::DEFAULT_EXCEPTIONS }`.
224
+ Accepts same options as Circuitbox:CircuitBreaker#new
291
225
 
292
226
  ```ruby
293
227
  c.use Circuitbox::FaradayMiddleware, circuit_breaker_options: {}
@@ -300,71 +234,6 @@ c.use Circuitbox::FaradayMiddleware, circuit_breaker_options: {}
300
234
  c.use Circuitbox::FaradayMiddleware, open_circuit: lambda { |response| response.status >= 500 }
301
235
  ```
302
236
 
303
- ## CHANGELOG
304
- ### v1.1.0
305
- - ruby 2.2 support [#58](https://github.com/yammer/circuitbox/pull/58)
306
- - configurable logger [#58](https://github.com/yammer/circuitbox/pull/58)
307
-
308
- ### v1.0.3
309
- - fix timeout issue for default configuration, as default `:Memory` adapter does
310
- not natively support expires, we need to actually load it on demand.
311
- - fix memoization of `circuit_breaker_options` not actually doing memoization in
312
- `excon` and `faraday` middleware.
313
-
314
- ### v1.0.2
315
- - Fix timeout issue [#51](https://github.com/yammer/circuitbox/issues/51)
316
- [sebastian-juliu](https://github.com/sebastian-julius)
317
-
318
- ### v1.0.1
319
- - Fix Rails integration, as version 1.0.0 removed the rails tasks integration, but missed
320
- removing the related railtie.
321
-
322
- ### v1.0.0
323
- - support for cross process circuitbreakers by swapping the circuitbreaker store for a
324
- `Moneta` supported key value store.
325
- - Change `FaradayMiddleware` default behaviour to not open on `4xx` errors but just on `5xx`
326
- server errors and connection errors
327
- - Remove stat store, which was largely unused
328
-
329
- ### v0.11.0
330
- - fix URI require missing (https://github.com/yammer/circuitbox/pull/42 @gottfrois)
331
- - configurable circuitbox store backend via Moneta supporting multi process circuits
332
-
333
- ### v0.10.4
334
- - Issue #39, keep the original backtrace for the wrapped exception around when
335
- re-raising a Circuitbox::Error
336
-
337
- ### v0.10.3
338
- - Circuitbox::ServiceFailureError wraps the original exception that was raised.
339
- The behaviour for to_s wasn't exposing this information and was returning the
340
- name of class "Circuitbox::ServiceFailureError". Change the behaviour for to_s
341
- to indicate this exception is a wrapper around the original exception.
342
- [sherrry](https://github.com/sherrry)
343
-
344
- ### v0.10.2
345
- - Faraday middleware passes two arguments to the `default_value` callback, not
346
- just one. First argument is still the error response from Faraday if there is
347
- one. Second argument is the exception that caused the call to fail if it
348
- failed before Faraday returned a response. Old behaviour is preserved if you
349
- pass a lambda that takes just one argument, but this is deprecated and will be
350
- removed in the next version of Circuitbox.
351
- [dwaller](https://github.com/dwaller)
352
-
353
- ### v0.10.1
354
- - [Documentation fix](https://github.com/yammer/circuitbox/pull/29) [chiefcll](https://github.com/chiefcll)
355
- - [Faraday middleware fix](https://github.com/yammer/circuitbox/pull/30) [chiefcll](https://github.com/chiefcll)
356
-
357
- ### v0.10
358
- - configuration option for faraday middleware for what should be considered to open the circuit [enrico-scalavio](https://github.com/enrico-scalavino)
359
- - fix for issue 16, support of in_parallel requests in faraday middlware which were opening the circuit.
360
- - deprecate the __run_option__ `:storage_key`
361
-
362
- ### v0.9
363
- - add `run!` method to raise exception on circuit open and service
364
-
365
- ### v0.8
366
- - Everything prior to keeping the change log
367
-
368
237
  ## Installation
369
238
 
370
239
  Add this line to your application's Gemfile:
@@ -1,67 +1,24 @@
1
- require 'uri'
2
- require 'singleton'
3
- require 'active_support'
4
- require 'logger'
5
- require 'timeout'
6
- require 'moneta'
1
+ # frozen_string_literal: true
7
2
 
8
- require 'circuitbox/version'
9
- require 'circuitbox/memcache_store'
10
- require 'circuitbox/circuit_breaker'
11
- require 'circuitbox/notifier'
3
+ require 'logger'
12
4
 
13
- require 'circuitbox/errors/error'
14
- require 'circuitbox/errors/open_circuit_error'
15
- require 'circuitbox/errors/service_failure_error'
5
+ require_relative 'circuitbox/version'
6
+ require_relative 'circuitbox/circuit_breaker'
7
+ require_relative 'circuitbox/errors/error'
8
+ require_relative 'circuitbox/errors/open_circuit_error'
9
+ require_relative 'circuitbox/errors/service_failure_error'
10
+ require_relative 'circuitbox/configuration'
16
11
 
17
12
  class Circuitbox
18
- attr_accessor :circuits, :circuit_store
19
- cattr_accessor :configure
20
-
21
- def self.instance
22
- @@instance ||= new
23
- end
24
-
25
- def initialize
26
- self.instance_eval(&@@configure) if @@configure
27
- end
28
-
29
- def self.configure(&block)
30
- @@configure = block if block
31
- end
32
-
33
- def self.reset
34
- @@instance = nil
35
- @@configure = nil
36
- end
13
+ class << self
14
+ include Configuration
37
15
 
38
- def self.circuit_store
39
- self.instance.circuit_store ||= Moneta.new(:Memory, expires: true)
40
- end
16
+ def circuit(service_name, options, &block)
17
+ circuit = (cached_circuits[service_name] ||= CircuitBreaker.new(service_name, options))
41
18
 
42
- def self.circuit_store=(store)
43
- self.instance.circuit_store = store
44
- end
45
-
46
- def self.[](service_identifier, options = {})
47
- self.circuit(service_identifier, options)
48
- end
19
+ return circuit unless block
49
20
 
50
- def self.circuit(service_identifier, options = {})
51
- service_name = self.parameter_to_service_name(service_identifier)
52
-
53
- self.instance.circuits ||= Hash.new
54
- self.instance.circuits[service_name] ||= CircuitBreaker.new(service_name, options)
55
-
56
- if block_given?
57
- self.instance.circuits[service_name].run { yield }
58
- else
59
- self.instance.circuits[service_name]
21
+ circuit.run(circuitbox_exceptions: false, &block)
60
22
  end
61
23
  end
62
-
63
- def self.parameter_to_service_name(param)
64
- uri = URI(param.to_s)
65
- uri.host.present? ? uri.host : param.to_s
66
- end
67
24
  end