如何检测mongodb重新连接失败的事件

时间:2018-03-21 08:28:43

标签: mongodb

我在我的应用程序中使用nodejs mongo驱动程序。我在连接中设置了以下选项:

{
  connectTimeoutMS: 30000,
  socketTimeoutMS: 30000,
  // retry to connect for 120 times
  reconnectTries: 120,
  // wait 1 second before retrying
  reconnectInterval: 1000
};

如果连接中断,它将尝试重新连接120次,每次延迟时间为1秒。我需要在重新连接期间监听服务器状态更改。我在下面添加了事件监听器:

db.on('close', this.onClose.bind(this));
db.on('error', this.onError.bind(this));
db.on('timeout', this.onTimeout.bind(this));
db.on('parseError', this.onParseError.bind(this));
db.on('reconnect', this.onReconnect.bind(this));

所有事件监听器都正常工作但我的问题是如何在120次重试后检测到reconnect失败。例如,如果服务器关闭,那么我将收到close事件。如果服务器在120秒内启动,我将收到reconnect事件。但是如果服务器在120秒内没有运行怎么办?我怎样才能发现这种变化?我应该自己实施吗?

0 个答案:

没有答案
相关问题