Ruby 错误 Net::ReadTimeout
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19798091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby error Net::ReadTimeout
提问by Luigi
I am making a series of API calls using httparty. The first two API calls succeed, but the third one fails. It pauses for about 60 seconds (default timeout period) and then returns this error:
我正在使用 httparty 进行一系列 API 调用。前两个 API 调用成功,但第三个调用失败。它暂停约 60 秒(默认超时时间),然后返回此错误:
/Users/luigi/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/protocol.rb:158:in `rescue in rbuf_fill': Net::ReadTimeout (Net::ReadTimeout)
from /Users/luigi/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/protocol.rb:152:in `rbuf_fill'
from /Users/luigi/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/protocol.rb:134:in `readuntil'
from /Users/luigi/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/protocol.rb:144:in `readline'
from /Users/luigi/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http/response.rb:39:in `read_status_line'
from /Users/luigi/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http/response.rb:28:in `read_new'
from /Users/luigi/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1406:in `block in transport_request'
from /Users/luigi/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1403:in `catch'
from /Users/luigi/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1403:in `transport_request'
from /Users/luigi/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1376:in `request'
from /Users/luigi/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1369:in `block in request'
from /Users/luigi/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:852:in `start'
from /Users/luigi/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1367:in `request'
from /Users/luigi/.rvm/gems/ruby-2.0.0-p247@pdf/gems/httparty-0.12.0/lib/httparty/request.rb:93:in `perform'
from /Users/luigi/.rvm/gems/ruby-2.0.0-p247@pdf/gems/httparty-0.12.0/lib/httparty.rb:486:in `perform_request'
from /Users/luigi/.rvm/gems/ruby-2.0.0-p247@pdf/gems/httparty-0.12.0/lib/httparty.rb:423:in `get'
from /Users/jmccann/.rvm/gems/ruby-2.0.0-p247@pdf/gems/httparty-0.12.0/lib/httparty.rb:518:in `get'
My question is why is this happening? Is this error indicative of an error with the API, or is there something I could do to cause it?
我的问题是为什么会发生这种情况?此错误是否表示 API 出现错误,或者我可以做些什么来导致它?
My code:
我的代码:
This is the call that works:
这是有效的调用:
url = HTTParty.get("https://dev.test.com#{call}",
:basic_auth => auth,
:headers => {'Accept' => 'application/json' } )
This is the call that doesn't work:
这是不起作用的调用:
url = HTTParty.get("https://dev.test.com#{call}",
:basic_auth => auth,
:headers => {'Accept' => 'application/json' } )
The only thing that changes at all is the actual call, but they are both valid calls according to the API Documentation.
唯一改变的是实际的call,但根据 API 文档,它们都是有效的调用。
采纳答案by jaysqrd
From the Ruby docs: http://ruby-doc.org/stdlib-2.1.2/libdoc/net/http/rdoc/Net/HTTP.html
来自 Ruby 文档:http: //ruby-doc.org/stdlib-2.1.2/libdoc/net/http/rdoc/Net/HTTP.html
Number of seconds to wait for one block to be read (via one read(2) call). Any number may be used, including Floats for fractional seconds. If the HTTP object cannot read data in this many seconds, it raises a Net::ReadTimeout exception. The default value is 60 seconds.
等待读取一个块的秒数(通过一次 read(2) 调用)。可以使用任何数字,包括小数秒的浮点数。如果 HTTP 对象在这么多秒内无法读取数据,则会引发 Net::ReadTimeout 异常。默认值为 60 秒。
What is call? Are the service endpoints different? Are you paging through records? APIs often rate limit callers to prevent DDOS attacks. You can try wrapping your api calls in some retry logic or adding some sleep code.
什么是call?服务端点是否不同?你在翻阅记录吗?API 通常会限制调用者的速率以防止 DDOS 攻击。您可以尝试将 api 调用包装在一些重试逻辑中或添加一些睡眠代码。

