ruby Restclient超时无法正常工作,它在定义的超时加倍后抛出异常

标签 ruby timeout rest-client

注意到Ruby RestClient不精确遵循给定的超时参数,但它通过将当前超时设置加倍来工作。

有没有人注意到同样的情况?下面附上测试代码示例。

由于 httpbin.org 的最大延迟为 10,我们需要使用较小的超时:

  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

使用 Ruby RestClient 2.0.2 并使用 2.1.0.rc1 进行测试

还使用 read_timeout 和 open_timeout 参数进行了测试,它们具有相同的

  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

最佳答案

超时 = read_timeout + open_timeout

open_timeout:超时后如果连接失败,请求将被拒绝

read_timeout:您愿意等待从服务器接收某些数据的时间

  RestClient::Request.execute(url: "http://httpbin.org/delay/#{delay * 10}",
                            method: 'GET',
                            timeout: 1)

关于ruby Restclient超时无法正常工作,它在定义的超时加倍后抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52782161/

相关文章:

tomcat - Grails/Groovy - 在后台导出/导入时超时

android - 正确的 Android REST 客户端

ruby-on-rails - 为什么 Rails 响应在开发模式下需要一分钟?

html - Ruby on Rails - 创建 #Show 的自定义路由

amazon-web-services - 无法连接到端口 22 上的 AWS 实例

java ThreadPoolExecutor超时不被遵守

java - 使用 Web 服务时出现 UnKnowHost 异常

ruby-on-rails - 如何将 trackable 添加到现有的 Devise 设置中?

Ruby Selenium Webdriver 拖放

ruby-on-rails - 如何在所有模型上添加 has_many 关联