Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
Next Next commit
Add stackCycle feature for decrease useless connections
  • Loading branch information
davidshimjs committed Aug 18, 2017
commit 6cc79769423735f72b65606ec9e067c51eab32ff
5 changes: 3 additions & 2 deletions 5 Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ time one is needed.
Connections are lazily created by the pool. If you configure the pool to allow
up to 100 connections, but only ever use 5 simultaneously, only 5 connections
will be made. Connections are also cycled round-robin style, with connections
being taken from the top of the pool and returning to the bottom.
being taken from the top of the pool and returning to the bottom. If you want to change cycle strategy, you can set `stackCycle` option in pool options.

When a previous connection is retrieved from the pool, a ping packet is sent
to the server to check if the connection is still good.
Expand All @@ -392,6 +392,7 @@ 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`)
* `stackCycle`: Determines how the released connections rotates. If `true`, It's cycled stack style. If you need to disconnect a useless connection after suddenly an increase in connection, use the stack strategy. If `false`, It's cycled round-robin style. (Default: `false`)

## Pool events

Expand Down Expand Up @@ -1418,7 +1419,7 @@ and no password set for the `root` user, run:

```sh
$ mysql -u root -e "CREATE DATABASE IF NOT EXISTS node_mysql_test"
$ MYSQL_HOST=localhost MYSQL_PORT=3306 MYSQL_DATABASE=node_mysql_test MYSQL_USER=root MYSQL_PASSWORD= FILTER=integration npm test
$ MYSQL_HOST=localhost MYSQL_PORT=3306 MYSQL_DATABASE=node_mysql_test MYSQL_USER=root MYSQL_PASSWORD=park FILTER=integration npm test
```

## Todo
Expand Down
8 changes: 6 additions & 2 deletions 8 lib/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ Pool.prototype.releaseConnection = function releaseConnection(connection) {
// this won't catch all double-release cases
throw new Error('Connection already released');
} else {
// add connection to end of free queue
this._freeConnections.push(connection);
if (this.config.stackCycle) {
this._freeConnections.unshift(connection);
} else {
// add connection to end of free queue
this._freeConnections.push(connection);
}
this.emit('release', connection);
}
}
Expand Down
3 changes: 3 additions & 0 deletions 3 lib/PoolConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function PoolConfig(options) {
this.queueLimit = (options.queueLimit === undefined)
? 0
: Number(options.queueLimit);
this.stackCycle = (options.stackCycle === undefined)
? false
: Boolean(options.stackCycle);
}

PoolConfig.prototype.newConnectionConfig = function newConnectionConfig() {
Expand Down
46 changes: 46 additions & 0 deletions 46 test/unit/pool/test-connection-stack-cycle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var assert = require('assert');
var common = require('../../common');
var pool = common.createPool({
connectionLimit : 2,
port : common.fakeServerPort,
stackCycle : true
});

var server = common.createFakeServer();

server.listen(common.fakeServerPort, function (err) {
assert.ifError(err);

var releasedThreadId = null;
var releaseCount = 0;

pool.on('release', function (connection) {
releaseCount++;
releasedThreadId = connection.threadId;
});

pool.getConnection(function (err, firstConnection) {
assert.ifError(err);
assert.ok(firstConnection);

pool.getConnection(function (err, secondConnection) {
assert.ifError(err);
assert.ok(secondConnection);
firstConnection.release();
secondConnection.release();
assert.equal(releaseCount, 2);
assert.equal(releasedThreadId, secondConnection.threadId);

pool.getConnection(function (err, recycledConnection) {
assert.ifError(err);
assert.ok(recycledConnection);
assert.equal(recycledConnection.threadId, releasedThreadId);

pool.end(function (err) {
assert.ifError(err);
server.destroy();
});
});
});
});
});
Morty Proxy This is a proxified and sanitized view of the page, visit original site.