gpt4 book ai didi

Ruby 错误 Net::ReadTimeout

转载 作者:数据小太阳 更新时间:2023-10-29 06:30:20 25 4
gpt4 key购买 nike

我正在使用 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' } )

唯一发生变化的是实际的调用,但根据 API 文档,它们都是有效的调用。

最佳答案

来自 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.

什么是调用?服务端点是否不同?你在翻阅记录吗? API 通常对调用者进行速率限制以防止 DDOS 攻击。您可以尝试将 api 调用包装在一些重试逻辑中或添加一些 sleep 代码。

关于Ruby 错误 Net::ReadTimeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19798091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com