faraday_middleware-circuit_breaker 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8deaaad1b7866b3bb93efe91ae63f492e446f382
4
- data.tar.gz: 4c79b86afe36d6ae42733d83ff21beac8365284c
2
+ SHA256:
3
+ metadata.gz: 58839ba030df8c51e5a2bda81aad28ba586fbe0a3f0a3cffc817a8f171ea830c
4
+ data.tar.gz: 053b87ffe546b0ec2c42db10ce41b7dc6a890f1d1c7b08793858f2599190c4f5
5
5
  SHA512:
6
- metadata.gz: 0a1267f7b41141ff8f6c02dc96a5ae199d2ff5b2cc6b3e83e242d1eade766a0a24de721c3602525fd38867cf192df2ebb627fca73cd376d4d1b745ce31eb7387
7
- data.tar.gz: 2361860fcffd409f10db8ac07265116bedd52c0bc2947645ae4f00c2e4b802936679cbced2faf4f74a7c0d6b1688cf5737625df2910384658a227c5fb94ab2d8
6
+ metadata.gz: 5aaf8fa2fd96875159b0bc42537a5211dd33dd975a4731f80c04bdb43ec848cfb5c6a646727b128ef90aefc9cccbf58224347fec01a4f42df05a7f8bf849a3c7
7
+ data.tar.gz: eb314d3a44362297d1c9ba025444abb6d1c26e61ac36b0e669291d36aa15f8436b54ef24d6dc27790b1981d225abd303e51f0f0b7fb70860448b5f5aac3f6d00
@@ -0,0 +1,50 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ - push
12
+
13
+ jobs:
14
+ test:
15
+
16
+ runs-on: ubuntu-latest
17
+
18
+ strategy:
19
+ matrix:
20
+ ruby:
21
+ - "2.4"
22
+ - "2.5"
23
+ - "2.6"
24
+ - "2.7"
25
+
26
+ name: ${{ matrix.ruby }}
27
+
28
+ steps:
29
+ - uses: actions/checkout@v2
30
+
31
+ - name: Set up Ruby
32
+ uses: ruby/setup-ruby@v1
33
+ with:
34
+ ruby-version: ${{ matrix.ruby }}
35
+
36
+ - name: Set up Cache
37
+ uses: actions/cache@v2
38
+ with:
39
+ path: vendor/bundle
40
+ key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
41
+ restore-keys: |
42
+ ${{ runner.os }}-gems-
43
+
44
+ - name: Install dependencies
45
+ run: |
46
+ bundle config path vendor/bundle
47
+ bundle install --jobs 4 --retry 3
48
+
49
+ - name: Run tests
50
+ run: bundle exec rake
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .ruby-version
11
+ .rbenv-gemsets
@@ -1,7 +1,27 @@
1
- # 0.3.0
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
2
3
 
3
- * Handles stoplight data store
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
4
6
 
5
- # 0.2.0
7
+ ## [Unreleased]
8
+
9
+ ## [0.4.0]
10
+ ### Added
11
+
12
+ - Handle stoplight custom error handlers. Fixes https://github.com/textmaster/faraday_middleware-circuit_breaker/issues/3
13
+
14
+ ## [0.3.0]
15
+ ### Added
16
+ - Handles stoplight data store
17
+
18
+ ## [0.2.0]
19
+ ### Added
6
20
 
7
21
  * Introduces sentry/raven notifier
22
+
23
+ [Unreleased]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.4.0...HEAD
24
+ [0.4.0]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.3.0...v0.4.0
25
+ [0.3.0]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.2.0...v0.3.0
26
+ [0.2.0]: https://github.com/textmaster/faraday_middleware-circuit_breaker/compare/v0.1.0...v0.2.0
27
+ [0.0.1]: https://github.com/textmaster/faraday_middleware-circuit_breaker/releases/tag/v0.1.0
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in faraday_middleware-circuit_breaker.gemspec
4
4
  gemspec
5
+
6
+ gem "rake", "~> 12.0"
data/README.md CHANGED
@@ -57,7 +57,7 @@ The default is `3` times.
57
57
 
58
58
  ### Custom fallback
59
59
 
60
- On a failure, middlware will render an empty `503` http response by default. You can customize the fallback response:
60
+ On a failure, middleware will render an empty `503` http response by default. You can customize the fallback response:
61
61
 
62
62
  ```ruby
63
63
  Faraday.new(url: 'http://foo.com') do |c|
@@ -88,6 +88,40 @@ Whatever you chose, your method should return a valid faraday response. For exam
88
88
  proc { Faraday::Response.new(status: 503, response_headers: {}) }
89
89
  ```
90
90
 
91
+ ### Custom error handling
92
+
93
+ In some situations, it might required to allow for particular error types to be exempt from tripping the circuit breaker
94
+ (like regular 403 or 401 HTTP responses, which aren't really out-of-the-ordinary conditions that should trip the circuit breaker).
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:
97
+
98
+ ```ruby
99
+ Faraday.new(url: 'http://foo.com') do |c|
100
+ c.use :circuit_breaker, error_handler: ->(exception, handler) { # do something }
101
+ end
102
+ ```
103
+
104
+ Middleware will try to call the `call` method on `error_handler` passing 2 arguments:
105
+
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
108
+
109
+ You can pass a method to be eager called like this (with a handler that exempts `ArgumentError` from tripping the circuit):
110
+
111
+ ```ruby
112
+ Faraday.new(url: 'http://foo.com') do |c|
113
+ c.use :circuit_breaker, error_handler: method(:foo)
114
+ end
115
+
116
+ def foo(exception, handler)
117
+ raise exception if exception.is_a?(ArgumentError)
118
+ handler.call(exception)
119
+ end
120
+ ```
121
+
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
+ 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
+
91
125
  ### Notifiers
92
126
 
93
127
  Middleware send notifications to standard error by default. You can customize the receivers.
data/Rakefile CHANGED
@@ -3,4 +3,4 @@ require "rspec/core/rake_task"
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -1,7 +1,4 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'faraday_middleware/circuit_breaker/version'
1
+ require_relative 'lib/faraday_middleware/circuit_breaker/version'
5
2
 
6
3
  Gem::Specification.new do |spec|
7
4
  spec.name = "faraday_middleware-circuit_breaker"
@@ -13,16 +10,25 @@ Gem::Specification.new do |spec|
13
10
  spec.description = %q{A Faraday Middleware to handle spotty web services.}
14
11
  spec.homepage = "https://github.com/textmaster/faraday_middleware-circuit_breaker"
15
12
  spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
14
 
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "bin"
19
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/textmaster/faraday_middleware-circuit_breaker"
19
+ spec.metadata["changelog_uri"] = "https://github.com/textmaster/faraday_middleware-circuit_breaker/blob/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
28
  spec.require_paths = ["lib"]
21
29
 
22
- spec.add_dependency 'faraday', '~> 0.9'
30
+ spec.add_dependency 'faraday', '>= 0.9', '< 2.0'
23
31
  spec.add_dependency 'stoplight', '~> 2.1'
24
32
 
25
- spec.add_development_dependency 'bundler', '~> 1.10'
26
- spec.add_development_dependency 'rake', '~> 11.2'
27
33
  spec.add_development_dependency 'rspec'
28
34
  end
@@ -17,12 +17,14 @@ module FaradayMiddleware
17
17
  end
18
18
 
19
19
  def call(env)
20
- Stoplight(env.url.to_s) do
20
+ base_url = URI.join(env.url, '/')
21
+ Stoplight(base_url.to_s) do
21
22
  @app.call(env)
22
23
  end
23
24
  .with_cool_off_time(option_set.timeout)
24
25
  .with_threshold(option_set.threshold)
25
26
  .with_fallback { |e| option_set.fallback.call(env, e) }
27
+ .with_error_handler { |err, handler| option_set.error_handler.call(err, handler) }
26
28
  .run
27
29
  end
28
30
 
@@ -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)
9
+ VALID_OPTIONS = %w(timeout threshold fallback notifiers data_store, error_handler)
10
10
 
11
- attr_accessor :timeout, :threshold, :fallback, :notifiers, :data_store
11
+ attr_accessor :timeout, :threshold, :fallback, :notifiers, :data_store, :error_handler
12
12
 
13
13
  def initialize(options = {})
14
14
  @timeout = options[:timeout] || 60.0
@@ -16,6 +16,7 @@ module FaradayMiddleware
16
16
  @fallback = options[:fallback] || proc { Faraday::Response.new(status: 503, response_headers: {}) }
17
17
  @notifiers = options[:notifiers] || {}
18
18
  @data_store = options[:data_store] || proc { Stoplight::Light.default_data_store }
19
+ @error_handler = options[:error_handler] || Stoplight::Default::ERROR_HANDLER
19
20
  end
20
21
 
21
22
  def self.validate!(options)
@@ -1,5 +1,5 @@
1
1
  module FaradayMiddleware
2
2
  module CircuitBreaker
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday_middleware-circuit_breaker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre-Louis Gottfrois
8
- autorequire:
9
- bindir: bin
8
+ autorequire:
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-09 00:00:00.000000000 Z
11
+ date: 2020-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0.9'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '0.9'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: stoplight
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -38,34 +44,6 @@ dependencies:
38
44
  - - "~>"
39
45
  - !ruby/object:Gem::Version
40
46
  version: '2.1'
41
- - !ruby/object:Gem::Dependency
42
- name: bundler
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '1.10'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '1.10'
55
- - !ruby/object:Gem::Dependency
56
- name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '11.2'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '11.2'
69
47
  - !ruby/object:Gem::Dependency
70
48
  name: rspec
71
49
  requirement: !ruby/object:Gem::Requirement
@@ -83,12 +61,11 @@ dependencies:
83
61
  description: A Faraday Middleware to handle spotty web services.
84
62
  email:
85
63
  - pierre-louis@textmaster.com
86
- executables:
87
- - console
88
- - setup
64
+ executables: []
89
65
  extensions: []
90
66
  extra_rdoc_files: []
91
67
  files:
68
+ - ".github/workflows/ruby.yml"
92
69
  - ".gitignore"
93
70
  - ".rspec"
94
71
  - ".travis.yml"
@@ -107,8 +84,12 @@ files:
107
84
  homepage: https://github.com/textmaster/faraday_middleware-circuit_breaker
108
85
  licenses:
109
86
  - MIT
110
- metadata: {}
111
- post_install_message:
87
+ metadata:
88
+ allowed_push_host: https://rubygems.org
89
+ homepage_uri: https://github.com/textmaster/faraday_middleware-circuit_breaker
90
+ source_code_uri: https://github.com/textmaster/faraday_middleware-circuit_breaker
91
+ changelog_uri: https://github.com/textmaster/faraday_middleware-circuit_breaker/blob/master/CHANGELOG.md
92
+ post_install_message:
112
93
  rdoc_options: []
113
94
  require_paths:
114
95
  - lib
@@ -116,16 +97,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
97
  requirements:
117
98
  - - ">="
118
99
  - !ruby/object:Gem::Version
119
- version: '0'
100
+ version: 2.4.0
120
101
  required_rubygems_version: !ruby/object:Gem::Requirement
121
102
  requirements:
122
103
  - - ">="
123
104
  - !ruby/object:Gem::Version
124
105
  version: '0'
125
106
  requirements: []
126
- rubyforge_project:
127
- rubygems_version: 2.6.13
128
- signing_key:
107
+ rubygems_version: 3.0.8
108
+ signing_key:
129
109
  specification_version: 4
130
110
  summary: Middleware to apply circuit breaker pattern.
131
111
  test_files: []