处理MongoDB与Node断开连接/重新连接的问题 [英] Handling MongoDB disconnect/reconnects from Node
问题描述
当我的MongoDB连接空闲几分钟后,下一个请求将错误结束.在mongo
命令行客户端中,它看起来像这样:
> db.users.find()
Sat Jan 12 23:42:35 Socket recv() errno:54 Connection reset by peer 107.22.25.25:47207
Sat Jan 12 23:42:35 SocketException: remote: 107.22.25.25:47207 error: 9001 socket exception [1] server [107.22.25.25:47207]
Sat Jan 12 23:42:35 DBClientCursor::init call() failed
Sat Jan 12 23:42:35 query failed : chowology.users {} to: ds047207.mongolab.com:47207
Error: error doing query: failed
Sat Jan 12 23:42:35 trying reconnect to ds047207.mongolab.com:47207
Sat Jan 12 23:42:35 reconnect ds047207.mongolab.com:47207 ok
我看到了针对MongoHQ和MongoLab沙盒实例的问题.
由于重新连接,下一个请求通过正常.这是我的Web应用程序中的一个问题,因为几分钟不活动后,在Web请求期间将出现此错误.有两件事让我感到惊讶:
- 如此频繁和频繁地破坏MongoDB连接,并且
- 驱动程序仅引发异常,而不是在重新连接后自动重试(我正在使用connect-mongo,该连接使用mongoose,而mongoose则使用解决方案
您要查看服务器对象的文档
http://mongodb.github.com/node-mongodb-native/api-generated/server.html#server
特别是socketOptions,您可以在其中设置keepAlive和连接超时.默认情况下,keepalive关闭并且超时为0或从不,这意味着os默认套接字超时有效(从os到os有所不同).保持活动状态将在tcp套接字连接下不时发送一次数据包,以使其保持活动状态.有时,防火墙配置错误,并且在关闭连接时不会发送结束数据包,从而使连接死机且处于混乱状态,这是人们一直在谈论的monoglabs(通常来说,它们是经过可怕配置的).>
When my MongoDB connection is idle for a few minutes, the next request ends in error. From the mongo
command line client, it looks like this:
> db.users.find()
Sat Jan 12 23:42:35 Socket recv() errno:54 Connection reset by peer 107.22.25.25:47207
Sat Jan 12 23:42:35 SocketException: remote: 107.22.25.25:47207 error: 9001 socket exception [1] server [107.22.25.25:47207]
Sat Jan 12 23:42:35 DBClientCursor::init call() failed
Sat Jan 12 23:42:35 query failed : chowology.users {} to: ds047207.mongolab.com:47207
Error: error doing query: failed
Sat Jan 12 23:42:35 trying reconnect to ds047207.mongolab.com:47207
Sat Jan 12 23:42:35 reconnect ds047207.mongolab.com:47207 ok
I see the problem against sandbox instances of MongoHQ and MongoLab.
The next request goes through fine, because of the reconnect. This is a problem in my web app, because after a few minutes of inactivity, this error will come up during a web request. There are two things that surprise me:
- That MongoDB connections are destroyed so regularly and frequently, and
- That the driver simply raises an exception as opposed to automatically retrying after reconnecting (I'm using connect-mongo which uses mongoose which, in turn, uses node-mongodb-native).
Is this everyone else's experience? How is this supposed to be handled? I'd be surprised if app developers wrap their database operations in some retry-exception-handling nonsense.
You want to look at the docs for the Server object
http://mongodb.github.com/node-mongodb-native/api-generated/server.html#server
Especially the socketOptions where you can set keepAlive and the connection timeouts. By default keepalive is off and timeout is 0 or never which means the os default socket timeout is in effect (varies from os to os). Keep alive will send a packet once in awhile down the tcp socket connection to keep it alive. Sometimes firewalls are badly configured and don't send an end packet when they close a connection leaving the connection dead and in limbo which is what the monoglabs people are talking about (more often than not to be honest they are horribly configured).
这篇关于处理MongoDB与Node断开连接/重新连接的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!