{"action":"opened","issue":{"url":"https://api.github.com/repos/brianc/node-pg-pool/issues/36","repository_url":"https://api.github.com/repos/brianc/node-pg-pool","labels_url":"https://api.github.com/repos/brianc/node-pg-pool/issues/36/labels{/name}","comments_url":"https://api.github.com/repos/brianc/node-pg-pool/issues/36/comments","events_url":"https://api.github.com/repos/brianc/node-pg-pool/issues/36/events","html_url":"https://github.com/brianc/node-pg-pool/issues/36","id":192862831,"number":36,"title":"`done` doesn't return connection","user":{"login":"bryanburgers","id":56045,"avatar_url":"https://avatars.githubusercontent.com/u/56045?v=3","gravatar_id":"","url":"https://api.github.com/users/bryanburgers","html_url":"https://github.com/bryanburgers","followers_url":"https://api.github.com/users/bryanburgers/followers","following_url":"https://api.github.com/users/bryanburgers/following{/other_user}","gists_url":"https://api.github.com/users/bryanburgers/gists{/gist_id}","starred_url":"https://api.github.com/users/bryanburgers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bryanburgers/subscriptions","organizations_url":"https://api.github.com/users/bryanburgers/orgs","repos_url":"https://api.github.com/users/bryanburgers/repos","events_url":"https://api.github.com/users/bryanburgers/events{/privacy}","received_events_url":"https://api.github.com/users/bryanburgers/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-12-01T14:35:05Z","updated_at":"2016-12-01T14:35:05Z","closed_at":null,"body":"When using `pool.connect` in the callback form, `done(client)` and `client.release()` do not behave in the same way.\r\n\r\nIf the pool is fully utilized and there are `.connect` requests waiting for a client, `done(client)` does not seem to allow those waiting requests to complete. `client.release()` does.\r\n\r\nUsing the following code:\r\n\r\n```\r\n'use strict';\r\n\r\nconst pg = require('pg');\r\n\r\nconst databasePool = new pg.Pool({\r\n\t/* Other connection params here */\r\n\tmax: 2,\r\n});\r\n\r\nlet requested = 0;\r\nlet retrieved = 0;\r\nlet returned = 0;\r\nfor (let i = 0; i < 5; i++) {\r\n\trequested += 1;\r\n\tconsole.log(`Requested ${i} (${requested}/${retrieved}/${returned})`);\r\n\tdatabasePool.connect((err, client, done) => {\r\n\t\tretrieved += 1;\r\n\t\tconsole.log(`Retrieved ${i} (${requested}/${retrieved}/${returned})`);\r\n\t\tif (err) {\r\n\t\t\treturned += 1;\r\n\t\t\tdone(client);\r\n\t\t\t// client.release();\r\n\t\t\tconsole.log(`Returned ${i} (${requested}/${retrieved}/${returned})`);\r\n\r\n\t\t\treturn console.log(err);\r\n\t\t}\r\n\r\n\t\tclient.query('select pg_sleep(1)', [], (err) => {\r\n\t\t\treturned += 1;\r\n\t\t\tdone(client);\r\n\t\t\t// client.release();\r\n\t\t\tconsole.log(`Returned ${i} (${requested}/${retrieved}/${returned})`);\r\n\t\t\tif (err) {\r\n\t\t\t\tconsole.log(err);\r\n\t\t\t}\r\n\t\t});\r\n\t});\r\n}\r\n```\r\n\r\nI get the following output. Notice that, even after the two connections are returned with `done(client)`, the other requested connections to not ever get a chance.\r\n\r\n```\r\nRequested 0 (1/0/0)\r\nRequested 1 (2/0/0)\r\nRequested 2 (3/0/0)\r\nRequested 3 (4/0/0)\r\nRequested 4 (5/0/0)\r\nRetrieved 0 (5/1/0)\r\nRetrieved 1 (5/2/0)\r\nReturned 1 (5/2/1)\r\nReturned 0 (5/2/2)\r\n```\r\n\r\nHowever, if I switch to `client.release()`, I get the following output. Notice that once a connection is returned, the connection is available for another request, as expected.\r\n\r\n```\r\nRequested 0 (1/0/0)\r\nRequested 1 (2/0/0)\r\nRequested 2 (3/0/0)\r\nRequested 3 (4/0/0)\r\nRequested 4 (5/0/0)\r\nRetrieved 0 (5/1/0)\r\nRetrieved 1 (5/2/0)\r\nRetrieved 2 (5/3/1)\r\nReturned 0 (5/3/1)\r\nRetrieved 3 (5/4/2)\r\nReturned 1 (5/4/2)\r\nRetrieved 4 (5/5/3)\r\nReturned 3 (5/5/3)\r\nReturned 2 (5/5/4)\r\nReturned 4 (5/5/5)\r\n```\r\n\r\n# Environment\r\n\r\nMac OS X 10.11.6\r\n\r\n```\r\n$ node -e 'console.log(process.versions)'\r\n{ http_parser: '2.7.0',\r\n  node: '6.8.1',\r\n  v8: '5.1.281.84',\r\n  uv: '1.9.1',\r\n  zlib: '1.2.8',\r\n  ares: '1.10.1-DEV',\r\n  icu: '57.1',\r\n  modules: '48',\r\n  openssl: '1.0.2j' }\r\n```\r\n\r\n```\r\n$ npm list\r\npg-error@1.0.0\r\n└─┬ pg@6.1.0\r\n  ├── buffer-writer@1.0.1\r\n  ├── packet-reader@0.2.0\r\n  ├── pg-connection-string@0.1.3\r\n  ├─┬ pg-pool@1.5.0\r\n  │ ├── generic-pool@2.4.2\r\n  │ └── object-assign@4.1.0\r\n  ├─┬ pg-types@1.11.0\r\n  │ ├── ap@0.2.0\r\n  │ ├── postgres-array@1.0.0\r\n  │ ├── postgres-bytea@1.0.0\r\n  │ ├── postgres-date@1.0.3\r\n  │ └── postgres-interval@1.0.2\r\n  ├─┬ pgpass@1.0.1\r\n  │ └── split@1.0.0\r\n  └── semver@4.3.2"}}
{"actor":{"display_login":"bryanburgers"}}