rest-easy 1.4.2 → 1.5.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 +16 -1
- data/README.md +9 -0
- data/lib/rest_easy/settings.rb +1 -0
- data/lib/rest_easy/version.rb +1 -1
- data/lib/rest_easy.rb +45 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c047221a492782c10e7531864861b44537f867a5c87411311ce1d7ff1233e515
|
|
4
|
+
data.tar.gz: 0bcb2f88739589f5d906bf629550a8d2293a516ea0d799e52bd360e716aa44e6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e09ac325027fbcc7d13e0d1520fc26db758f6cd77701ce69a249bfed78db2a910b1e61aa3abb1e7284f21d2a0080e60766fa84222940aafadcf304fb9ce27c62
|
|
7
|
+
data.tar.gz: 430830b51e4c3e5a658ebd73a374d3bb2db419cd996a455221ad6e1bcfd2e3d8af07141f9fe76a8b7cfe5e66dc28abf6084459e78d3ce61ea0e2c2a75cacc2e8
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [1.5.0] - 2026-11-17
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- **`retry_transient_errors` setting** (default `false`). When on, 429
|
|
10
|
+
responses, and 5xx responses, timeouts and dropped connections on GET, PUT
|
|
11
|
+
and DELETE, are retried up to `max_retries` without calling the
|
|
12
|
+
authentication's `on_rejected`, which then only sees the other failures.
|
|
13
|
+
These retries wait for the response's `Retry-After` header (seconds or an
|
|
14
|
+
HTTP date, 1 second without one or without a response); one asking for
|
|
15
|
+
more than 10 seconds raises instead. A retry that `on_rejected` asks for
|
|
16
|
+
is still made at once. Off by default, since PUT and DELETE are only safe
|
|
17
|
+
to repeat on APIs that implement them idempotently.
|
|
18
|
+
|
|
5
19
|
## [1.4.2] - 2026-09-23
|
|
6
20
|
|
|
7
21
|
### Fixed
|
|
@@ -195,7 +209,8 @@
|
|
|
195
209
|
|
|
196
210
|
Initial release.
|
|
197
211
|
|
|
198
|
-
[Unreleased]: https://github.com/accodeing/rest-easy/compare/v1.
|
|
212
|
+
[Unreleased]: https://github.com/accodeing/rest-easy/compare/v1.5.0...HEAD
|
|
213
|
+
[1.5.0]: https://github.com/accodeing/rest-easy/compare/v1.4.2...v1.5.0
|
|
199
214
|
[1.4.2]: https://github.com/accodeing/rest-easy/compare/v1.4.1...v1.4.2
|
|
200
215
|
[1.4.1]: https://github.com/accodeing/rest-easy/compare/v1.4.0...v1.4.1
|
|
201
216
|
[1.4.0]: https://github.com/accodeing/rest-easy/compare/v1.3.1...v1.4.0
|
data/README.md
CHANGED
|
@@ -101,6 +101,7 @@ end
|
|
|
101
101
|
| `log_bodies` | `false` | When `true`, request/response bodies are logged. Off by default to avoid leaking domain secrets |
|
|
102
102
|
| `logger` | `nil` | When set, attaches Faraday's logger middleware and writes HTTP request/response details to it |
|
|
103
103
|
| `max_retries` | `3` | Retry count on request failure |
|
|
104
|
+
| `retry_transient_errors` | `false` | When `true`, 429 responses, and 5xx responses, timeouts and dropped connections on GET, PUT and DELETE, are retried without calling `on_rejected`; see [the retry lifecycle](#custom-authentication) |
|
|
104
105
|
|
|
105
106
|
### Logging HTTP traffic
|
|
106
107
|
|
|
@@ -655,6 +656,14 @@ The retry lifecycle:
|
|
|
655
656
|
- Return normally → retry (up to `max_retries`)
|
|
656
657
|
- Raise → propagate error
|
|
657
658
|
|
|
659
|
+
With `retry_transient_errors` on, a 429 skips step 3 and is retried directly, up to `max_retries`. So is a 5xx on GET, PUT or DELETE. A 5xx on POST still goes through step 3, because the server may already have acted on it, and repeating a POST that creates something can create it twice.
|
|
660
|
+
|
|
661
|
+
A timeout or a dropped connection (`Faraday::TimeoutError`, `Faraday::ConnectionFailed`) has no response to pass to `on_rejected`, so it is normally raised straight away. With the setting on, it is retried on GET, PUT or DELETE in the same way, for the same reason. On POST it is still raised.
|
|
662
|
+
|
|
663
|
+
Before each of these retries the request waits for as long as the response's `Retry-After` header says, either in seconds or as an HTTP date, and 1 second without one or without a response. If `Retry-After` asks for more than 10 seconds the error is raised straight away, since retrying sooner than the server allows would only fail again. A retry that `on_rejected` asks for, such as after a 5xx on POST, is made straight away, as it is without the setting.
|
|
664
|
+
|
|
665
|
+
It is off by default because a PUT or DELETE is only safe to repeat when the API implements them idempotently. Turn it on for APIs that do.
|
|
666
|
+
|
|
658
667
|
## CRUD operations
|
|
659
668
|
|
|
660
669
|
Resources provide standard CRUD methods:
|
data/lib/rest_easy/settings.rb
CHANGED
|
@@ -12,6 +12,7 @@ module RestEasy
|
|
|
12
12
|
setting :log_bodies, default: false, reader: true
|
|
13
13
|
setting :logger, reader: true
|
|
14
14
|
setting :max_retries, default: 3, reader: true
|
|
15
|
+
setting :retry_transient_errors, default: false, reader: true
|
|
15
16
|
|
|
16
17
|
setting :conversions do
|
|
17
18
|
setting :json_attributes, default: Conventions::DEFAULT, reader: true
|
data/lib/rest_easy/version.rb
CHANGED
data/lib/rest_easy.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "rubygems"
|
|
4
|
+
require "time"
|
|
4
5
|
require "dry/types"
|
|
5
6
|
require "faraday"
|
|
6
7
|
require "zeitwerk"
|
|
@@ -17,6 +18,9 @@ module RestEasy
|
|
|
17
18
|
# Make Boolean available as a bare type constant (Ruby has no built-in Boolean)
|
|
18
19
|
::Object.const_set(:Boolean, Dry::Types["params.bool"]) unless defined?(::Boolean)
|
|
19
20
|
|
|
21
|
+
DEFAULT_RETRY_AFTER_IN_SECONDS = 1
|
|
22
|
+
MAX_RETRY_AFTER_IN_SECONDS = 10
|
|
23
|
+
|
|
20
24
|
# ── Error hierarchy ──────────────────────────────────────────────────
|
|
21
25
|
|
|
22
26
|
class Error < StandardError; end
|
|
@@ -69,6 +73,9 @@ module RestEasy
|
|
|
69
73
|
STANDARD_AUTH_HEADERS = %w[Authorization Proxy-Authorization Cookie Set-Cookie].freeze
|
|
70
74
|
private_constant :STANDARD_AUTH_HEADERS
|
|
71
75
|
|
|
76
|
+
IDEMPOTENT_METHODS = %i[get put delete].freeze
|
|
77
|
+
private_constant :IDEMPOTENT_METHODS
|
|
78
|
+
|
|
72
79
|
def config
|
|
73
80
|
self::Settings.config
|
|
74
81
|
end
|
|
@@ -166,13 +173,50 @@ module RestEasy
|
|
|
166
173
|
rescue RequestError => e
|
|
167
174
|
attempts += 1
|
|
168
175
|
if attempts <= max_retries
|
|
169
|
-
|
|
176
|
+
if config.retry_transient_errors && transient?(method, e.response)
|
|
177
|
+
wait = retry_after(e.response)
|
|
178
|
+
raise if wait > MAX_RETRY_AFTER_IN_SECONDS
|
|
179
|
+
|
|
180
|
+
Kernel.sleep(wait)
|
|
181
|
+
else
|
|
182
|
+
auth.on_rejected(e.response)
|
|
183
|
+
end
|
|
170
184
|
retry
|
|
171
185
|
end
|
|
172
186
|
raise
|
|
187
|
+
|
|
188
|
+
rescue Faraday::TimeoutError, Faraday::ConnectionFailed
|
|
189
|
+
attempts += 1
|
|
190
|
+
raise unless attempts <= max_retries && config.retry_transient_errors && IDEMPOTENT_METHODS.include?(method)
|
|
191
|
+
|
|
192
|
+
Kernel.sleep(DEFAULT_RETRY_AFTER_IN_SECONDS)
|
|
193
|
+
retry
|
|
173
194
|
end
|
|
174
195
|
end
|
|
175
196
|
|
|
197
|
+
# A 429 means the server did not act on the request, so any method can be
|
|
198
|
+
# repeated. A 5xx may come after it did, so only idempotent ones are.
|
|
199
|
+
def transient?(method, response)
|
|
200
|
+
return false unless response
|
|
201
|
+
return true if response.status == 429
|
|
202
|
+
|
|
203
|
+
(500..599).cover?(response.status) && IDEMPOTENT_METHODS.include?(method)
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def retry_after(response)
|
|
207
|
+
value = response.headers["Retry-After"]
|
|
208
|
+
return DEFAULT_RETRY_AFTER_IN_SECONDS unless value
|
|
209
|
+
|
|
210
|
+
seconds = Integer(value, 10, exception: false) || seconds_until(value) || DEFAULT_RETRY_AFTER_IN_SECONDS
|
|
211
|
+
seconds.clamp(0..)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def seconds_until(http_date)
|
|
215
|
+
(Time.httpdate(http_date) - Time.now).ceil
|
|
216
|
+
rescue ArgumentError
|
|
217
|
+
nil
|
|
218
|
+
end
|
|
219
|
+
|
|
176
220
|
end
|
|
177
221
|
|
|
178
222
|
# ── Type bridge ─────────────────────────────────────────────────────
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rest-easy
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jonas Schubert Erlandsson
|
|
@@ -10,7 +10,7 @@ authors:
|
|
|
10
10
|
autorequire:
|
|
11
11
|
bindir: bin
|
|
12
12
|
cert_chain: []
|
|
13
|
-
date: 2026-
|
|
13
|
+
date: 2026-11-17 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
16
16
|
name: dry-types
|