Skip to content

Commit

Permalink
RequestOptions#fetch_timeout => Adapter#request_timeout
Browse files Browse the repository at this point in the history
This allows adapter internals and specs to treat request options as a 
plain hash.
  • Loading branch information
technoweenie committed Sep 19, 2019
1 parent 8dae875 commit b35f6a2
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 69 deletions.
22 changes: 22 additions & 0 deletions lib/faraday/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,27 @@ def save_response(env, status, body, headers = nil, reason_phrase = nil)
env.response.finish(env) unless env.parallel?
env.response
end

# Fetches either a read, write, or open timeout setting. Defaults to the
# :timeout value if a more specific one is not given.
#
# @param type [Symbol] Describes which timeout setting to get: :read,
# :write, or :open.
# @param options [Hash] Hash containing Symbol keys like :timeout,
# :read_timeout, :write_timeout, :open_timeout, or
# :timeout
#
# @return [Integer, nil] Timeout duration in seconds, or nil if no timeout
# has been set.
def request_timeout(type, options)
unless TIMEOUT_TYPES.include?(type)
msg = "Expected :read, :write, :open. Got #{type.inspect} :("
raise ArgumentError, msg
end

options["#{type}_timeout".to_sym] || options[:timeout]
end

TIMEOUT_TYPES = Set.new(%i[read write open])
end
end
4 changes: 2 additions & 2 deletions lib/faraday/adapter/em_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def configure_ssl(options, env)
# Reads out timeout settings from env into options
def configure_timeout(options, env)
req = request_options(env)
options[:inactivity_timeout] = req.fetch_timeout(:read)
options[:connect_timeout] = req.fetch_timeout(:open)
options[:inactivity_timeout] = request_timeout(:read, req)
options[:connect_timeout] = request_timeout(:open, req)
end

# Reads out compression header settings from env into options
Expand Down
6 changes: 3 additions & 3 deletions lib/faraday/adapter/excon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ def amend_opts_with_ssl!(opts, ssl)
end

def amend_opts_with_timeouts!(opts, req)
if (sec = req.fetch_timeout(:read))
if (sec = request_timeout(:read, req))
opts[:read_timeout] = sec
end

if (sec = req.fetch_timeout(:write))
if (sec = request_timeout(:write, req))
opts[:write_timeout] = sec
end

return unless (sec = req.fetch_timeout(:open))
return unless (sec = request_timeout(:open, req))

opts[:connect_timeout] = sec
end
Expand Down
6 changes: 3 additions & 3 deletions lib/faraday/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ def configure_ssl(http, ssl)
end

def configure_request(http, req)
if (sec = req.fetch_timeout(:read))
if (sec = request_timeout(:read, req))
http.read_timeout = sec
end
if (sec = http.respond_to?(:write_timeout=) &&
req.fetch_timeout(:write))
request_timeout(:write, req))
http.write_timeout = sec
end
if (sec = req.fetch_timeout(:open))
if (sec = request_timeout(:open, req))
http.open_timeout = sec
end

Expand Down
17 changes: 0 additions & 17 deletions lib/faraday/options/request_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@ def []=(key, value)
end
end

# Fetches either a read, write, or open timeout setting. Defaults to the
# :timeout value if a more specific one is not given.
#
# @param type [Symbol] Describes which timeout setting to get: :read,
# :write, or :open.
#
# @return [Integer, nil] Timeout duration in seconds, or nil if no timeout
# has been set.
def fetch_timeout(type)
unless TIMEOUT_TYPES.include?(type)
msg = "Expected :read, :write, :open. Got #{type.inspect} :("
raise ArgumentError, msg
end

self["#{type}_timeout".to_sym] || self[:timeout]
end

def stream_response?
on_data.is_a?(Proc)
end
Expand Down
4 changes: 1 addition & 3 deletions spec/faraday/adapter/em_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

it_behaves_like 'an adapter'

let(:request) { Faraday::RequestOptions.new }

it 'allows to provide adapter specific configs' do
url = URI('https://example.com:1234')
adapter = described_class.new nil, inactivity_timeout: 20
req = adapter.create_request(url: url, request: request)
req = adapter.create_request(url: url, request: {})

expect(req.connopts.inactivity_timeout).to eq(20)
end
Expand Down
51 changes: 51 additions & 0 deletions spec/faraday/adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

RSpec.describe Faraday::Adapter do
let(:adapter) { Faraday::Adapter.new }
let(:request) { {} }

context '#request_timeout' do
it 'gets :read timeout' do
expect(timeout(:read)).to eq(nil)

request[:timeout] = 5
request[:write_timeout] = 1

expect(timeout(:read)).to eq(5)

request[:read_timeout] = 2

expect(timeout(:read)).to eq(2)
end

it 'gets :open timeout' do
expect(timeout(:open)).to eq(nil)

request[:timeout] = 5
request[:write_timeout] = 1

expect(timeout(:open)).to eq(5)

request[:open_timeout] = 2

expect(timeout(:open)).to eq(2)
end

it 'gets :write timeout' do
expect(timeout(:write)).to eq(nil)

request[:timeout] = 5
request[:read_timeout] = 1

expect(timeout(:write)).to eq(5)

request[:write_timeout] = 2

expect(timeout(:write)).to eq(2)
end

def timeout(type)
adapter.send(:request_timeout, type, request)
end
end
end
41 changes: 0 additions & 41 deletions spec/faraday/options/request_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,6 @@
RSpec.describe Faraday::RequestOptions do
subject(:options) { Faraday::RequestOptions.new }

context '#fetch_timeout' do
it 'gets :read timeout' do
expect(options.fetch_timeout(:read)).to eq(nil)

options[:timeout] = 5
options[:write_timeout] = 1

expect(options.fetch_timeout(:read)).to eq(5)

options[:read_timeout] = 2

expect(options.fetch_timeout(:read)).to eq(2)
end

it 'gets :open timeout' do
expect(options.fetch_timeout(:open)).to eq(nil)

options[:timeout] = 5
options[:write_timeout] = 1

expect(options.fetch_timeout(:open)).to eq(5)

options[:open_timeout] = 2

expect(options.fetch_timeout(:open)).to eq(2)
end

it 'gets :write timeout' do
expect(options.fetch_timeout(:write)).to eq(nil)

options[:timeout] = 5
options[:read_timeout] = 1

expect(options.fetch_timeout(:write)).to eq(5)

options[:write_timeout] = 2

expect(options.fetch_timeout(:write)).to eq(2)
end
end

it 'allows to set the request proxy' do
expect(options.proxy).to be_nil

Expand Down

0 comments on commit b35f6a2

Please sign in to comment.