faraday_middleware-circuit_breaker 0.5.0 → 0.6.0

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
  SHA256:
3
- metadata.gz: 005afebe17fb37c37d260ca776c581cc92cda261b7cafc241d6d4fb7ce39d860
4
- data.tar.gz: e54b70864d6a74e8b13414c8af69dfb9ec5040a7c883663b088478ac523f19e3
3
+ metadata.gz: d5a3dccaa9bcc535cc6bfc148b6053694f1b00ae665fef5e804d5ba62ae06d03
4
+ data.tar.gz: '09ac266ab4de9e01bd451a0e74bf9fdaf69d6fd8e5d28d6c7b3307a817fbd43f'
5
5
  SHA512:
6
- metadata.gz: 81884db0ec02ed879a7e09d71b370a16a64a23daa183c9d9cfeb2db8650bc4f11339c7a83702f169e814ae048723caa2bb7ecf7c6426055f8a18ad9e4b978e49
7
- data.tar.gz: d4f2942b93d59d50f9a994a9e86625a52307a38356b3e40fbf7454ca54c0774da2ca75667babab2fdd2950c71883ee457d49a622f892e50b1cb4fa5c2f3c2b0e
6
+ metadata.gz: 98b77c124228e11873cba284f6cb21e7a967f2d150e9935fef813c7e9edc29f217585bf9e3fc8680995d638a754e377a4ed4aa7252419b25fcbb8ac8c4163901
7
+ data.tar.gz: a4fef74912ba82159af427ebbbd92b73cf03d97f8520b8bce434f84c710a2d3acdb9cd250e16067edffbd5bf384d94b57983ee377188327bbb6059d8bdf260f3
data/CHANGELOG.md CHANGED
@@ -1,11 +1,20 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.6.0]
10
+ ### Added
11
+
12
+ - Merged [12](https://github.com/textmaster/faraday_middleware-circuit_breaker/pull/12) which allows to customize the cache key for the circuit breaker
13
+
14
+ ### Changed
15
+
16
+ - Relaxed version requirement for Faraday to include 2.x
17
+
9
18
  ## [0.5.0]
10
19
 
11
20
  ### Changed
@@ -32,7 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
32
41
 
33
42
  * Introduces sentry/raven notifier
34
43
 
35
- [Unreleased]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.5.0...HEAD
44
+ [Unreleased]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.6.0...HEAD
45
+ [0.6.0]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.4.1...v0.6.0
36
46
  [0.5.0]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.4.1...v0.5.0
37
47
  [0.4.1]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.4.0...v0.4.1
38
48
  [0.4.0]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.3.0...v0.4.0
data/README.md CHANGED
@@ -93,7 +93,7 @@ proc { Faraday::Response.new(status: 503, response_headers: {}) }
93
93
  In some situations, it might required to allow for particular error types to be exempt from tripping the circuit breaker
94
94
  (like regular 403 or 401 HTTP responses, which aren't really out-of-the-ordinary conditions that should trip the circuit breaker).
95
95
  The underlying stoplight gem supports [custom error handling](https://github.com/orgsync/stoplight#custom-errors),
96
- The `error_handler` option allows you to add your own customer error handler behavior:
96
+ The `error_handler` option allows you to add your own customer error handler behavior:
97
97
 
98
98
  ```ruby
99
99
  Faraday.new(url: 'http://foo.com') do |c|
@@ -104,7 +104,7 @@ end
104
104
  Middleware will try to call the `call` method on `error_handler` passing 2 arguments:
105
105
 
106
106
  - `exception` -- the exception raised that triggered the circuit breaker
107
- - `handler` -- the current error handler `Proc` that would be in charge of handling the `exception` if no `error_handler` option was passed
107
+ - `handler` -- the current error handler `Proc` that would be in charge of handling the `exception` if no `error_handler` option was passed
108
108
 
109
109
  You can pass a method to be eager called like this (with a handler that exempts `ArgumentError` from tripping the circuit):
110
110
 
@@ -122,6 +122,52 @@ end
122
122
  NOTE: It is most always a good idea to call the original `handler` with the exception that was passed in at the end of your
123
123
  handler. (By default, the `error_handler` will just be [`Stoplight::Default::ERROR_HANDLER`](https://github.com/orgsync/stoplight/blob/master/lib/stoplight/default.rb#L9))
124
124
 
125
+ ### Custom Stoplight key
126
+
127
+ By default, the circuit breaker will count failures by domain, but this logic can be tweak by passing a lambda to the `cache_key_generator` option.
128
+ The lambda will receive the [URI](https://docs.ruby-lang.org/en/2.1.0/URI.html) that Faraday is trying to call, and whatever string it returns will be used as the key to count the errors,
129
+ and all URI with the same key will trip together.
130
+
131
+ The default behaviour is:
132
+
133
+ ```ruby
134
+ Faraday.new(url: 'http://foo.com/bar') do |c|
135
+ c.use :circuit_breaker, cache_key_generator: ->(url) { URI.join(url, '/').to_s }
136
+ end
137
+ ```
138
+
139
+ But for instance if when `http://foo.com/bar?id=1` trips you also want `http://foo.com/bar?id=2` to be tripped but `http://foo.com/foo` to go through, then you could pass the following:
140
+
141
+ ```ruby
142
+ Faraday.new(url: 'http://foo.com/bar') do |c|
143
+ c.use :circuit_breaker, cache_key_generator: lambda do |url|
144
+ base_url = url.clone
145
+ base_url.fragment = base_url.query = nil
146
+ base_url.to_s
147
+ end
148
+ end
149
+ ```
150
+
151
+ Because the key is a simple string, it doesn't have to be constructed from the URI directly, so the following is also valid:
152
+
153
+ ```ruby
154
+ Faraday.new(url: 'http://foo.com/bar') do |c|
155
+ c.use :circuit_breaker, cache_key_generator: lambda do |url|
156
+ if url.hostname == 'api.mydomain.com'
157
+ if url.path.start_with? "/users"
158
+ return "user_service"
159
+ elsif url.path.start_with? "/orders"
160
+ return "orders_service"
161
+ else
162
+ return "other_service"
163
+ end
164
+ end
165
+
166
+ URI.join(url, '/').to_s
167
+ end
168
+ end
169
+ ```
170
+
125
171
  ### Notifiers
126
172
 
127
173
  Middleware send notifications to standard error by default. You can customize the receivers.
@@ -196,6 +242,22 @@ end
196
242
 
197
243
  You'll need to have [Bugsnag](https://rubygems.org/gems/bugsnag) gem installed.
198
244
 
245
+ #### Sentry
246
+
247
+ To send notifications to sentry:
248
+
249
+ ```ruby
250
+ require 'sentry-raven'
251
+
252
+ sentry_raven = Raven::Configuration.new
253
+
254
+ Faraday.new(url: 'http://foo.com') do |c|
255
+ c.use :circuit_breaker, notifiers: { sentry: sentry_raven } # or { raven: sentry_raven }
256
+ end
257
+ ```
258
+
259
+ You'll need to have [Sentry](https://rubygems.org/gems/sentry-raven) gem installed.
260
+
199
261
  ## Development
200
262
 
201
263
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -206,8 +268,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
206
268
 
207
269
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/faraday_middleware-circuit_breaker.
208
270
 
209
-
210
271
  ## License
211
272
 
212
273
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
213
-
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
29
 
30
- spec.add_dependency 'faraday', '>= 0.9', '< 2.0'
30
+ spec.add_dependency 'faraday', '>= 0.9', '< 3.0'
31
31
  spec.add_dependency 'stoplight', '>= 2.1', '< 4.0'
32
32
 
33
33
  spec.add_development_dependency 'rspec'
@@ -17,8 +17,8 @@ module FaradayMiddleware
17
17
  end
18
18
 
19
19
  def call(env)
20
- base_url = URI.join(env.url, '/')
21
- Stoplight(base_url.to_s) do
20
+ base_url = option_set.cache_key_generator.call(env.url)
21
+ Stoplight(base_url) do
22
22
  @app.call(env)
23
23
  end
24
24
  .with_cool_off_time(option_set.timeout)
@@ -6,9 +6,9 @@ module FaradayMiddleware
6
6
  module CircuitBreaker
7
7
  class OptionSet
8
8
 
9
- VALID_OPTIONS = %w(timeout threshold fallback notifiers data_store error_handler)
9
+ VALID_OPTIONS = %w(timeout threshold fallback notifiers data_store error_handler cache_key_generator)
10
10
 
11
- attr_accessor :timeout, :threshold, :fallback, :notifiers, :data_store, :error_handler
11
+ attr_accessor :timeout, :threshold, :fallback, :notifiers, :data_store, :error_handler, :cache_key_generator
12
12
 
13
13
  def initialize(options = {})
14
14
  @timeout = options[:timeout] || 60.0
@@ -17,6 +17,7 @@ module FaradayMiddleware
17
17
  @notifiers = options[:notifiers] || {}
18
18
  @data_store = options[:data_store] || proc { Stoplight::Light.default_data_store }
19
19
  @error_handler = options[:error_handler] || Stoplight::Default::ERROR_HANDLER
20
+ @cache_key_generator = options[:cache_key_generator] || ->(url) { URI.join(url, '/').to_s }
20
21
  end
21
22
 
22
23
  def self.validate!(options)
@@ -1,5 +1,5 @@
1
1
  module FaradayMiddleware
2
2
  module CircuitBreaker
3
- VERSION = "0.5.0".freeze
3
+ VERSION = "0.6.0".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday_middleware-circuit_breaker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre-Louis Gottfrois
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-27 00:00:00.000000000 Z
11
+ date: 2024-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '0.9'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '2.0'
22
+ version: '3.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '0.9'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '2.0'
32
+ version: '3.0'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: stoplight
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
112
  requirements: []
113
- rubygems_version: 3.0.3
113
+ rubygems_version: 3.4.10
114
114
  signing_key:
115
115
  specification_version: 4
116
116
  summary: Middleware to apply circuit breaker pattern.