2013-01-04 88 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