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

Commit

Permalink
Allow to set retry options on a connection
Browse files Browse the repository at this point in the history
  • Loading branch information
rsamoilov authored and brightredchilli committed Dec 8, 2020
1 parent 6f68705 commit ec35d50
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [v0.11.0] - 2020-12-08
### Added
- Allow to set retry options on a connection([#36])


## [v0.9.1] - 2020-11-20
### Added
- Automatically retry on network errors([#31])
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ Disable the middleware:
We::Call.configure do |config|
config.retry = false
end

# Provided at initialization
connection = We::Call::Connection.new(retry_options: false)
```

Adjust the middleware:
Expand All @@ -120,6 +123,9 @@ Adjust the middleware:
We::Call.configure do |config|
config.retry_options = { interval: 0.5 }
end

# Provided at initialization
connection = We::Call::Connection.new(retry_options: { interval: 0.5 })
```

The gem smartly merges the options passed, so you can specify your own list of exceptions without being afraid to override the default ones:
Expand Down
15 changes: 11 additions & 4 deletions lib/we/call/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ def get_adapter
# @param [String] app
# @param [String] env
# @yieldparam [Faraday::Connection] Faraday connection object is yielded to a block
def new(host:, timeout: nil, open_timeout: OPEN_TIMEOUT, app: guess_app, env: guess_env, &block)
def new(host:, timeout: nil, open_timeout: OPEN_TIMEOUT, app: guess_app, env: guess_env, retry_options: {}, &block)
@host = host
@retry_options = retry_options
@app = app or raise_missing_app!
@env = env or raise_missing_env!
@timeout = timeout or raise_missing_timeout!
Expand All @@ -57,7 +58,7 @@ def new(host:, timeout: nil, open_timeout: OPEN_TIMEOUT, app: guess_app, env: gu

private

attr_reader :app, :env, :host, :timeout, :open_timeout
attr_reader :app, :env, :host, :timeout, :open_timeout, :retry_options

# @return [Faraday::Connection] Preconfigured Faraday Connection object, for hitting get, post, etc.
def create
Expand All @@ -78,7 +79,7 @@ def create
if config.detect_deprecations
faraday.response :sunset, setup_sunset_middleware(faraday)
end
if config.retry
if should_retry?
faraday.request :retry, fetch_retry_options
end

Expand Down Expand Up @@ -127,8 +128,14 @@ def setup_sunset_middleware(faraday)
options
end

def should_retry?
retry_options.any? || config.retry
end

def fetch_retry_options
DEFAULT_RETRY_OPTIONS.merge(config.retry_options) do |key, default_val, new_val|
client_options = retry_options.any? ? retry_options : config.retry_options

DEFAULT_RETRY_OPTIONS.merge(client_options) do |key, default_val, new_val|
if key == :exceptions
default_val + Array(new_val)
else
Expand Down
2 changes: 1 addition & 1 deletion lib/we/call/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module We
module Call
VERSION = "0.10.0"
VERSION = "0.11.0"
end
end
13 changes: 13 additions & 0 deletions spec/unit/we/call/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,19 @@
We::Call::Connection::DEFAULT_RETRY_OPTIONS.merge(options)
)
end

context 'when retry options are set on a connection' do
let(:options) { { max: 3, backoff_factor: 2 } }
let(:valid_arguments) { super().merge(retry_options: options) }

it 'registers the middleware with the correct options' do
subject
expect(builder_spy).to have_received(:request).with(
:retry,
We::Call::Connection::DEFAULT_RETRY_OPTIONS.merge(options)
)
end
end
end

context 'when exceptions are overriden' do
Expand Down

0 comments on commit ec35d50

Please sign in to comment.