Search code examples
rubytimeoutrest-client

ruby restclient timeout does not work properly, it throws exception after double the defined timeout


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

Solution

  • 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!