Skip to content

New Pool options and refactoring. #1591

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 23 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,27 @@ 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`)
* `queueWaitTimeout`: The maximum number of milliseconds that the pool will wait
for a connection when there are no available spare connections. If set to `0`,
there is no limit. (Default: `0`)
* `pingCheckInterval`: The number of milliseconds to indicate how often to check
the validity of the connection. If set to `0`, checks whether or not the connection
to the server is working every time. Setting this value in high-traffic can
improve performance. (Default: `0`)
* `startConnections`: The initial number of connections that are created when the
pool is started. (Default: `0`)
* `minSpareConnections`: The minimum number of spare connections that should be
kept in the pool at all times. If set to `0`, it doen't work. (Default: `0`)
* `maxSpareConnections`: The maximum number of spare connections that should be
kept in the pool at all times. If set to `0`, it doen't work. (Default: `0`)
* `spareCheckInterval`: The number of milliseconds to indicate how often to check
the status of spare connections. If set to `0`, it doen't work. (Default: `0`)

## Pool events

### connection

The pool will emit a `connection` event when a new connection is made within the pool.
The pool will emit a `connection` event when a new connection is made within the pool.
If you need to set session variables on the connection before it gets used, you can
listen to the `connection` event.

Expand All @@ -415,6 +430,11 @@ pool.on('enqueue', function () {
});
```

### prepared

The pool will emit an `prepared` event when all new connections are created in.
It's meaningful when `startConnections` is set.

## Closing all the connections in a pool

When you are done using the pool, you have to end all the connections or the
Expand Down Expand Up @@ -463,7 +483,7 @@ poolCluster.getConnection('MASTER', function (err, connection) {});
// Target Group : SLAVE1-2, Selector : order
// If can't connect to SLAVE1, return SLAVE2. (remove SLAVE1 in the cluster)
poolCluster.on('remove', function (nodeId) {
console.log('REMOVED NODE : ' + nodeId); // nodeId = SLAVE1
console.log('REMOVED NODE : ' + nodeId); // nodeId = SLAVE1
});

poolCluster.getConnection('SLAVE*', 'ORDER', function (err, connection) {});
Expand All @@ -485,7 +505,7 @@ poolCluster.end(function (err) {
### PoolCluster options

* `canRetry`: If `true`, `PoolCluster` will attempt to reconnect when connection fails. (Default: `true`)
* `removeNodeErrorCount`: If connection fails, node's `errorCount` increases.
* `removeNodeErrorCount`: If connection fails, node's `errorCount` increases.
When `errorCount` is greater than `removeNodeErrorCount`, remove a node in the `PoolCluster`. (Default: `5`)
* `restoreNodeTimeout`: If connection fails, specifies the number of milliseconds
before another connection attempt will be made. If set to `0`, then node will be
Expand Down
20 changes: 16 additions & 4 deletions lib/ConnectionConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,22 @@ function ConnectionConfig(options) {
: options.charsetNumber||Charsets.UTF8_GENERAL_CI;

// Set the client flags
var defaultFlags = ConnectionConfig.getDefaultFlags(options);
this.clientFlags = ConnectionConfig.mergeFlags(defaultFlags, options.flags);
this.clientFlags = ConnectionConfig._getClientFlags(this.multipleStatements, this.flags);
}

var _clientFlagsCache = {};

ConnectionConfig._getClientFlags = function(multipleStatements, flags) {
var key = flags + multipleStatements;

if (_clientFlagsCache[key] === undefined) {
var defaultFlags = ConnectionConfig.getDefaultFlags(multipleStatements);
_clientFlagsCache[key] = ConnectionConfig.mergeFlags(defaultFlags, flags);
}

return _clientFlagsCache[key];
};

ConnectionConfig.mergeFlags = function mergeFlags(defaultFlags, userFlags) {
var allFlags = ConnectionConfig.parseFlagList(defaultFlags);
var newFlags = ConnectionConfig.parseFlagList(userFlags);
Expand Down Expand Up @@ -93,7 +105,7 @@ ConnectionConfig.getCharsetNumber = function getCharsetNumber(charset) {
return num;
};

ConnectionConfig.getDefaultFlags = function getDefaultFlags(options) {
ConnectionConfig.getDefaultFlags = function getDefaultFlags(multipleStatements) {
var defaultFlags = [
'-COMPRESS', // Compression protocol *NOT* supported
'-CONNECT_ATTRS', // Does *NOT* send connection attributes in Protocol::HandshakeResponse41
Expand All @@ -114,7 +126,7 @@ ConnectionConfig.getDefaultFlags = function getDefaultFlags(options) {
'+TRANSACTIONS' // Expects status flags
];

if (options && options.multipleStatements) {
if (multipleStatements) {
// May send multiple statements per COM_QUERY and COM_STMT_PREPARE
defaultFlags.push('+MULTI_STATEMENTS');
}
Expand Down
Loading