Skip to content
This repository was archived by the owner on Aug 27, 2021. It is now read-only.

Commit ec35d50

Browse files
rsamoilovbrightredchilli
authored andcommitted
Allow to set retry options on a connection
1 parent 6f68705 commit ec35d50

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [v0.11.0] - 2020-12-08
9+
### Added
10+
- Allow to set retry options on a connection([#36])
11+
12+
813
## [v0.9.1] - 2020-11-20
914
### Added
1015
- Automatically retry on network errors([#31])

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ Disable the middleware:
112112
We::Call.configure do |config|
113113
config.retry = false
114114
end
115+
116+
# Provided at initialization
117+
connection = We::Call::Connection.new(retry_options: false)
115118
```
116119

117120
Adjust the middleware:
@@ -120,6 +123,9 @@ Adjust the middleware:
120123
We::Call.configure do |config|
121124
config.retry_options = { interval: 0.5 }
122125
end
126+
127+
# Provided at initialization
128+
connection = We::Call::Connection.new(retry_options: { interval: 0.5 })
123129
```
124130

125131
The gem smartly merges the options passed, so you can specify your own list of exceptions without being afraid to override the default ones:

lib/we/call/connection.rb

+11-4
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ def get_adapter
4646
# @param [String] app
4747
# @param [String] env
4848
# @yieldparam [Faraday::Connection] Faraday connection object is yielded to a block
49-
def new(host:, timeout: nil, open_timeout: OPEN_TIMEOUT, app: guess_app, env: guess_env, &block)
49+
def new(host:, timeout: nil, open_timeout: OPEN_TIMEOUT, app: guess_app, env: guess_env, retry_options: {}, &block)
5050
@host = host
51+
@retry_options = retry_options
5152
@app = app or raise_missing_app!
5253
@env = env or raise_missing_env!
5354
@timeout = timeout or raise_missing_timeout!
@@ -57,7 +58,7 @@ def new(host:, timeout: nil, open_timeout: OPEN_TIMEOUT, app: guess_app, env: gu
5758

5859
private
5960

60-
attr_reader :app, :env, :host, :timeout, :open_timeout
61+
attr_reader :app, :env, :host, :timeout, :open_timeout, :retry_options
6162

6263
# @return [Faraday::Connection] Preconfigured Faraday Connection object, for hitting get, post, etc.
6364
def create
@@ -78,7 +79,7 @@ def create
7879
if config.detect_deprecations
7980
faraday.response :sunset, setup_sunset_middleware(faraday)
8081
end
81-
if config.retry
82+
if should_retry?
8283
faraday.request :retry, fetch_retry_options
8384
end
8485

@@ -127,8 +128,14 @@ def setup_sunset_middleware(faraday)
127128
options
128129
end
129130

131+
def should_retry?
132+
retry_options.any? || config.retry
133+
end
134+
130135
def fetch_retry_options
131-
DEFAULT_RETRY_OPTIONS.merge(config.retry_options) do |key, default_val, new_val|
136+
client_options = retry_options.any? ? retry_options : config.retry_options
137+
138+
DEFAULT_RETRY_OPTIONS.merge(client_options) do |key, default_val, new_val|
132139
if key == :exceptions
133140
default_val + Array(new_val)
134141
else

lib/we/call/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module We
22
module Call
3-
VERSION = "0.10.0"
3+
VERSION = "0.11.0"
44
end
55
end

spec/unit/we/call/connection_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,19 @@
273273
We::Call::Connection::DEFAULT_RETRY_OPTIONS.merge(options)
274274
)
275275
end
276+
277+
context 'when retry options are set on a connection' do
278+
let(:options) { { max: 3, backoff_factor: 2 } }
279+
let(:valid_arguments) { super().merge(retry_options: options) }
280+
281+
it 'registers the middleware with the correct options' do
282+
subject
283+
expect(builder_spy).to have_received(:request).with(
284+
:retry,
285+
We::Call::Connection::DEFAULT_RETRY_OPTIONS.merge(options)
286+
)
287+
end
288+
end
276289
end
277290

278291
context 'when exceptions are overriden' do

0 commit comments

Comments
 (0)