Skip to content

Commit

Permalink
Add support for setting Ruby Net::HTTP read_timeout option separately
Browse files Browse the repository at this point in the history
Right now `timeout` setting sets all the timeout values (`read`, `open` & `write` if available). To unify the API with `Net::HTTP` one (which has a dedicated method for `read_timeout` as well) I propose extending the `RequestOptions` by `read_timeout`.
  • Loading branch information
springerigor committed Jul 18, 2019
1 parent ac27ce5 commit 4fde5d7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/faraday/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ def configure_request(http, req)
end
end
http.open_timeout = req[:open_timeout] if req[:open_timeout]
http.read_timeout = req[:read_timeout] if req[:read_timeout]

if req[:write_timeout] && http.respond_to?(:write_timeout=)
http.write_timeout = req[:write_timeout]
end
Expand Down
5 changes: 3 additions & 2 deletions lib/faraday/options/request_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
module Faraday
# RequestOptions contains the configurable properties for a Faraday request.
class RequestOptions < Options.new(:params_encoder, :proxy, :bind,
:timeout, :open_timeout, :write_timeout,
:boundary, :oauth, :context, :on_data)
:timeout, :open_timeout, :read_timeout,
:write_timeout, :boundary, :oauth, :context,
:on_data)

def []=(key, value)
if key && key.to_sym == :proxy
Expand Down
10 changes: 10 additions & 0 deletions spec/faraday/adapter/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@

expect(http.write_timeout).to eq(10) if http.respond_to?(:write_timeout=)
end
it 'supports open_timeout' do
adapter.send(:configure_request, http, open_timeout: 10)

expect(http.open_timeout).to eq(10)
end
it 'supports read_timeout' do
adapter.send(:configure_request, http, read_timeout: 10)

expect(http.read_timeout).to eq(10)
end

context 'with https url' do
let(:url) { URI('https://example.com') }
Expand Down

0 comments on commit 4fde5d7

Please sign in to comment.