faraday_middleware-circuit_breaker 0.4.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -2
- data/README.md +64 -4
- data/faraday_middleware-circuit_breaker.gemspec +2 -2
- data/lib/faraday_middleware/circuit_breaker/middleware.rb +2 -2
- data/lib/faraday_middleware/circuit_breaker/option_set.rb +3 -2
- data/lib/faraday_middleware/circuit_breaker/version.rb +1 -1
- metadata +13 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d5a3dccaa9bcc535cc6bfc148b6053694f1b00ae665fef5e804d5ba62ae06d03
|
4
|
+
data.tar.gz: '09ac266ab4de9e01bd451a0e74bf9fdaf69d6fd8e5d28d6c7b3307a817fbd43f'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98b77c124228e11873cba284f6cb21e7a967f2d150e9935fef813c7e9edc29f217585bf9e3fc8680995d638a754e377a4ed4aa7252419b25fcbb8ac8c4163901
|
7
|
+
data.tar.gz: a4fef74912ba82159af427ebbbd92b73cf03d97f8520b8bce434f84c710a2d3acdb9cd250e16067edffbd5bf384d94b57983ee377188327bbb6059d8bdf260f3
|
data/CHANGELOG.md
CHANGED
@@ -1,11 +1,26 @@
|
|
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.
|
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
|
+
|
18
|
+
## [0.5.0]
|
19
|
+
|
20
|
+
### Changed
|
21
|
+
|
22
|
+
- Relaxed version requirement for Stoplight to include 3.0
|
23
|
+
|
9
24
|
## [0.4.1]
|
10
25
|
### Fixed
|
11
26
|
|
@@ -26,7 +41,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
26
41
|
|
27
42
|
* Introduces sentry/raven notifier
|
28
43
|
|
29
|
-
[Unreleased]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.
|
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
|
46
|
+
[0.5.0]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.4.1...v0.5.0
|
47
|
+
[0.4.1]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.4.0...v0.4.1
|
30
48
|
[0.4.0]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.3.0...v0.4.0
|
31
49
|
[0.3.0]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.2.0...v0.3.0
|
32
50
|
[0.2.0]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.1.0...v0.2.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,8 +27,8 @@ 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', '<
|
31
|
-
spec.add_dependency 'stoplight', '
|
30
|
+
spec.add_dependency 'faraday', '>= 0.9', '< 3.0'
|
31
|
+
spec.add_dependency 'stoplight', '>= 2.1', '< 4.0'
|
32
32
|
|
33
33
|
spec.add_development_dependency 'rspec'
|
34
34
|
end
|
@@ -17,8 +17,8 @@ module FaradayMiddleware
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def call(env)
|
20
|
-
base_url =
|
21
|
-
Stoplight(base_url
|
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)
|
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.
|
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:
|
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: '
|
22
|
+
version: '3.0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,21 +29,27 @@ dependencies:
|
|
29
29
|
version: '0.9'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '3.0'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: stoplight
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "
|
37
|
+
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: '2.1'
|
40
|
+
- - "<"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '4.0'
|
40
43
|
type: :runtime
|
41
44
|
prerelease: false
|
42
45
|
version_requirements: !ruby/object:Gem::Requirement
|
43
46
|
requirements:
|
44
|
-
- - "
|
47
|
+
- - ">="
|
45
48
|
- !ruby/object:Gem::Version
|
46
49
|
version: '2.1'
|
50
|
+
- - "<"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '4.0'
|
47
53
|
- !ruby/object:Gem::Dependency
|
48
54
|
name: rspec
|
49
55
|
requirement: !ruby/object:Gem::Requirement
|
@@ -104,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
110
|
- !ruby/object:Gem::Version
|
105
111
|
version: '0'
|
106
112
|
requirements: []
|
107
|
-
rubygems_version: 3.
|
113
|
+
rubygems_version: 3.4.10
|
108
114
|
signing_key:
|
109
115
|
specification_version: 4
|
110
116
|
summary: Middleware to apply circuit breaker pattern.
|