Maarten
Maarten

Reputation: 7318

Customize IO stream timeout value in Ruby / Rails

In my rails app, I use open-uri to open an external file which may take up to 10 minutes to load

Example:

dl_stream = open('http://wetten.overheid.nl/xml.php?regelingID=bwbr0020368')

Now, after 1 minute, Ruby will throw a timeout error. I gleaned this from the source code, in \net\protocol.rc:

@read_timeout = 60

def rbuf_fill
  begin
    @rbuf << @io.read_nonblock(BUFSIZE)
  rescue IO::WaitReadable
    if IO.select([@io], nil, nil, @read_timeout)
      retry
    else
      raise Timeout::Error
    end
  rescue IO::WaitWritable
    # OpenSSL::Buffering#read_nonblock may fail with IO::WaitWritable.
    # http://www.openssl.org/support/faq.html#PROG10
    if IO.select(nil, [@io], nil, @read_timeout)
      retry
    else
      raise Timeout::Error
    end
  end
end

I'm guessing I can set this timeout value to something more amenable to my situation, like 15 minutes, in my app settings, but how and where?

Upvotes: 2

Views: 737

Answers (1)

Patrick Oscity
Patrick Oscity

Reputation: 54694

You can add the timeout in seconds to the call to open with the :read_timeout option:

# timeout after 10 minutes
open('http://example.com', :read_timeout => 600).read

All the options are documented here.

Upvotes: 3

Related Questions