faraday-retry 2.2.1 → 2.3.2

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
2
  SHA256:
3
- metadata.gz: 4215b6f0b7dd61fa8702103e77d6373cace2c46b469defeb0ed93ce94aa26e6c
4
- data.tar.gz: 295faca61565e49a36a7f03201d6aee90b6a353c40d22a8bf8e1f6b1c729c041
3
+ metadata.gz: 7e0d25040c1d5a44f97588ae549113ddc1f5605575982a9914bbcf04ba75479e
4
+ data.tar.gz: 9894f02b5c148166cf87eba7b80399ab52d6ab52d62a754c7cdb923cb77b55d6
5
5
  SHA512:
6
- metadata.gz: 5b8f2bee0e35492efc578659ce28a0b947374c8cafdba07606d953c417e8844128248a639392e4ad60c682528c0d478d3a894ca688e1f72965172633aee3be7f
7
- data.tar.gz: 148ac2094f76cd59ea9278a4d1948b3211f0c2abc1876b6f10fbdd88a384257dfdfab33f9a1cf102ed23352b5b0dd2e9f1f4d533cd8459c4b86d1f5e7578a053
6
+ metadata.gz: 6166b5f81ddc21739eb1654a2e8aa55191ecb275f4299d752980df1b7220c7698703fadeee074454da963ca0877dd00283105f0e1f423e6043735f042c984ea2
7
+ data.tar.gz: c6e11e52b1007c9dc6bca895b89a06638b4269bc7f19f89d5916e6c81136958c49f2095dd6cc8bc2e1391ba4dcd47d5879931cdf19a68a8f0608a0e11c82bc05
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ _nothing yet_
6
+
7
+ ## v2.2.1 (2024-04-15)
8
+
5
9
  * Avoid deprecation warning about ::UploadIO constant when used without faraday-multipart gem [PR #37](https://github.com/lostisland/faraday-retry/pull/37) [@iMacTia]
6
10
  * Documentation update [PR #30](https://github.com/lostisland/faraday-retry/pull/30) [@olleolleolle]
7
11
  * Documentation update [PR #32](https://github.com/lostisland/faraday-retry/pull/32) Thanks, [@Drowze]!
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Faraday Retry
2
2
 
3
- [![CI](https://github.com/lostisland/faraday-retry/actions/workflows/ci.yaml/badge.svg)](https://github.com/lostisland/faraday-retry/actions/workflows/ci.yaml)
3
+ [![CI](https://github.com/lostisland/faraday-retry/actions/workflows/ci.yml/badge.svg)](https://github.com/lostisland/faraday-retry/actions/workflows/ci.yml)
4
4
  [![Gem](https://img.shields.io/gem/v/faraday-retry.svg?style=flat-square)](https://rubygems.org/gems/faraday-retry)
5
5
  [![License](https://img.shields.io/github/license/lostisland/faraday-retry.svg?style=flat-square)](LICENSE.md)
6
6
 
@@ -149,8 +149,8 @@ retry_options = {
149
149
 
150
150
  ### Call a block on every retry
151
151
 
152
- You can specify a proc object through the `retry_block` option that will be called before every
153
- retry, before There are many different applications for this feature, spacing from instrumentation to monitoring.
152
+ You can specify a proc object through the `retry_block` option that will be called before every retry.
153
+ There are many different applications for this feature, ranging from instrumentation to monitoring.
154
154
 
155
155
  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)
156
156
 
@@ -163,6 +163,27 @@ retry_options = {
163
163
  }
164
164
  ```
165
165
 
166
+ ### Call a block after retries have been exhausted
167
+
168
+ You can specify a lambda object through the `exhausted_retries_block` option that will be called after all retries are exhausted.
169
+ This block will be called once.
170
+
171
+ The block is passed keyword arguments with contextual information and passed your data:
172
+ * Request environment,
173
+ * exception,
174
+ * middleware options
175
+ * and your data.
176
+
177
+ In a lambda you can pass any logic for further work.
178
+
179
+ For example, you might want to update user by request query.
180
+
181
+ ```ruby
182
+ retry_options = {
183
+ exhausted_retries_block: -> (user_id:, env:, exception:, options:) { User.find_by!(id: user_id).do_admin! }
184
+ }
185
+ ```
186
+
166
187
  ## Development
167
188
 
168
189
  After checking out the repo, run `bin/setup` to install dependencies.
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'retryable'
4
+
3
5
  module Faraday
4
6
  module Retry
5
7
  # This class provides the main implementation for your middleware.
@@ -15,6 +17,8 @@ module Faraday
15
17
  # (see "retry" middleware: https://github.com/lostisland/faraday/blob/main/lib/faraday/request/retry.rb#L142).
16
18
  # IMPORTANT: Remember to call `@app.call(env)` or `super` to not interrupt the middleware chain!
17
19
  class Middleware < Faraday::Middleware
20
+ include Retryable
21
+
18
22
  DEFAULT_EXCEPTIONS = [
19
23
  Errno::ETIMEDOUT, 'Timeout::Error',
20
24
  Faraday::TimeoutError, Faraday::RetriableResponse
@@ -27,7 +31,8 @@ module Faraday
27
31
  :backoff_factor, :exceptions,
28
32
  :methods, :retry_if, :retry_block,
29
33
  :retry_statuses, :rate_limit_retry_header,
30
- :rate_limit_reset_header, :header_parser_block)
34
+ :rate_limit_reset_header, :header_parser_block,
35
+ :exhausted_retries_block)
31
36
 
32
37
  DEFAULT_CHECK = ->(_env, _exception) { false }
33
38
 
@@ -78,6 +83,10 @@ module Faraday
78
83
  def retry_statuses
79
84
  Array(self[:retry_statuses] ||= [])
80
85
  end
86
+
87
+ def exhausted_retries_block
88
+ self[:exhausted_retries_block] ||= proc {}
89
+ end
81
90
  end
82
91
 
83
92
  # @param app [#call]
@@ -124,6 +133,13 @@ module Faraday
124
133
  # the the value of the retry header and should return the number of
125
134
  # seconds to wait before retrying the request. This is useful if the
126
135
  # value of the header is not a number of seconds or a RFC 2822 formatted date.
136
+ # @option options [Block] :exhausted_retries_block block will receive
137
+ # when all attempts are exhausted. The block will be yielded keyword arguments:
138
+ # * env [Faraday::Env]: Request environment
139
+ # * exception [Exception]: exception that triggered the retry,
140
+ # will be the synthetic `Faraday::RetriableResponse` if the
141
+ # retry was triggered by something other than an exception.
142
+ # * options [Faraday::Options]: middleware options
127
143
  def initialize(app, options = nil)
128
144
  super(app)
129
145
  @options = Options.from(options)
@@ -147,32 +163,14 @@ module Faraday
147
163
  def call(env)
148
164
  retries = @options.max
149
165
  request_body = env[:body]
150
- begin
166
+
167
+ with_retries(env: env, options: @options, retries: retries, body: request_body, errmatch: @errmatch) do
151
168
  # after failure env[:body] is set to the response body
152
169
  env[:body] = request_body
170
+
153
171
  @app.call(env).tap do |resp|
154
172
  raise Faraday::RetriableResponse.new(nil, resp) if @options.retry_statuses.include?(resp.status)
155
173
  end
156
- rescue @errmatch => e
157
- if retries.positive? && retry_request?(env, e)
158
- retries -= 1
159
- rewind_files(request_body)
160
- if (sleep_amount = calculate_sleep_amount(retries + 1, env))
161
- @options.retry_block.call(
162
- env: env,
163
- options: @options,
164
- retry_count: @options.max - (retries + 1),
165
- exception: e,
166
- will_retry_in: sleep_amount
167
- )
168
- sleep sleep_amount
169
- retry
170
- end
171
- end
172
-
173
- raise unless e.is_a?(Faraday::RetriableResponse)
174
-
175
- e.response
176
174
  end
177
175
  end
178
176
 
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ # Adds the ability to retry a request based on settings and errors that have occurred.
5
+ module Retryable
6
+ def with_retries(env:, options:, retries:, body:, errmatch:)
7
+ yield
8
+ rescue errmatch => e
9
+ exhausted_retries(options, env, e) if retries_zero?(retries, env, e)
10
+
11
+ if retries.positive? && retry_request?(env, e)
12
+ retries -= 1
13
+ rewind_files(body)
14
+ if (sleep_amount = calculate_sleep_amount(retries + 1, env))
15
+ options.retry_block.call(
16
+ env: env,
17
+ options: options,
18
+ retry_count: options.max - (retries + 1),
19
+ exception: e,
20
+ will_retry_in: sleep_amount
21
+ )
22
+ sleep sleep_amount
23
+ retry
24
+ end
25
+ end
26
+
27
+ raise unless e.is_a?(Faraday::RetriableResponse)
28
+
29
+ e.response
30
+ end
31
+
32
+ private
33
+
34
+ def retries_zero?(retries, env, exception)
35
+ retries.zero? && retry_request?(env, exception)
36
+ end
37
+
38
+ def exhausted_retries(options, env, exception)
39
+ options.exhausted_retries_block.call(
40
+ env: env,
41
+ exception: exception,
42
+ options: options
43
+ )
44
+ end
45
+ end
46
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  module Retry
5
- VERSION = '2.2.1'
5
+ VERSION = '2.3.2'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-retry
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Giuffrida
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-04-15 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: faraday
@@ -151,17 +150,17 @@ files:
151
150
  - lib/faraday/retriable_response.rb
152
151
  - lib/faraday/retry.rb
153
152
  - lib/faraday/retry/middleware.rb
153
+ - lib/faraday/retry/retryable.rb
154
154
  - lib/faraday/retry/version.rb
155
155
  homepage: https://github.com/lostisland/faraday-retry
156
156
  licenses:
157
157
  - MIT
158
158
  metadata:
159
159
  bug_tracker_uri: https://github.com/lostisland/faraday-retry/issues
160
- changelog_uri: https://github.com/lostisland/faraday-retry/blob/v2.2.1/CHANGELOG.md
161
- documentation_uri: http://www.rubydoc.info/gems/faraday-retry/2.2.1
160
+ changelog_uri: https://github.com/lostisland/faraday-retry/blob/v2.3.2/CHANGELOG.md
161
+ documentation_uri: http://www.rubydoc.info/gems/faraday-retry/2.3.2
162
162
  homepage_uri: https://github.com/lostisland/faraday-retry
163
163
  source_code_uri: https://github.com/lostisland/faraday-retry
164
- post_install_message:
165
164
  rdoc_options: []
166
165
  require_paths:
167
166
  - lib
@@ -179,8 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
178
  - !ruby/object:Gem::Version
180
179
  version: '0'
181
180
  requirements: []
182
- rubygems_version: 3.1.6
183
- signing_key:
181
+ rubygems_version: 3.6.7
184
182
  specification_version: 4
185
183
  summary: Catches exceptions and retries each request a limited number of times
186
184
  test_files: []