我使用central_logger来存储mongodb中我们的Rails应用程序的日志。当mongo服务器最近宕机时,我们的应用程序开始在mongo插件上超时。如何防止Rails在mongo服务器宕机时超时?
发布于 2011-05-03 22:01:26
ruby driver支持这样的超时
@conn = Connection.new("localhost", 27017, :pool_size => 5, :timeout => 5)但central_logger gem并没有使用它。因此,您可以派生它以将其添加到其中,或者使用猴子路径将CentralLogger::MongoLogger.connect方法
def connect
@mongo_connection ||= Mongo::Connection.new(@db_configuration['host'],
@db_configuration['port'],
:auto_reconnect => true).db(@db_configuration['database'])
if @db_configuration['username'] && @db_configuration['password']
# the driver stores credentials in case reconnection is required
@authenticated = @mongo_connection.authenticate(@db_configuration['username'],
@db_configuration['password'])
end
end您需要通过monkey-path将:timeout=>5 (或其他任何内容)连接到Mongo::Connection.new
我敢打赌,central-logger的作者会希望有这样的东西,所以fork和pull请求可能会很受欢迎。
发布于 2011-05-03 21:51:53
您可以使用replica sets -因此,如果主服务器宕机,它可以自动故障转移到其中一个副本服务器。
发布于 2011-05-03 21:54:19
通常,数据库插入应该很快,所以您可以使用ruby超时:
require 'timeout'
Timeout::timeout(0.2) do
... write to log server
end在任何情况下,此代码都将超时并在200毫秒后继续。
https://stackoverflow.com/questions/5870459
复制相似问题