Forum: Ruby How to change the Timeout::Error threshold for open_uri

Posted by Keith Carter (kcarter80)
on 2008-03-20 21:18
When opening a particularly slow loading URL I am getting the following
error:

Loading production environment (Rails 2.0.2)
>> require 'open-uri'
=> ["OpenURI"]
>>
?> open('http://www.wwtdd.com/?start=2331') { |f|
?>   puts "    Opened successfully with charset: " + f.charset
>> }
/usr/lib64/ruby/1.8/timeout.rb:54:in `rbuf_fill': execution expired
(Timeout::Error)
        from /usr/lib64/ruby/1.8/timeout.rb:56:in `timeout'
        from /usr/lib64/ruby/1.8/timeout.rb:76:in `timeout'
        from /usr/lib64/ruby/1.8/net/protocol.rb:132:in `rbuf_fill'
        from /usr/lib64/ruby/1.8/net/protocol.rb:116:in `readuntil'
        from /usr/lib64/ruby/1.8/net/protocol.rb:126:in `readline'
        from /usr/lib64/ruby/1.8/net/http.rb:2029:in `read_status_line'
        from /usr/lib64/ruby/1.8/net/http.rb:2018:in `read_new'
        from /usr/lib64/ruby/1.8/net/http.rb:1059:in `request'
         ... 9 levels...
        from /usr/lib64/ruby/1.8/open-uri.rb:30:in `open'
        from (irb):33:in `irb_binding'
        from /usr/lib64/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
        from /usr/lib64/ruby/1.8/irb/workspace.rb:52

Does anyone know how to fix this? I want it to wait longer for the web
server to respond.
Posted by Keith Carter (kcarter80)
on 2008-03-21 02:28
Shameless bump. Shoud I be posting on another forum?
Posted by Mikel Lindsaar (Guest)
on 2008-03-21 05:54
(Received via mailing list)
> Does anyone know how to fix this? I want it to wait longer for
> the web server to respond.

Look in the docs/code and you shall find...

OpenURI wraps net/http and net/https

Looking in net/http finds these accessor methods:

    # Seconds to wait until connection is opened.
    # If the HTTP object cannot open a connection in this many seconds,
    # it raises a TimeoutError exception.
    attr_accessor :open_timeout

    # Seconds to wait until reading one block (by one read(2) call).
    # If the HTTP object cannot open a connection in this many seconds,
    # it raises a TimeoutError exception.
    attr_reader :read_timeout


Maybe that would help.

Mikel
Posted by John Powers (jpowers)
on 2009-05-04 02:01
Keith Carter wrote:
> When opening a particularly slow loading URL I am getting the following
> error:
> 
> Loading production environment (Rails 2.0.2)
>>> require 'open-uri'
> => ["OpenURI"]
>>>
> ?> open('http://www.wwtdd.com/?start=2331') { |f|
> ?>   puts "    Opened successfully with charset: " + f.charset
>>> }
> /usr/lib64/ruby/1.8/timeout.rb:54:in `rbuf_fill': execution expired
> (Timeout::Error)
>         from /usr/lib64/ruby/1.8/timeout.rb:56:in `timeout'
>         from /usr/lib64/ruby/1.8/timeout.rb:76:in `timeout'
>         from /usr/lib64/ruby/1.8/net/protocol.rb:132:in `rbuf_fill'
>         from /usr/lib64/ruby/1.8/net/protocol.rb:116:in `readuntil'
>         from /usr/lib64/ruby/1.8/net/protocol.rb:126:in `readline'
>         from /usr/lib64/ruby/1.8/net/http.rb:2029:in `read_status_line'
>         from /usr/lib64/ruby/1.8/net/http.rb:2018:in `read_new'
>         from /usr/lib64/ruby/1.8/net/http.rb:1059:in `request'
>          ... 9 levels...
>         from /usr/lib64/ruby/1.8/open-uri.rb:30:in `open'
>         from (irb):33:in `irb_binding'
>         from /usr/lib64/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
>         from /usr/lib64/ruby/1.8/irb/workspace.rb:52
> 
> Does anyone know how to fix this? I want it to wait longer for the web
> server to respond.

require 'net/http'

# Lengthen timeout in Net::HTTP
module Net
    class HTTP
        alias old_initialize initialize

        def initialize(*args)
            old_initialize(*args)
            @read_timeout = 3*60     # 3 minutes
        end
    end
end
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.