Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faraday timeout

Tags:

ruby

I've searched the docs and other places online and cannot seem to come up with the proper way to set the timeout option for Faraday. Anyone have the answer?

I've tried:

  conn = FaradayStack.build(url)
  conn.headers[:user_agent]   = AppConfig.user_agent
  # conn.options[:timeout]      = 20
  # conn.options[:open_timeout] = 20
  response = conn.get do |req|
    req.options = {
      :timeout      => 20,
      :open_timeout => 20
    }
  end
  response.body

Nothing seems to be working. BTW, I'm using the Typhoeus adapter if that makes a difference.

like image 801
Michael Irwin Avatar asked Nov 11 '11 18:11

Michael Irwin


2 Answers

Well it seems I figured it out. If I pass the timeout option into the initializer, it seems to work:

  options = {
    :timeout      => 20,
    :open_timeout => 20
  }
  conn = FaradayStack.build(url, options)
  conn.headers[:user_agent] = AppConfig.user_agent
  conn.get.body
like image 169
Michael Irwin Avatar answered Sep 20 '22 19:09

Michael Irwin


The Faraday README currently contains this example using a block style:

conn.get do |req|
  req.url '/search'
  req.options[:timeout] = 5           # open/read timeout in seconds
  req.options[:open_timeout] = 2      # connection open timeout in seconds
end

If this doesn't work for you, perhaps you should file a ticket over on Github.

like image 41
David J. Avatar answered Sep 22 '22 19:09

David J.