Handling MongoDB disconnect/reconnects from Node

…衆ロ難τιáo~ 提交于 2019-11-29 00:30:17

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).

  1. Check that your computer isn't going to sleep
  2. Check that your router/firewall is not killing idle connections

The first problem turned out to be my computer sleeping and dropping the network connection unknowingly. It's a new computer and I didn't realize I hadn't disabled the sleep :-P

Jared from MongoLab helped me troubleshoot this and I'm thankful for it. He said this behavior is common when going through a firewall (as mjhm suggested in his comment). So one test would be to bypass that.

Still going through my router, I get a different error after several hours idle:

db.users.find()
Sun Jan 13 14:55:02 Socket say send() errno:32 Broken pipe 107.22.25.25:47207
Error: 9001 socket exception [2] server [107.22.25.25:47207] 
Sun Jan 13 14:55:02 trying reconnect to ds047207.mongolab.com:47207
Sun Jan 13 14:55:02 reconnect ds047207.mongolab.com:47207 ok

I'll try it again from a server that doesn't go through my router/firewall.

The behavior of the driver raising the exception on the current op is expected and acceptable since a dropped connections is truly an exceptional case.

Update: Neither of these problems occur when I bypass my router, nor do they occur in my Nodejitsu instance, which I believe runs in a Joyent data center.

I had the same problem and I think it's because I'm accessing the internet behind a proxy server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!