ruby rest-client:永远不会超时?

ear*_*old 18 ruby-on-rails rest-client

我正在尝试使用ruby rest-client将大量图像上传到我正在编写的网站.我的代码看起来像:

RestClient.post url, :timeout => 90000000, :open_timeout => 90000000, :file_param => file_obj
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误:

RestClient::RequestTimeout: Request Timeout
    from /Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/request.rb:174:in `transmit'
    from /Library/Ruby/
Run Code Online (Sandbox Code Playgroud)

但是当我查看服务器日志时

Completed in 61493ms (View: 2, DB: 1) | 201 Created 
Run Code Online (Sandbox Code Playgroud)

所以似乎没有任何理由为什么这是超时.任何人都知道如果有超时参数我没有正确设置?

谢谢

phl*_*per 21

此语法将超时设置为请求标头(请参阅RestClient.post签名),如果要使用必须使用的timeout参数:

RestClient::Request.execute(:method => :post, :url => @url, :timeout => 90000000)
Run Code Online (Sandbox Code Playgroud)

请参阅:https://github.com/rest-client/rest-client/blob/master/lib/restclient/request.rb#L12

  • RestClient :: Request.execute(:method =>:post,:url => @url,:timeout => 90000000) (2认同)

cle*_*nsp 13

查看文档,您可以通过RestClient.execute超时参数传递-1:

# * :timeout and :open_timeout passing in -1 will disable the timeout by setting the corresponding net timeout values to nil
Run Code Online (Sandbox Code Playgroud)

它可以使用如下:

resource = RestClient::Resource.new(
  "url",
  :timeout => -1,
  :open_timeout => -1
response = resource.get :params => {<params>}
Run Code Online (Sandbox Code Playgroud)

  • 这似乎已更新为"nil"而不是-1.使用-1记录警告(但似乎有效). (4认同)

小智 3

我也有类似的问题。快速深入了解源代码就会发现这种不友好的情况:

def self.post(url, payload, headers={}, &block)
  Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers, &block)
end
Run Code Online (Sandbox Code Playgroud)

除非我遗漏了某些内容,否则超时选项不会传递给底层请求。是时候打个补丁了...

  • 稍微深入一点的研究表明,虽然 `get`、`post` 和相关的便捷方法确实不允许您传递 `:timeout` 和 `:open_timout` 选项,但它们只是 `Request.execute` 的薄包装,它将接受他们。恕我直言,最好用“execute”调用来替换对包装器的调用,而不是猴子补丁。 (5认同)