ruby rest-client:使其永不超时?

发布于 2024-10-07 19:08:57 字数 610 浏览 8 评论 0原文

我正在尝试使用 ruby​​ rest-client 将大量图像上传到我正在编写的网站。我的代码如下所示:

RestClient.post url, :timeout => 90000000, :open_timeout => 90000000, :file_param => file_obj

但是,我收到此错误:

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/

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

Completed in 61493ms (View: 2, DB: 1) | 201 Created 

,似乎没有任何原因导致超时。有人知道我是否未正确设置超时参数吗?

谢谢

I am trying to use ruby rest-client to upload a large number of images to a site that I'm writing. My code looks like:

RestClient.post url, :timeout => 90000000, :open_timeout => 90000000, :file_param => file_obj

However, I am getting this error:

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/

But when I look at the server log

Completed in 61493ms (View: 2, DB: 1) | 201 Created 

So there doesn't appear to be any reason why this is timing out. Anyone have any idea if there is a timeout param I am not correctly setting?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

玩套路吗 2024-10-14 19:08:57

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

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

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

This syntax sets the timeout as request header (see RestClient.post signature), if you want to use the timeout parameter you must use:

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

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

孤云独去闲 2024-10-14 19:08:57

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

# * :timeout and :open_timeout passing in -1 will disable the timeout by setting the corresponding net timeout values to nil

它可以按如下方式使用:

resource = RestClient::Resource.new(
  "url",
  :timeout => -1,
  :open_timeout => -1
response = resource.get :params => {<params>}

Looking at the docs, you can pass -1 through RestClient.execute timeout param:

# * :timeout and :open_timeout passing in -1 will disable the timeout by setting the corresponding net timeout values to nil

It can be used as follows:

resource = RestClient::Resource.new(
  "url",
  :timeout => -1,
  :open_timeout => -1
response = resource.get :params => {<params>}
巨坚强 2024-10-14 19:08:57

我使用了以下代码,并且像理查德指出的那样工作起来

resource = RestClient::Resource.new "url", 
                                    :timeout => $TIMEOUT, 
                                    :open_timeout => $OPEN_TIMEOUT

response = resource.get  :params => { ..... }

I have used following code and works like a charm as pointed out by Richard

resource = RestClient::Resource.new "url", 
                                    :timeout => $TIMEOUT, 
                                    :open_timeout => $OPEN_TIMEOUT

response = resource.get  :params => { ..... }
与君绝 2024-10-14 19:08:57

我已经广泛使用 RestClient.get 和 RestClient.post,所以对我来说,“Monkey Patch”RestClient 更容易。如果可能的话,我建议使用 RestClient::Resource.newRestClient::Request.Execute

但是,由于我很懒,并且不想在代码中交换 RestClient.get / RestClient.post 的每次出现,所以我决定走捷径。

$timeout = 30
$open_timeout = 30

module RestClient2
  include RestClient

  def self.get(url, headers={}, &block)
    Request.execute(:method => :get, :url => url, :headers => headers, 
     :timeout => $timeout, :open_timeout => $open_timeout, &block)
  end

  def self.post(url, payload, headers={}, &block)
    Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers,
     :timeout => $timeout, :open_timeout => $open_timeout, &block)
  end
end

我只是快速地将 RestClient.get/post 替换为 RestClient2.get/post 。

如果 RestClient::Request 指定了默认超时,那就太好了,例如:

  @timeout = args[:timeout] || 30
  @open_timeout = args[:open_timeout] || 30

I already use RestClient.get and RestClient.post extensively, so for me, it was easier to 'Monkey Patch' RestClient. I would recommend using RestClient::Resource.new or RestClient::Request.Execute if possible.

However, since I'm lazy, and don't want to go swap out every occurrence of RestClient.get / RestClient.post in my code, I've decided to take a shortcut.

$timeout = 30
$open_timeout = 30

module RestClient2
  include RestClient

  def self.get(url, headers={}, &block)
    Request.execute(:method => :get, :url => url, :headers => headers, 
     :timeout => $timeout, :open_timeout => $open_timeout, &block)
  end

  def self.post(url, payload, headers={}, &block)
    Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers,
     :timeout => $timeout, :open_timeout => $open_timeout, &block)
  end
end

And than I just just quick replaced RestClient.get/post with RestClient2.get/post.

It would be nice, if RestClient::Request had a default timeout specified, like:

  @timeout = args[:timeout] || 30
  @open_timeout = args[:open_timeout] || 30
笑梦风尘 2024-10-14 19:08:57

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

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

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

I'm having similar issues. A quick dive into the source reveals this bit of unfriendliness:

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

Unless I'm missing something, the timeout options aren't passed on to the underlying request. Time for a patch ...

逆光下的微笑 2024-10-14 19:08:57

RestClient::Resource.new() 允许您设置 :timeout 和 :open_timeout 值,当您使用资源的 get、post、put 等方法时,这些值将传递给 Request.execute 方法

The RestClient::Resource.new() allows you to set :timeout and :open_timeout values that will get passed to the Request.execute method, when you use the resource's get, post, put, etc methods

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文