oso-cloud 1.9.0 → 1.9.1.pre.vendored.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/.gitignore +0 -1
- data/Gemfile +5 -0
- data/Gemfile.lock +31 -12
- data/lib/oso/api.rb +18 -2
- data/lib/oso/version.rb +1 -1
- data/vendor/gems/faraday-2.5.2/CHANGELOG.md +574 -0
- data/vendor/gems/faraday-2.5.2/LICENSE.md +20 -0
- data/vendor/gems/faraday-2.5.2/README.md +55 -0
- data/vendor/gems/faraday-2.5.2/Rakefile +7 -0
- data/vendor/gems/faraday-2.5.2/examples/client_spec.rb +119 -0
- data/vendor/gems/faraday-2.5.2/examples/client_test.rb +144 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/adapter/test.rb +298 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/adapter.rb +102 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/adapter_registry.rb +30 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/connection.rb +561 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/encoders/nested_params_encoder.rb +183 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/error.rb +147 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/logging/formatter.rb +106 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/methods.rb +6 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/middleware.rb +30 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/middleware_registry.rb +83 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/options/connection_options.rb +22 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/options/env.rb +199 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/options/proxy_options.rb +32 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/options/request_options.rb +22 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/options/ssl_options.rb +69 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/options.rb +218 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/parameters.rb +5 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/rack_builder.rb +252 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/request/authorization.rb +49 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/request/instrumentation.rb +56 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/request/json.rb +55 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/request/url_encoded.rb +60 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/request.rb +136 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/response/json.rb +54 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/response/logger.rb +33 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/response/raise_error.rb +64 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/response.rb +90 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/utils/headers.rb +139 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/utils/params_hash.rb +61 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/utils.rb +122 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/version.rb +5 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday.rb +157 -0
- data/vendor/gems/faraday-2.5.2/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/adapter/test_spec.rb +413 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/adapter_registry_spec.rb +28 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/adapter_spec.rb +55 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/connection_spec.rb +793 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/error_spec.rb +60 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/middleware_registry_spec.rb +31 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/middleware_spec.rb +52 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/options/env_spec.rb +76 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/options/options_spec.rb +297 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/options/proxy_options_spec.rb +44 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/options/request_options_spec.rb +19 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/params_encoders/flat_spec.rb +42 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/params_encoders/nested_spec.rb +150 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/rack_builder_spec.rb +317 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/request/authorization_spec.rb +83 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/request/instrumentation_spec.rb +74 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/request/json_spec.rb +111 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/request/url_encoded_spec.rb +93 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/request_spec.rb +110 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/response/json_spec.rb +117 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/response/logger_spec.rb +220 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/response/raise_error_spec.rb +172 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/response_spec.rb +75 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/utils/headers_spec.rb +82 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/utils_spec.rb +118 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday_spec.rb +37 -0
- data/vendor/gems/faraday-2.5.2/spec/spec_helper.rb +132 -0
- data/vendor/gems/faraday-2.5.2/spec/support/disabling_stub.rb +14 -0
- data/vendor/gems/faraday-2.5.2/spec/support/fake_safe_buffer.rb +15 -0
- data/vendor/gems/faraday-2.5.2/spec/support/helper_methods.rb +96 -0
- data/vendor/gems/faraday-2.5.2/spec/support/shared_examples/adapter.rb +105 -0
- data/vendor/gems/faraday-2.5.2/spec/support/shared_examples/params_encoder.rb +18 -0
- data/vendor/gems/faraday-2.5.2/spec/support/shared_examples/request_method.rb +263 -0
- data/vendor/gems/faraday-2.5.2/spec/support/streaming_response_checker.rb +35 -0
- data/vendor/gems/faraday-net_http-3.0.2/LICENSE.md +21 -0
- data/vendor/gems/faraday-net_http-3.0.2/README.md +57 -0
- data/vendor/gems/faraday-net_http-3.0.2/lib/faraday/adapter/net_http.rb +208 -0
- data/vendor/gems/faraday-net_http-3.0.2/lib/faraday/net_http/version.rb +7 -0
- data/vendor/gems/faraday-net_http-3.0.2/lib/faraday/net_http.rb +10 -0
- data/vendor/gems/faraday-net_http_persistent-2.3.0/LICENSE.md +21 -0
- data/vendor/gems/faraday-net_http_persistent-2.3.0/README.md +66 -0
- data/vendor/gems/faraday-net_http_persistent-2.3.0/lib/faraday/adapter/net_http_persistent.rb +234 -0
- data/vendor/gems/faraday-net_http_persistent-2.3.0/lib/faraday/net_http_persistent/version.rb +7 -0
- data/vendor/gems/faraday-net_http_persistent-2.3.0/lib/faraday/net_http_persistent.rb +18 -0
- data/vendor/gems/faraday-retry-2.0.0/CHANGELOG.md +24 -0
- data/vendor/gems/faraday-retry-2.0.0/LICENSE.md +21 -0
- data/vendor/gems/faraday-retry-2.0.0/README.md +169 -0
- data/vendor/gems/faraday-retry-2.0.0/lib/faraday/retriable_response.rb +8 -0
- data/vendor/gems/faraday-retry-2.0.0/lib/faraday/retry/middleware.rb +254 -0
- data/vendor/gems/faraday-retry-2.0.0/lib/faraday/retry/version.rb +7 -0
- data/vendor/gems/faraday-retry-2.0.0/lib/faraday/retry.rb +13 -0
- data/vendor/gems/net-http-persistent-4.0.5/.autotest +9 -0
- data/vendor/gems/net-http-persistent-4.0.5/.gemtest +0 -0
- data/vendor/gems/net-http-persistent-4.0.5/Gemfile +14 -0
- data/vendor/gems/net-http-persistent-4.0.5/History.txt +460 -0
- data/vendor/gems/net-http-persistent-4.0.5/Manifest.txt +13 -0
- data/vendor/gems/net-http-persistent-4.0.5/README.rdoc +82 -0
- data/vendor/gems/net-http-persistent-4.0.5/Rakefile +25 -0
- data/vendor/gems/net-http-persistent-4.0.5/lib/net/http/persistent/connection.rb +41 -0
- data/vendor/gems/net-http-persistent-4.0.5/lib/net/http/persistent/pool.rb +65 -0
- data/vendor/gems/net-http-persistent-4.0.5/lib/net/http/persistent/timed_stack_multi.rb +79 -0
- data/vendor/gems/net-http-persistent-4.0.5/lib/net/http/persistent.rb +1158 -0
- data/vendor/gems/net-http-persistent-4.0.5/test/test_net_http_persistent.rb +1512 -0
- data/vendor/gems/net-http-persistent-4.0.5/test/test_net_http_persistent_timed_stack_multi.rb +151 -0
- metadata +108 -4
@@ -0,0 +1,169 @@
|
|
1
|
+
# Faraday Retry
|
2
|
+
|
3
|
+
[](https://github.com/lostisland/faraday-retry/actions?query=branch%3Amain)
|
4
|
+
[](https://rubygems.org/gems/faraday-retry)
|
5
|
+
[](LICENSE.md)
|
6
|
+
|
7
|
+
The `Retry` middleware automatically retries requests that fail due to intermittent client
|
8
|
+
or server errors (such as network hiccups).
|
9
|
+
By default, it retries 2 times and handles only timeout exceptions.
|
10
|
+
It can be configured with an arbitrary number of retries, a list of exceptions to handle,
|
11
|
+
a retry interval, a percentage of randomness to add to the retry interval, and a backoff factor.
|
12
|
+
The middleware can also handle the [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After)
|
13
|
+
header automatically when configured with the right status codes (see below for an example).
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'faraday-retry'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
```shell
|
26
|
+
bundle install
|
27
|
+
```
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
```shell
|
32
|
+
gem install faraday-retry
|
33
|
+
```
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
This example will result in a first interval that is random between 0.05 and 0.075
|
38
|
+
and a second interval that is random between 0.1 and 0.125.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
require 'faraday'
|
42
|
+
require 'faraday/retry'
|
43
|
+
|
44
|
+
retry_options = {
|
45
|
+
max: 2,
|
46
|
+
interval: 0.05,
|
47
|
+
interval_randomness: 0.5,
|
48
|
+
backoff_factor: 2
|
49
|
+
}
|
50
|
+
|
51
|
+
conn = Faraday.new(...) do |f|
|
52
|
+
f.request :retry, retry_options
|
53
|
+
#...
|
54
|
+
end
|
55
|
+
|
56
|
+
conn.get('/')
|
57
|
+
```
|
58
|
+
|
59
|
+
### Control when the middleware will retry requests
|
60
|
+
|
61
|
+
By default, the `Retry` middleware will only retry idempotent methods and the most common network-related exceptions.
|
62
|
+
You can change this behaviour by providing the right option when adding the middleware to your connection.
|
63
|
+
|
64
|
+
#### Specify which methods will be retried
|
65
|
+
|
66
|
+
You can provide a `methods` option with a list of HTTP methods.
|
67
|
+
This will replace the default list of HTTP methods: `delete`, `get`, `head`, `options`, `put`.
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
retry_options = {
|
71
|
+
methods: %i[get post]
|
72
|
+
}
|
73
|
+
```
|
74
|
+
|
75
|
+
#### Specify which exceptions should trigger a retry
|
76
|
+
|
77
|
+
You can provide an `exceptions` option with a list of exceptions that will replace
|
78
|
+
the default list of network-related exceptions: `Errno::ETIMEDOUT`, `Timeout::Error`, `Faraday::TimeoutError`.
|
79
|
+
This can be particularly useful when combined with the [RaiseError][raise_error] middleware.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
retry_options = {
|
83
|
+
exceptions: [Faraday::ResourceNotFound, Faraday::UnauthorizedError]
|
84
|
+
}
|
85
|
+
```
|
86
|
+
|
87
|
+
#### Specify on which response statuses to retry
|
88
|
+
|
89
|
+
By default the `Retry` middleware will only retry the request if one of the expected exceptions arise.
|
90
|
+
However, you can specify a list of HTTP statuses you'd like to be retried. When you do so, the middleware will
|
91
|
+
check the response `status` code and will retry the request if included in the list.
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
retry_options = {
|
95
|
+
retry_statuses: [401, 409]
|
96
|
+
}
|
97
|
+
```
|
98
|
+
|
99
|
+
#### Automatically handle the `Retry-After` and `RateLimit-Reset` headers
|
100
|
+
|
101
|
+
Some APIs, like the [Slack API](https://api.slack.com/docs/rate-limits), will inform you when you reach their API limits by replying with a response status code of `429`
|
102
|
+
and a response header of `Retry-After` containing a time in seconds. You should then only retry querying after the amount of time provided by the `Retry-After` header,
|
103
|
+
otherwise you won't get a response. Other APIs communicate their rate limits via the [RateLimit-xxx](https://tools.ietf.org/id/draft-polli-ratelimit-headers-00.html#rfc.section.3.3) headers
|
104
|
+
where `RateLimit-Reset` behaves similarly to the `Retry-After`.
|
105
|
+
|
106
|
+
You can automatically handle both headers and have Faraday pause and retry for the right amount of time by including the `429` status code in the retry statuses list:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
retry_options = {
|
110
|
+
retry_statuses: [429]
|
111
|
+
}
|
112
|
+
```
|
113
|
+
|
114
|
+
#### Specify a custom retry logic
|
115
|
+
|
116
|
+
You can also specify a custom retry logic with the `retry_if` option.
|
117
|
+
This option accepts a block that will receive the `env` object and the exception raised
|
118
|
+
and should decide if the code should retry still the action or not independent of the retry count.
|
119
|
+
This would be useful if the exception produced is non-recoverable or if the the HTTP method called is not idempotent.
|
120
|
+
|
121
|
+
**NOTE:** this option will only be used for methods that are not included in the `methods` option.
|
122
|
+
If you want this to apply to all HTTP methods, pass `methods: []` as an additional option.
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
# Retries the request if response contains { success: false }
|
126
|
+
retry_options = {
|
127
|
+
retry_if: -> (env, _exc) { env.body[:success] == 'false' }
|
128
|
+
}
|
129
|
+
```
|
130
|
+
|
131
|
+
### Call a block on every retry
|
132
|
+
|
133
|
+
You can specify a proc object through the `retry_block` option that will be called before every
|
134
|
+
retry, before There are many different applications for this feature, spacing from instrumentation to monitoring.
|
135
|
+
|
136
|
+
|
137
|
+
The block is passed keyword arguments with contextual information: Request environment, middleware options, current number of retries, exception, and amount of time we will wait before retrying. (retry_block is called before the wait time happens)
|
138
|
+
|
139
|
+
|
140
|
+
For example, you might want to keep track of the response statuses:
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
response_statuses = []
|
144
|
+
retry_options = {
|
145
|
+
retry_block: -> (env:, options:, retries_remaining:, exception:, will_retry_in:) { response_statuses << env.status }
|
146
|
+
}
|
147
|
+
```
|
148
|
+
|
149
|
+
## Development
|
150
|
+
|
151
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
152
|
+
|
153
|
+
Then, run `bin/test` to run the tests.
|
154
|
+
|
155
|
+
To install this gem onto your local machine, run `rake build`.
|
156
|
+
|
157
|
+
### Releasing a new version
|
158
|
+
|
159
|
+
To release a new version, make a commit with a message such as "Bumped to 0.0.2", and change the _Unreleased_ heading in `CHANGELOG.md` to a heading like "0.0.2 (2022-01-01)", and then use GitHub Releases to author a release. A GitHub Actions workflow then publishes a new gem to [RubyGems.org](https://rubygems.org/gems/faraday-retry).
|
160
|
+
|
161
|
+
## Contributing
|
162
|
+
|
163
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/lostisland/faraday-retry).
|
164
|
+
|
165
|
+
## License
|
166
|
+
|
167
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
168
|
+
|
169
|
+
[raise_error]: https://lostisland.github.io/faraday/middleware/raise-error
|
@@ -0,0 +1,254 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
module Retry
|
5
|
+
# This class provides the main implementation for your middleware.
|
6
|
+
# Your middleware can implement any of the following methods:
|
7
|
+
# * on_request - called when the request is being prepared
|
8
|
+
# * on_complete - called when the response is being processed
|
9
|
+
#
|
10
|
+
# Optionally, you can also override the following methods from Faraday::Middleware
|
11
|
+
# * initialize(app, options = {}) - the initializer method
|
12
|
+
# * call(env) - the main middleware invocation method.
|
13
|
+
# This already calls on_request and on_complete, so you normally don't need to override it.
|
14
|
+
# You may need to in case you need to "wrap" the request or need more control
|
15
|
+
# (see "retry" middleware: https://github.com/lostisland/faraday/blob/main/lib/faraday/request/retry.rb#L142).
|
16
|
+
# IMPORTANT: Remember to call `@app.call(env)` or `super` to not interrupt the middleware chain!
|
17
|
+
class Middleware < Faraday::Middleware
|
18
|
+
DEFAULT_EXCEPTIONS = [
|
19
|
+
Errno::ETIMEDOUT, 'Timeout::Error',
|
20
|
+
Faraday::TimeoutError, Faraday::RetriableResponse
|
21
|
+
].freeze
|
22
|
+
IDEMPOTENT_METHODS = %i[delete get head options put].freeze
|
23
|
+
|
24
|
+
# Options contains the configurable parameters for the Retry middleware.
|
25
|
+
class Options < Faraday::Options.new(:max, :interval, :max_interval,
|
26
|
+
:interval_randomness,
|
27
|
+
:backoff_factor, :exceptions,
|
28
|
+
:methods, :retry_if, :retry_block,
|
29
|
+
:retry_statuses)
|
30
|
+
|
31
|
+
DEFAULT_CHECK = ->(_env, _exception) { false }
|
32
|
+
|
33
|
+
def self.from(value)
|
34
|
+
if value.is_a?(Integer)
|
35
|
+
new(value)
|
36
|
+
else
|
37
|
+
super(value)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def max
|
42
|
+
(self[:max] ||= 2).to_i
|
43
|
+
end
|
44
|
+
|
45
|
+
def interval
|
46
|
+
(self[:interval] ||= 0).to_f
|
47
|
+
end
|
48
|
+
|
49
|
+
def max_interval
|
50
|
+
(self[:max_interval] ||= Float::MAX).to_f
|
51
|
+
end
|
52
|
+
|
53
|
+
def interval_randomness
|
54
|
+
(self[:interval_randomness] ||= 0).to_f
|
55
|
+
end
|
56
|
+
|
57
|
+
def backoff_factor
|
58
|
+
(self[:backoff_factor] ||= 1).to_f
|
59
|
+
end
|
60
|
+
|
61
|
+
def exceptions
|
62
|
+
Array(self[:exceptions] ||= DEFAULT_EXCEPTIONS)
|
63
|
+
end
|
64
|
+
|
65
|
+
def methods
|
66
|
+
Array(self[:methods] ||= IDEMPOTENT_METHODS)
|
67
|
+
end
|
68
|
+
|
69
|
+
def retry_if
|
70
|
+
self[:retry_if] ||= DEFAULT_CHECK
|
71
|
+
end
|
72
|
+
|
73
|
+
def retry_block
|
74
|
+
self[:retry_block] ||= proc {}
|
75
|
+
end
|
76
|
+
|
77
|
+
def retry_statuses
|
78
|
+
Array(self[:retry_statuses] ||= [])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# @param app [#call]
|
83
|
+
# @param options [Hash]
|
84
|
+
# @option options [Integer] :max (2) Maximum number of retries
|
85
|
+
# @option options [Integer] :interval (0) Pause in seconds between retries
|
86
|
+
# @option options [Integer] :interval_randomness (0) The maximum random
|
87
|
+
# interval amount expressed as a float between
|
88
|
+
# 0 and 1 to use in addition to the interval.
|
89
|
+
# @option options [Integer] :max_interval (Float::MAX) An upper limit
|
90
|
+
# for the interval
|
91
|
+
# @option options [Integer] :backoff_factor (1) The amount to multiply
|
92
|
+
# each successive retry's interval amount by in order to provide backoff
|
93
|
+
# @option options [Array] :exceptions ([ Errno::ETIMEDOUT,
|
94
|
+
# 'Timeout::Error', Faraday::TimeoutError, Faraday::RetriableResponse])
|
95
|
+
# The list of exceptions to handle. Exceptions can be given as
|
96
|
+
# Class, Module, or String.
|
97
|
+
# @option options [Array] :methods (the idempotent HTTP methods
|
98
|
+
# in IDEMPOTENT_METHODS) A list of HTTP methods to retry without
|
99
|
+
# calling retry_if. Pass an empty Array to call retry_if
|
100
|
+
# for all exceptions.
|
101
|
+
# @option options [Block] :retry_if (false) block that will receive
|
102
|
+
# the env object and the exception raised
|
103
|
+
# and should decide if the code should retry still the action or
|
104
|
+
# not independent of the retry count. This would be useful
|
105
|
+
# if the exception produced is non-recoverable or if the
|
106
|
+
# the HTTP method called is not idempotent.
|
107
|
+
# @option options [Block] :retry_block block that is executed before
|
108
|
+
# every retry. The block will be yielded keyword arguments:
|
109
|
+
# * env [Faraday::Env]: Request environment
|
110
|
+
# * options [Faraday::Options]: middleware options
|
111
|
+
# * retry_count [Integer]: how many retries have already occured (starts at 0)
|
112
|
+
# * exception [Exception]: exception that triggered the retry,
|
113
|
+
# will be the synthetic `Faraday::RetriableResponse` if the
|
114
|
+
# retry was triggered by something other than an exception.
|
115
|
+
# * will_retry_in [Float]: retry_block is called *before* the retry
|
116
|
+
# delay, actual retry will happen in will_retry_in number of
|
117
|
+
# seconds.
|
118
|
+
# @option options [Array] :retry_statuses Array of Integer HTTP status
|
119
|
+
# codes or a single Integer value that determines whether to raise
|
120
|
+
# a Faraday::RetriableResponse exception based on the HTTP status code
|
121
|
+
# of an HTTP response.
|
122
|
+
def initialize(app, options = nil)
|
123
|
+
super(app)
|
124
|
+
@options = Options.from(options)
|
125
|
+
@errmatch = build_exception_matcher(@options.exceptions)
|
126
|
+
end
|
127
|
+
|
128
|
+
def calculate_sleep_amount(retries, env)
|
129
|
+
retry_after = [calculate_retry_after(env), calculate_rate_limit_reset(env)].compact.max
|
130
|
+
retry_interval = calculate_retry_interval(retries)
|
131
|
+
|
132
|
+
return if retry_after && retry_after > @options.max_interval
|
133
|
+
|
134
|
+
if retry_after && retry_after >= retry_interval
|
135
|
+
retry_after
|
136
|
+
else
|
137
|
+
retry_interval
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# @param env [Faraday::Env]
|
142
|
+
def call(env)
|
143
|
+
retries = @options.max
|
144
|
+
request_body = env[:body]
|
145
|
+
begin
|
146
|
+
# after failure env[:body] is set to the response body
|
147
|
+
env[:body] = request_body
|
148
|
+
@app.call(env).tap do |resp|
|
149
|
+
raise Faraday::RetriableResponse.new(nil, resp) if @options.retry_statuses.include?(resp.status)
|
150
|
+
end
|
151
|
+
rescue @errmatch => e
|
152
|
+
if retries.positive? && retry_request?(env, e)
|
153
|
+
retries -= 1
|
154
|
+
rewind_files(request_body)
|
155
|
+
if (sleep_amount = calculate_sleep_amount(retries + 1, env))
|
156
|
+
@options.retry_block.call(
|
157
|
+
env: env,
|
158
|
+
options: @options,
|
159
|
+
retry_count: @options.max - (retries + 1),
|
160
|
+
exception: e,
|
161
|
+
will_retry_in: sleep_amount
|
162
|
+
)
|
163
|
+
sleep sleep_amount
|
164
|
+
retry
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
raise unless e.is_a?(Faraday::RetriableResponse)
|
169
|
+
|
170
|
+
e.response
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# An exception matcher for the rescue clause can usually be any object
|
175
|
+
# that responds to `===`, but for Ruby 1.8 it has to be a Class or Module.
|
176
|
+
#
|
177
|
+
# @param exceptions [Array]
|
178
|
+
# @api private
|
179
|
+
# @return [Module] an exception matcher
|
180
|
+
def build_exception_matcher(exceptions)
|
181
|
+
matcher = Module.new
|
182
|
+
(
|
183
|
+
class << matcher
|
184
|
+
self
|
185
|
+
end).class_eval do
|
186
|
+
define_method(:===) do |error|
|
187
|
+
exceptions.any? do |ex|
|
188
|
+
if ex.is_a? Module
|
189
|
+
error.is_a? ex
|
190
|
+
else
|
191
|
+
Object.const_defined?(ex.to_s) && error.is_a?(Object.const_get(ex.to_s))
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
matcher
|
197
|
+
end
|
198
|
+
|
199
|
+
private
|
200
|
+
|
201
|
+
def retry_request?(env, exception)
|
202
|
+
@options.methods.include?(env[:method]) ||
|
203
|
+
@options.retry_if.call(env, exception)
|
204
|
+
end
|
205
|
+
|
206
|
+
def rewind_files(body)
|
207
|
+
return unless defined?(UploadIO)
|
208
|
+
return unless body.is_a?(Hash)
|
209
|
+
|
210
|
+
body.each do |_, value|
|
211
|
+
value.rewind if value.is_a?(UploadIO)
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
# RFC for RateLimit Header Fields for HTTP:
|
216
|
+
# https://tools.ietf.org/id/draft-polli-ratelimit-headers-00.html#rfc.section.3.3
|
217
|
+
def calculate_rate_limit_reset(env)
|
218
|
+
parse_retry_header(env, 'RateLimit-Reset')
|
219
|
+
end
|
220
|
+
|
221
|
+
# MDN spec for Retry-After header:
|
222
|
+
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
|
223
|
+
def calculate_retry_after(env)
|
224
|
+
parse_retry_header(env, 'Retry-After')
|
225
|
+
end
|
226
|
+
|
227
|
+
def calculate_retry_interval(retries)
|
228
|
+
retry_index = @options.max - retries
|
229
|
+
current_interval = @options.interval *
|
230
|
+
(@options.backoff_factor**retry_index)
|
231
|
+
current_interval = [current_interval, @options.max_interval].min
|
232
|
+
random_interval = rand * @options.interval_randomness.to_f *
|
233
|
+
@options.interval
|
234
|
+
|
235
|
+
current_interval + random_interval
|
236
|
+
end
|
237
|
+
|
238
|
+
def parse_retry_header(env, header)
|
239
|
+
response_headers = env[:response_headers]
|
240
|
+
return unless response_headers
|
241
|
+
|
242
|
+
retry_after_value = env[:response_headers][header]
|
243
|
+
|
244
|
+
# Try to parse date from the header value
|
245
|
+
begin
|
246
|
+
datetime = DateTime.rfc2822(retry_after_value)
|
247
|
+
datetime.to_time - Time.now.utc
|
248
|
+
rescue ArgumentError
|
249
|
+
retry_after_value.to_f
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
require_relative 'retriable_response'
|
5
|
+
require_relative 'retry/middleware'
|
6
|
+
require_relative 'retry/version'
|
7
|
+
|
8
|
+
module Faraday
|
9
|
+
# Middleware main module.
|
10
|
+
module Retry
|
11
|
+
Faraday::Request.register_middleware(retry: Faraday::Retry::Middleware)
|
12
|
+
end
|
13
|
+
end
|
File without changes
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
source "https://rubygems.org/"
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
gem "rake", "~>13.0"
|
8
|
+
gem "minitest", "~>5.15", :group => [:development, :test]
|
9
|
+
gem "rdoc", ">=4.0", "<7", :group => [:development, :test]
|
10
|
+
gem "rake-manifest", "~>0.2"
|
11
|
+
|
12
|
+
gem 'net-http-pipeline', '~> 1.0' if ENV['CI_MATRIX'] == 'pipeline'
|
13
|
+
|
14
|
+
# vim: syntax=ruby
|