2

Noticed that Ruby RestClient does not follow the given timeout parameter precisely, however it works by doubling the current timeout setting.

Has anyone noticed the same? attached a test code example below.

As httpbin.org is having max delay 10, we need to use small timeouts:

  def test_timeout(delay)
    start = Time.now.to_i
    RestClient::Request.execute(url:     "http://httpbin.org/delay/10",
                                method:  'GET',
                                timeout: delay)
    return 0
  rescue RestClient::Exceptions::ReadTimeout => exception
    endtime = Time.now.to_i
    return endtime - start
  end

pry(main)> test_timeout 1
RestClient.get "http://httpbin.org/delay/10", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate"

=> 3

pry(main)> test_timeout 5
RestClient.get "http://httpbin.org/delay/10", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate"

=> 10

Using Ruby RestClient 2.0.2 and also tested with 2.1.0.rc1

Also tested with read_timeout and open_timeout parameters, which had the same

  def test_timeout(delay)
    start = Time.now.to_i
    RestClient::Request.execute(url: "http://httpbin.org/delay/10",
                                method: 'GET',
                                open_timeout: 1,
                                read_timeout: delay)
    return 0
  rescue RestClient::Exceptions::ReadTimeout => exception
    endtime = Time.now.to_i
    return endtime - start
  end

pry(main)> test_timeout 2
RestClient.get "http://httpbin.org/delay/10", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate"

=> 4
pry(main)> test_timeout 3
RestClient.get "http://httpbin.org/delay/10", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate"

=> 6
jaluke
  • 41
  • 8

2 Answers2

1

Timeout = read_timeout + open_timeout

open_timeout: is the time after which if the connection fails to establish, the request will be rejected

read_timeout: the time you are willing to wait for some data to be received from the server

  RestClient::Request.execute(url: "http://httpbin.org/delay/#{delay * 10}",
                            method: 'GET',
                            timeout: 1)
arjunsv3691
  • 791
  • 6
  • 19
  • The initial examples shows that RestClient doubles the time we shall wait, even if the open_timeout is 1 and read_timeout is 3 then the actual timeout occurs at ~6seconds. – jaluke Oct 15 '18 at 05:58
1

As a summary,

its a wanted feature on Net::HTTP library. Ruby 2.5 should have a parameter to over ride the feature.

More information from https://engineering.wework.com/ruby-users-be-wary-of-net-http-f284747288b2

Thank you all regarding you replies!

jaluke
  • 41
  • 8