2013-01-04 87 views
7

我一直在寻找很多mongodb中意外关闭的连接,但只能从想要关闭它们的连接的用户那里找到问题。node-mongodb-native MongoClient意外关闭连接

我正在使用node-mongodb-native连接到一个数据库,但我一直在看似随机的“错误:连接关闭”消息。如果我手动重试请求(浏览器刷新),请求将起作用。

任何想法是什么造成这种情况?有一些简单的选项可以帮助吗?

我得到使用我的分贝手柄:

 MongoClient.connect(connection_string, { auto_reconnect: true }, function (err, db) { 
    //server code/routes in here 
    } 

我一直在寻找通过https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/connection/server.js但我知道我如何连接池一般被绊倒了我的管理理解有限。我的印象是他们会在我的服务器的一生中保持开放。有人可以帮忙吗?

编辑: 阅读mjhm的评论后,我开始更深入地研究TCP保持活力。偶然发现一些网站表示这可能是Azure的做法(现在这个问题被错误分类了!)。显然,Azure负载平衡器会在1分钟的活动后终止连接。我使用的是Azure网站,因此可能适用也可能不适用,但我认为这种见解足以让我们开始新的调查。这里http://blogs.msdn.com/b/avkashchauhan/archive/2011/11/12/windows-azure-load-balancer-timeout-details.aspx

+0

你能否提供更多信息?你的数据库是本地主机还是其他机器?什么数据库调用触发连接关闭的消息?服务器上有多少负载?等等 – mjhm

+0

嗨,数据库是在mongohq上托管的。该服务的负载很少。我无法可靠地重现该问题,但我注意到find()调用确实失败。我尝试在node-mongodb-native驱动器中将keepAlive标志硬编码为true,但那也没用。 –

+2

本文有一些建议,http://christiankvalheim.com/post/32209721702/tcp-keepalive – mjhm

回答

5

从更多细节:http://christiankvalheim.com/post/32209721702/tcp-keepalive

TCP keepalive One thing that comes up quite frequently as a question when using the mongodb node.js driver is a socket that stops responding. This usually have two sources.

There is a firewall in between the application and the mongodb instance and it does not observe keepAlive.

The socket timeout is to high on your system leaving the socket hanging and never closing. The first situation can be remedied by setting the socket connection options and enabling keepAlive and setting a hard timeout value on the socket. This will ensure that a correctly configured firewall will keep the connection alive and if it does not it will timeout. The other thing to tweak is the os tcp_keepalive_time. Basically it’s to high for something like MongoDB (default 2 hours on linux). Setting this lower will correctly timeout dead sockets and let the driver recover.

A good link to read more about it. http://www.mongodb.org/display/DOCS/Troubleshooting#Troubleshooting-Socketerrorsinshardedclustersandreplicasets