# Open-uri timeout

**URL:** <https://www.ruby-forum.com/t/open-uri-timeout/65238>\
**Category:** Ruby\
**Created:** [August 16, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/open-uri-timeout/65238 "2006-08-16T23:52:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeffrey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d6d6ee/32.png) [@Jeffrey](https://www.ruby-forum.com/u/Jeffrey)\
**Post date:** [August 16, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/open-uri-timeout/65238/1 "2006-08-16T23:52:06Z")

</div>

I love open-uri, but does anyone know how to configure the time-out  
before open() will throw a Timeout::Error? I’d like to make it  
shorter.

I’m trying to download some documents from a server that sometimes  
works fine, but other times seems to ignore HTTP requests entirely. If  
a download doesn’t begin after a few seconds, I want to give up on the  
request and try again. Ideas are welcome.

---

<div class="post-metadata">

**Author:** ![Jeffrey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d6d6ee/32.png) [@Jeffrey](https://www.ruby-forum.com/u/Jeffrey)\
**Post date:** [August 17, 2006, 12:07am UTC](https://www.ruby-forum.com/t/open-uri-timeout/65238/2 "2006-08-17T00:07:08Z")

</div>

On Thu, 17 Aug 2006, Jeffrey wrote:

> I love open-uri, but does anyone know how to configure the time-out  
> before open() will throw a Timeout::Error? I’d like to make it  
> shorter.
> 
> I’m trying to download some documents from a server that sometimes  
> works fine, but other times seems to ignore HTTP requests entirely. If  
> a download doesn’t begin after a few seconds, I want to give up on the  
> request and try again. Ideas are welcome.

the simple way is

require ‘timeout’

retries = 42

begin  
Timeout::timeout(s){  
open(‘[http://a.url.com](http://a.url.com)’) do |f|  
# … stuff with f  
end  
}  
rescue Timeout::Error  
retries -= 1  
if retry \> 0  
sleep 0.42 and retry  
else  
raise  
end  
end

-a

---

<div class="post-metadata">

**Author:** ![Jeffrey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d6d6ee/32.png) [@Jeffrey](https://www.ruby-forum.com/u/Jeffrey)\
**Post date:** [August 17, 2006, 12:31am UTC](https://www.ruby-forum.com/t/open-uri-timeout/65238/3 "2006-08-17T00:31:14Z")

</div>

removed\_email\_address@domain.invalid wrote:

> On Thu, 17 Aug 2006, Jeffrey wrote:
> 
> > I love open-uri, but does anyone know how to configure the time-out  
> > before open() will throw a Timeout::Error? I’d like to make it  
> > shorter.

> ```
> end
> }
> 
> ```
> 
> rescue Timeout::Error  
> retries -= 1  
> if retry \> 0  
> sleep 0.42 and retry  
> else  
> raise  
> end  
> end

Thanks, that’s great. It seems to be working.
