12

I want to override the default timeout for the service call in my ruby code. I open connection as under.

res = Net::HTTP.start(@@task_url.host, @@task_url.port) do |http|
    http.get("/tasks/#{task_id}")
end          

I tried to set the read_timeout time as under but then I got the NoMethodError exception in my code.

res = Net::HTTP.start(@@task_url.host, @@task_url.port)
res.read_timeout = 10
res do |http|
    http.get("/tasks/#{task_id}")
end

Suggest me how should I set the read_timeout. And I am looking to set the read_timeout somewhere globally so that I can use that timeout for all my service call through Net::HTTPP.start()

manyu
  • 261
  • 1
  • 4
  • 14

2 Answers2

20

If you use Ruby 1.8 you have to use Net::HTTP.new:

http = Net::HTTP.new(host, port)
http.read_timeout = 10

response = http.get("/")    
# or intead of above line if you need block
# http.start do
#   response = http.get("/")
# end

If you look at the source code of Net::HTTP.start, you will see that ::start is just calling ::new with #start method:

# File net/http.rb, line 439
def HTTP.start(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil, &block) # :yield: +http+
  new(address, port, p_addr, p_port, p_user, p_pass).start(&block)
end

>= Ruby 1.9

You can set read_timeout in opt argument: start(address, ..., opt, &block)

like this:

res = Net::HTTP.start(host, port, :read_timeout => 10)
A.D.
  • 4,487
  • 3
  • 38
  • 50
  • Can you post somewhere (gist.github.com) piece of code you're trying? – A.D. Mar 01 '13 at 13:58
  • Try `irb`, just `require "net/http"; res = Net::HTTP.start(your_host, your_port, :read_timeout => 10)` if it works. – A.D. Mar 01 '13 at 14:19
  • actually i think signature expects other values to be passed in the method as well. So will not try by passing other parameters as nul – manyu Mar 01 '13 at 14:28
  • Yes try it with other params with nil, but it's strange, because it works for me without problem. – A.D. Mar 01 '13 at 14:32
  • Running in irb gives the same TypeError cannot convert hash to string – manyu Mar 01 '13 at 14:40
  • what are you running exactly ? I am getting wrong number of arguments (7 for 6) – manyu Mar 01 '13 at 14:44
  • https://gist.github.com/anonymous/5065172 I'm using `:open_timeout` for testing with `host="google.com"` and `port=81`. – A.D. Mar 01 '13 at 15:00
  • what is you version of ruby? mine is ruby 1.8.7 . Same error came with your example also – manyu Mar 01 '13 at 15:11
  • Ofc, versions ;) [Documentation](http://www.ruby-doc.org/stdlib-1.8.7/libdoc/net/http/rdoc/Net/HTTP.html#method-c-start) for 1.8.7 is without **opt** arg. – A.D. Mar 01 '13 at 15:20
  • how to do it in 1.8.7 ? any idea ? – manyu Mar 01 '13 at 15:31
  • i also tried doing it like this https://gist.github.com/anonymous/f6bd9db4b26a6359510f but it showed me that no such method. – manyu Mar 01 '13 at 15:34
  • @manyu There's not any reason to use `Net::HTTP.start` over `Net::HTTP.new` because `::start` is only wrapper for `::new`, check the source. – A.D. Mar 01 '13 at 18:51
  • Thanks A.D. I used new instead of start. Thanks a lot :) – manyu Mar 03 '13 at 10:07
1

You could use open from OpenURI. This method has a :read_timeout option. I don't know how to set the option globally, but you can wrap it inside a custom function that sets this option.

require 'open-uri'
module NetCustom

  def open_url(url, &task)
     open(url, :read_timeout => 20) do |file|
       yield file.read
     end
  end

end

Usage:

class Foo
  include NetCustom

  def bar
    open_url('http://example.org/tasks/') do |content|
      # Handle page text content
    end
  end
end
Baldrick
  • 23,882
  • 6
  • 74
  • 79