我正在使用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'
我的问题是为什么会发生这种情况?这个错误是指示API的错误,还是我可以做些什么来引起它?
我的代码:
url = HTTParty.get("https://dev.test.com#{call}",
:basic_auth => auth,
:headers => {'Accept' => 'application/json' } )
url = HTTParty.get("https://dev.test.com#{call}",
:basic_auth => auth,
:headers => {'Accept' => 'application/json' } )
唯一改变的是实际的call
,但根据API文档,它们都是有效的调用。
答案 0 :(得分:15)
来自Ruby文档:http://ruby-doc.org/stdlib-2.1.2/libdoc/net/http/rdoc/Net/HTTP.html
等待读取一个块的秒数(通过一次读取(2)调用)。 可以使用任何数字,包括小数秒的浮点数。 如果HTTP对象在这么多秒内无法读取数据, 它引发了一个Net :: ReadTimeout异常。默认值为60秒。
什么是call
?服务端点是否不同?你在翻阅记录吗? API通常会限制呼叫者以防止DDOS攻击。您可以尝试在某些重试逻辑中包装api调用或添加一些睡眠代码。