【发布时间】:2020-03-16 07:13:10
【问题描述】:
我正在使用我的 Ruby mongo 驱动程序执行此查询:
begin
User.collection.find({}).count()
rescue => e
Rails.logger.error e.to_s
end
我想捕获此操作失败的所有情况。它失败的主要原因是服务器不可用。
例如,我偶尔看到的错误之一是:
Mongo::Error::NoServerAvailable (No server is available matching preference: #<Mongo::ServerSelector::Primary:0x70302744731080 tag_sets=[] max_staleness=nil> using server_selection_timeout=30 and local_threshold=0.015)
我想在 6 秒后捕获错误。
从the docs 我看到有几个不同的超时选项(connect_timeout、server_selection_timeout、socket_timeout)。但我不确定要通过哪个,以及如何通过它们。
【问题讨论】: