8000 feat: pool adds gracefulExit to end gracefully by HQidea · Pull Request #1810 · mysqljs/mysql · GitHub
[go: up one dir, main page]

Skip to content

feat: pool adds gracefulExit to end gracefully #1810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: gracefulExit is given to pool.end
  • Loading branch information
HQidea committed Sep 9, 2017
commit f39bce5a6b955e38026548d134d4fc32462eb377
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
coverage/
node_modules/
npm-debug.log
.DS_Store
31 changes: 21 additions & 10 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ constructor. In addition to those options pools accept a few extras:
* `queueLimit`: The maximum number of connection requests the pool will queue
before returning an error from `getConnection`. If set to `0`, there is no
limit to the number of queued connection requests. (Default: `0`)
* `gracefulExit`: Determines whether to end gracefully. If `true`, every `pool.getConnection` or `pool.query` called before `pool.end` will success. If `false`, only commands / queries already in progress will complete, others will throw an error.

## Pool events

Expand Down Expand Up @@ -452,25 +451,37 @@ trying to gracefully shutdown a server. To end all the connections in the
pool, use the `end` method on the pool:

```js
pool.end(function (err) {
pool.end(false, function (err) {
// all connections in the pool have ended
});
```

The `end` method takes an _optional_ callback that you can use to know once
all the connections have ended.
The `end` method takes two _optional_ arguemnts:

* `gracefulExit`: Determines whether to end gracefully. If `true`, every
`pool.getConnection` or `pool.query` called before `pool.end` will complete.
If `false`, only commands / queries already in progress will complete,
others will fail. (Default: `false`)

* `callback`: Will be called once all the connections have ended.

**Once `pool.end()` has been called, `pool.getConnection` and other operations
can no longer be performed**

If `gracefulExit` is set to `true`, the connections end _gracefully_, so all
-pending queries will still complete and the time to end the pool will vary.
### Under the hood

If `gracefulExit` is set to `true`, after calling `pool.end` the poll will
enter into the `pendingClose` state, all former or queued queries will still
complete. But the pool will no longer accept new queries.

The default `gracefulExit` is `false`, the following behavior will take effect.
This works by not queueing the `QUIT` packet on all the connections until there
is no connection in the aquiring state and no queued queries. All established
connections will still queue queries which were added before calling `pool.end`.

This works by calling `connection.end()` on every active connection in the
pool, which queues a `QUIT` packet on the connection. And sets a flag to
prevent `pool.getConnection` from continuing to create any new connections.
If `gracefulExit` is set to `false`, `pool.end` works by calling `connection.end()`
on every active connection in the pool, which queues a `QUIT` packet on the
connection. And sets a flag to prevent `pool.getConnection` from continuing to
create any new connections.

Since this queues a `QUIT` packet on each connection, all commands / queries
already in progress will complete, just like calling `connection.end()`. If
Expand Down
20 changes: 12 additions & 8 deletions lib/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ function Pool(options) {
this._freeConnections = [];
this._connectionQueue = [];
this._closed = false;
this._pendingClosing = false;
this._pendingClose = false;
}

Pool.prototype.getConnection = function (cb, queued) {
Pool.prototype.getConnection = function (cb, _queued) {

if (this._closed) {
var err = new Error('Pool is closed.');
Expand All @@ -34,13 +34,13 @@ Pool.prototype.getConnection = function (cb, queued) {
var connection;
var pool = this;
< 8000 span class='blob-code-inner blob-code-marker ' data-code-marker=" ">
if (this._freeConnections.length > 0 && (!this._pendingClosing || queued)) {
if (this._freeConnections.length > 0 && (!this._pendingClose || _queued)) {
connection = this._freeConnections.shift();
this.acquireConnection(connection, cb);
return;
}

if (this._pendingClosing) {
if (this._pendingClose) {
var err = new Error('Pool is closed.');
err.code = 'POOL_CLOSED';
process.nextTick(function () {
Expand Down Expand Up @@ -152,8 +152,8 @@ Pool.prototype.releaseConnection = function releaseConnection(connection) {
this.emit('release', connection);
}

if (this._pendingClosing) {
this.end(this._endCallback);
if (this._pendingClose) {
this.end(true, this._endCallback);
}
}

Expand All @@ -172,7 +172,11 @@ Pool.prototype.releaseConnection = function releaseConnection(connection) {
}
};

Pool.prototype.end = function (cb) {
Pool.prototype.end = function (gracefulExit, cb) {
if (typeof gracefulExit === 'function') {
cb = gracefulExit;
gracefulExit = false;
}
if (typeof cb !== 'function') {
cb = function (err) {
if (err) throw err;
Expand All @@ -194,7 +198,7 @@ Pool.prototype.end = function (cb) {
readyToEnd = true;
}

if (!this.config.gracefulExit || readyToEnd) {
if (!gracefulExit || readyToEnd) {
this._closed = true;

while (this._allConnections.length !== 0) {
Expand Down
3 changes: 0 additions & 3 deletions lib/PoolConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ function PoolConfig(options) {
this.queueLimit = (options.queueLimit === undefined)
? 0
: Number(options.queueLimit);
this.gracefulExit = (options.gracefulExit === undefined)
? false
: Boolean(options.gracefulExit);
}

PoolConfig.prototype.newConnectionConfig = function newConnectionConfig() {
Expand Down
5 changes: 2 additions & 3 deletions test/unit/pool/test-graceful-exit-ping.js
9E88
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ var pool = common.createPool({
connectionLimit : 1,
port : common.fakeServerPort,
queueLimit : 5,
waitForConnections : true,
gracefulExit : true
waitForConnections : true
});

var server = common.createFakeServer();
Expand All @@ -22,7 +21,7 @@ server.listen(common.fakeServerPort, function (err) {
conn.release();
});

pool.end(function (err) {
pool.end(true, function (err) {
assert.ifError(err);
server.destroy();
});
Expand Down
5 changes: 2 additions & 3 deletions test/unit/pool/test-graceful-exit-queued.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ var pool = common.createPool({
connectionLimit : 1,
port : common.fakeServerPort,
queueLimit : 5,
waitForConnections : true,
gracefulExit : true
waitForConnections : true
});

var server = common.createFakeServer();
Expand All @@ -16,7 +15,7 @@ server.listen(common.fakeServerPort, function (err) {
pool.getConnection(function (err, conn) {
assert.ifError(err);

pool.end(function (err) {
pool.end(true, function (err) {
assert.ifError(err);
server.destroy();
});
Expand Down
0