faraday timeout on a simple get

Zhaohan Weng picture Zhaohan Weng · Aug 28, 2015 · Viewed 10.6k times · Source

Is there a way to add timeout options in this simple get method?

I am using Faraday 3.3.

Faraday.get(url)

After searching around, I can only apply timeout options after I initiate a connection first, then apply timeout options. Or there's a simple way?

This is what I am doing right now:

conn = Faraday.new
response = conn.get do |req|
  req.url url
  req.options.timeout = 2 # 2 seconds
end

Answer

Alexander Karmes picture Alexander Karmes · Aug 28, 2015

Try this one:

conn = Faraday.new do |conn|
  conn.options.timeout = 20
end
response = conn.get(url)

UPD: After I had reviewed gem sources, I found out that there's no way do it like you want.

With get method you can set only url, request params and headers. But to specify timeout, you have to access @options of Faraday::Connection instance. And you can do this by using attr_reader :options

conn = Faraday::Connection.new
conn.options.timeout = 20

Or on initialization of Faraday::Connection instance:

Faraday::Connection.new(nil, request: { timeout: 20 })

Or when it copies connection parameters to request parameter and yields request back:

Faraday::Connection.new.get(url) { |request| request.options.timeout = 20 }