{"action":"opened","number":10,"pull_request":{"url":"https://api.github.com/repos/gistia/mongodb-promise/pulls/10","id":173703899,"html_url":"https://github.com/gistia/mongodb-promise/pull/10","diff_url":"https://github.com/gistia/mongodb-promise/pull/10.diff","patch_url":"https://github.com/gistia/mongodb-promise/pull/10.patch","issue_url":"https://api.github.com/repos/gistia/mongodb-promise/issues/10","number":10,"state":"open","locked":false,"title":"Connection Handler: fixing connection leaks","user":{"login":"verto","id":139446,"avatar_url":"https://avatars2.githubusercontent.com/u/139446?v=4","gravatar_id":"","url":"https://api.github.com/users/verto","html_url":"https://github.com/verto","followers_url":"https://api.github.com/users/verto/followers","following_url":"https://api.github.com/users/verto/following{/other_user}","gists_url":"https://api.github.com/users/verto/gists{/gist_id}","starred_url":"https://api.github.com/users/verto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/verto/subscriptions","organizations_url":"https://api.github.com/users/verto/orgs","repos_url":"https://api.github.com/users/verto/repos","events_url":"https://api.github.com/users/verto/events{/privacy}","received_events_url":"https://api.github.com/users/verto/received_events","type":"User","site_admin":false},"body":"Currently, we create a connection pool (via MongoClient.connect) and destroy it to every request, based on stress tests using a HTTP benckmarking tool [3], I realized that response 'finish' event wasn't called for some request (I don't know why, and yes, I tried to use other events) adding connection leak.\r\n\r\n### Solution propose\r\n\r\nUsing a connection handler, the runtime application will get only one call to MongoClient.connect and reuse connections.\r\nCurrently, the MongoClient Pool design [1] will grow and contract based on the usage pattern, growing connections if there is no available connections and shrinks when a connection is idle. (respecting the pool size limit)\r\n\r\n#### Why is it important?\r\n\r\nTCP connections is expansive [2] resulting with more memory consumption, RTT, window size (low  throughput) and other networking aspects. So, it's important reuses TCP connections using a connection pool, removing the complexity of manage to open and close connections.\r\n\r\n#### Using mongodb-promise client\r\n\r\n`client.connection.close()` should be removed, it is not useful and add complexity of manage db connections to developers.\r\nEverything else still amazing, keeping lazy load connection initialization and async design.\r\n\r\n```node\r\nconst { Client } = require('mongodb-promise');\r\n\r\nconst bootstrap = () => (req, res, next) => {\r\n  const client = new Client();\r\n  client.init().then(_ => {\r\n    req.instances = { client };\r\n    next();\r\n  });\r\n};\r\n```\r\n\r\n[1] https://github.com/mongodb/node-mongodb-native/blob/master/docs/articles/MongoClient.md#mongoclient-connection-pooling\r\n[2] https://upload.wikimedia.org/wikipedia/commons/2/24/TCP_Slow-Start_and_Congestion_Avoidance.svg\r\n[3] https://github.com/giltene/wrk2\r\n\r\n\r\n@vinioliveira  could you do tests and check if everything will works well. I didn't have time enough to test all situations.","created_at":"2018-03-08T10:51:47Z","updated_at":"2018-03-08T10:51:47Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/gistia/mongodb-promise/pulls/10/commits","review_comments_url":"https://api.github.com/repos/gistia/mongodb-promise/pulls/10/comments","review_comment_url":"https://api.github.com/repos/gistia/mongodb-promise/pulls/comments{/number}","comments_url":"https://api.github.com/repos/gistia/mongodb-promise/issues/10/comments","statuses_url":"https://api.github.com/repos/gistia/mongodb-promise/statuses/aa263529b5180515a3ceea79080a1b44aa830741","head":{"label":"gistia:verto/propose/connection-handler","ref":"verto/propose/connection-handler","sha":"aa263529b5180515a3ceea79080a1b44aa830741","user":{"login":"gistia","id":2652882,"avatar_url":"https://avatars1.githubusercontent.com/u/2652882?v=4","gravatar_id":"","url":"https://api.github.com/users/gistia","html_url":"https://github.com/gistia","followers_url":"https://api.github.com/users/gistia/followers","following_url":"https://api.github.com/users/gistia/following{/other_user}","gists_url":"https://api.github.com/users/gistia/gists{/gist_id}","starred_url":"https://api.github.com/users/gistia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gistia/subscriptions","organizations_url":"https://api.github.com/users/gistia/orgs","repos_url":"https://api.github.com/users/gistia/repos","events_url":"https://api.github.com/users/gistia/events{/privacy}","received_events_url":"https://api.github.com/users/gistia/received_events","type":"Organization","site_admin":false},"repo":{"id":82614431,"name":"mongodb-promise","full_name":"gistia/mongodb-promise","owner":{"login":"gistia","id":2652882,"avatar_url":"https://avatars1.githubusercontent.com/u/2652882?v=4","gravatar_id":"","url":"https://api.github.com/users/gistia","html_url":"https://github.com/gistia","followers_url":"https://api.github.com/users/gistia/followers","following_url":"https://api.github.com/users/gistia/following{/other_user}","gists_url":"https://api.github.com/users/gistia/gists{/gist_id}","starred_url":"https://api.github.com/users/gistia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gistia/subscriptions","organizations_url":"https://api.github.com/users/gistia/orgs","repos_url":"https://api.github.com/users/gistia/repos","events_url":"https://api.github.com/users/gistia/events{/privacy}","received_events_url":"https://api.github.com/users/gistia/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/gistia/mongodb-promise","description":"A MongoDB client that uses ES6 promises instead of callbacks","fork":false,"url":"https://api.github.com/repos/gistia/mongodb-promise","forks_url":"https://api.github.com/repos/gistia/mongodb-promise/forks","keys_url":"https://api.github.com/repos/gistia/mongodb-promise/keys{/key_id}","collaborators_url":"https://api.github.com/repos/gistia/mongodb-promise/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/gistia/mongodb-promise/teams","hooks_url":"https://api.github.com/repos/gistia/mongodb-promise/hooks","issue_events_url":"https://api.github.com/repos/gistia/mongodb-promise/issues/events{/number}","events_url":"https://api.github.com/repos/gistia/mongodb-promise/events","assignees_url":"https://api.github.com/repos/gistia/mongodb-promise/assignees{/user}","branches_url":"https://api.github.com/repos/gistia/mongodb-promise/branches{/branch}","tags_url":"https://api.github.com/repos/gistia/mongodb-promise/tags","blobs_url":"https://api.github.com/repos/gistia/mongodb-promise/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/gistia/mongodb-promise/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/gistia/mongodb-promise/git/refs{/sha}","trees_url":"https://api.github.com/repos/gistia/mongodb-promise/git/trees{/sha}","statuses_url":"https://api.github.com/repos/gistia/mongodb-promise/statuses/{sha}","languages_url":"https://api.github.com/repos/gistia/mongodb-promise/languages","stargazers_url":"https://api.github.com/repos/gistia/mongodb-promise/stargazers","contributors_url":"https://api.github.com/repos/gistia/mongodb-promise/contributors","subscribers_url":"https://api.github.com/repos/gistia/mongodb-promise/subscribers","subscription_url":"https://api.github.com/repos/gistia/mongodb-promise/subscription","commits_url":"https://api.github.com/repos/gistia/mongodb-promise/commits{/sha}","git_commits_url":"https://api.github.com/repos/gistia/mongodb-promise/git/commits{/sha}","comments_url":"https://api.github.com/repos/gistia/mongodb-promise/comments{/number}","issue_comment_url":"https://api.github.com/repos/gistia/mongodb-promise/issues/comments{/number}","contents_url":"https://api.github.com/repos/gistia/mongodb-promise/contents/{+path}","compare_url":"https://api.github.com/repos/gistia/mongodb-promise/compare/{base}...{head}","merges_url":"https://api.github.com/repos/gistia/mongodb-promise/merges","archive_url":"https://api.github.com/repos/gistia/mongodb-promise/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/gistia/mongodb-promise/downloads","issues_url":"https://api.github.com/repos/gistia/mongodb-promise/issues{/number}","pulls_url":"https://api.github.com/repos/gistia/mongodb-promise/pulls{/number}","milestones_url":"https://api.github.com/repos/gistia/mongodb-promise/milestones{/number}","notifications_url":"https://api.github.com/repos/gistia/mongodb-promise/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/gistia/mongodb-promise/labels{/name}","releases_url":"https://api.github.com/repos/gistia/mongodb-promise/releases{/id}","deployments_url":"https://api.github.com/repos/gistia/mongodb-promise/deployments","created_at":"2017-02-20T23:45:32Z","updated_at":"2017-02-21T02:56:36Z","pushed_at":"2018-03-08T10:44:45Z","git_url":"git://github.com/gistia/mongodb-promise.git","ssh_url":"git@github.com:gistia/mongodb-promise.git","clone_url":"https://github.com/gistia/mongodb-promise.git","svn_url":"https://github.com/gistia/mongodb-promise","homepage":"","size":63,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":1,"license":null,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"gistia:master","ref":"master","sha":"c2937c0a6359226a0e4eb7b8afc6e466c67390fe","user":{"login":"gistia","id":2652882,"avatar_url":"https://avatars1.githubusercontent.com/u/2652882?v=4","gravatar_id":"","url":"https://api.github.com/users/gistia","html_url":"https://github.com/gistia","followers_url":"https://api.github.com/users/gistia/followers","following_url":"https://api.github.com/users/gistia/following{/other_user}","gists_url":"https://api.github.com/users/gistia/gists{/gist_id}","starred_url":"https://api.github.com/users/gistia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gistia/subscriptions","organizations_url":"https://api.github.com/users/gistia/orgs","repos_url":"https://api.github.com/users/gistia/repos","events_url":"https://api.github.com/users/gistia/events{/privacy}","received_events_url":"https://api.github.com/users/gistia/received_events","type":"Organization","site_admin":false},"repo":{"id":82614431,"name":"mongodb-promise","full_name":"gistia/mongodb-promise","owner":{"login":"gistia","id":2652882,"avatar_url":"https://avatars1.githubusercontent.com/u/2652882?v=4","gravatar_id":"","url":"https://api.github.com/users/gistia","html_url":"https://github.com/gistia","followers_url":"https://api.github.com/users/gistia/followers","following_url":"https://api.github.com/users/gistia/following{/other_user}","gists_url":"https://api.github.com/users/gistia/gists{/gist_id}","starred_url":"https://api.github.com/users/gistia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gistia/subscriptions","organizations_url":"https://api.github.com/users/gistia/orgs","repos_url":"https://api.github.com/users/gistia/repos","events_url":"https://api.github.com/users/gistia/events{/privacy}","received_events_url":"https://api.github.com/users/gistia/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/gistia/mongodb-promise","description":"A MongoDB client that uses ES6 promises instead of callbacks","fork":false,"url":"https://api.github.com/repos/gistia/mongodb-promise","forks_url":"https://api.github.com/repos/gistia/mongodb-promise/forks","keys_url":"https://api.github.com/repos/gistia/mongodb-promise/keys{/key_id}","collaborators_url":"https://api.github.com/repos/gistia/mongodb-promise/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/gistia/mongodb-promise/teams","hooks_url":"https://api.github.com/repos/gistia/mongodb-promise/hooks","issue_events_url":"https://api.github.com/repos/gistia/mongodb-promise/issues/events{/number}","events_url":"https://api.github.com/repos/gistia/mongodb-promise/events","assignees_url":"https://api.github.com/repos/gistia/mongodb-promise/assignees{/user}","branches_url":"https://api.github.com/repos/gistia/mongodb-promise/branches{/branch}","tags_url":"https://api.github.com/repos/gistia/mongodb-promise/tags","blobs_url":"https://api.github.com/repos/gistia/mongodb-promise/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/gistia/mongodb-promise/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/gistia/mongodb-promise/git/refs{/sha}","trees_url":"https://api.github.com/repos/gistia/mongodb-promise/git/trees{/sha}","statuses_url":"https://api.github.com/repos/gistia/mongodb-promise/statuses/{sha}","languages_url":"https://api.github.com/repos/gistia/mongodb-promise/languages","stargazers_url":"https://api.github.com/repos/gistia/mongodb-promise/stargazers","contributors_url":"https://api.github.com/repos/gistia/mongodb-promise/contributors","subscribers_url":"https://api.github.com/repos/gistia/mongodb-promise/subscribers","subscription_url":"https://api.github.com/repos/gistia/mongodb-promise/subscription","commits_url":"https://api.github.com/repos/gistia/mongodb-promise/commits{/sha}","git_commits_url":"https://api.github.com/repos/gistia/mongodb-promise/git/commits{/sha}","comments_url":"https://api.github.com/repos/gistia/mongodb-promise/comments{/number}","issue_comment_url":"https://api.github.com/repos/gistia/mongodb-promise/issues/comments{/number}","contents_url":"https://api.github.com/repos/gistia/mongodb-promise/contents/{+path}","compare_url":"https://api.github.com/repos/gistia/mongodb-promise/compare/{base}...{head}","merges_url":"https://api.github.com/repos/gistia/mongodb-promise/merges","archive_url":"https://api.github.com/repos/gistia/mongodb-promise/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/gistia/mongodb-promise/downloads","issues_url":"https://api.github.com/repos/gistia/mongodb-promise/issues{/number}","pulls_url":"https://api.github.com/repos/gistia/mongodb-promise/pulls{/number}","milestones_url":"https://api.github.com/repos/gistia/mongodb-promise/milestones{/number}","notifications_url":"https://api.github.com/repos/gistia/mongodb-promise/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/gistia/mongodb-promise/labels{/name}","releases_url":"https://api.github.com/repos/gistia/mongodb-promise/releases{/id}","deployments_url":"https://api.github.com/repos/gistia/mongodb-promise/deployments","created_at":"2017-02-20T23:45:32Z","updated_at":"2017-02-21T02:56:36Z","pushed_at":"2018-03-08T10:44:45Z","git_url":"git://github.com/gistia/mongodb-promise.git","ssh_url":"git@github.com:gistia/mongodb-promise.git","clone_url":"https://github.com/gistia/mongodb-promise.git","svn_url":"https://github.com/gistia/mongodb-promise","homepage":"","size":63,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":1,"license":null,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/gistia/mongodb-promise/pulls/10"},"html":{"href":"https://github.com/gistia/mongodb-promise/pull/10"},"issue":{"href":"https://api.github.com/repos/gistia/mongodb-promise/issues/10"},"comments":{"href":"https://api.github.com/repos/gistia/mongodb-promise/issues/10/comments"},"review_comments":{"href":"https://api.github.com/repos/gistia/mongodb-promise/pulls/10/comments"},"review_comment":{"href":"https://api.github.com/repos/gistia/mongodb-promise/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/gistia/mongodb-promise/pulls/10/commits"},"statuses":{"href":"https://api.github.com/repos/gistia/mongodb-promise/statuses/aa263529b5180515a3ceea79080a1b44aa830741"}},"author_association":"NONE","merged":false,"mergeable":null,"rebaseable":null,"mergeable_state":"unknown","merged_by":null,"comments":0,"review_comments":0,"maintainer_can_modify":false,"commits":1,"additions":24,"deletions":2,"changed_files":2}}
{"actor":{"display_login":"verto"}}