{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3863","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3863/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3863/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3863/events","html_url":"https://github.com/Automattic/mongoose/issues/3863","id":131862835,"number":3863,"title":"Mongoose not closing connections","user":{"login":"refaelos","id":986453,"avatar_url":"https://avatars.githubusercontent.com/u/986453?v=3","gravatar_id":"","url":"https://api.github.com/users/refaelos","html_url":"https://github.com/refaelos","followers_url":"https://api.github.com/users/refaelos/followers","following_url":"https://api.github.com/users/refaelos/following{/other_user}","gists_url":"https://api.github.com/users/refaelos/gists{/gist_id}","starred_url":"https://api.github.com/users/refaelos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/refaelos/subscriptions","organizations_url":"https://api.github.com/users/refaelos/orgs","repos_url":"https://api.github.com/users/refaelos/repos","events_url":"https://api.github.com/users/refaelos/events{/privacy}","received_events_url":"https://api.github.com/users/refaelos/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2016-02-06T14:48:02Z","updated_at":"2016-02-11T15:26:10Z","closed_at":"2016-02-11T15:26:10Z","body":"I'm using mongoose to connect to my MongoDB server on production.\r\n\r\n```\r\n  mongo   : {\r\n    uri    : process.env.MONGO_URL,\r\n    options: {\r\n      user   : process.env.MONGO_USER,\r\n      pass   : process.env.MONGO_PASS,\r\n      server : {\r\n        socketOptions: {\r\n          keepAlive       : 1,\r\n          socketTimeoutMS : 300000,\r\n          connectTimeoutMS: 300000\r\n        }\r\n      },\r\n      replset: {\r\n        rs_name      : process.env.MONGO_RS_NAME || \"set-name\",\r\n        socketOptions: {\r\n          keepAlive       : 1,\r\n          socketTimeoutMS : 300000,\r\n          connectTimeoutMS: 300000\r\n        },\r\n        readPreference: 'secondaryPreferred',\r\n        poolSize: 50\r\n      }\r\n    }\r\n\r\n  }\r\n```\r\n\r\nI've set tcp_keepalive_time to 120:\r\n\r\n```\r\necho 120 | sudo tee /proc/sys/net/ipv4/tcp_keepalive_time\r\n```\r\n\r\nThe problem is the connections keep opening to the server but never closing.\r\n\r\n1. Why are connections keep opening to the server?\r\n2. Why are connections not being closed?\r\n\r\nBtw, connections are only opening to Primary. On Secondary I don't see too many open connections."},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/182915823","html_url":"https://github.com/Automattic/mongoose/issues/3863#issuecomment-182915823","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/3863","id":182915823,"user":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars.githubusercontent.com/u/1620265?v=3","gravatar_id":"","url":"https://api.github.com/users/vkarpov15","html_url":"https://github.com/vkarpov15","followers_url":"https://api.github.com/users/vkarpov15/followers","following_url":"https://api.github.com/users/vkarpov15/following{/other_user}","gists_url":"https://api.github.com/users/vkarpov15/gists{/gist_id}","starred_url":"https://api.github.com/users/vkarpov15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkarpov15/subscriptions","organizations_url":"https://api.github.com/users/vkarpov15/orgs","repos_url":"https://api.github.com/users/vkarpov15/repos","events_url":"https://api.github.com/users/vkarpov15/events{/privacy}","received_events_url":"https://api.github.com/users/vkarpov15/received_events","type":"User","site_admin":false},"created_at":"2016-02-11T15:26:10Z","updated_at":"2016-02-11T15:26:10Z","body":"AFAIK socketTimeoutMS=0 means use the OS' socket timeout, so if that works for you go for it. I personally don't bother setting socketTImeoutMS ever. Some more info: http://mongodb.github.io/node-mongodb-native/2.0/tutorials/connection_failures/ ."}}