Skip to content

Commit

Permalink
Merge branch 'features/CONJS-178' into maintenance/3.x
Browse files Browse the repository at this point in the history
# Conflicts:
#	lib/config/pool-options.js
  • Loading branch information
diego Dupin committed Oct 13, 2021
2 parents a966f99 + 094aacf commit 8d11d51
Show file tree
Hide file tree
Showing 37 changed files with 3,936 additions and 3,193 deletions.
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
"plugin:prettier/recommended"
],
"parserOptions": {
"ecmaVersion": 2020
"ecmaVersion": 2022,
"sourceType": "module"
},
"env": {
"node": true,
"es6": true
"es2021": true
},
"rules": {
"linebreak-style": ["error", "unix"],
Expand Down
17 changes: 17 additions & 0 deletions benchmarks/benchs/do_pool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const assert = require('assert');

module.exports.title = 'do <random number>';
module.exports.displaySql = 'do ?';
module.exports.pool = true;
module.exports.benchFct = function (pool, deferred) {
pool
.query('do ?', [Math.floor(Math.random() * 50000000)])
.then((rows) => {
// let val = Array.isArray(rows) ? rows[0] : rows;
// assert.equal(1, val.info ? val.info.affectedRows : val.affectedRows);
deferred();
})
.catch((err) => {
throw err;
});
};
25 changes: 17 additions & 8 deletions callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ const pkg = require('./package.json');
require('please-upgrade-node')(pkg);

const ConnectionCallback = require('./lib/connection-callback');
const PoolClusterCallback = require('./lib/pool-cluster-callback');
const ClusterCallback = require('./lib/cluster-callback');
const PoolCallback = require('./lib/pool-callback');

const ConnOptions = require('./lib/config/connection-options');
const PoolOptions = require('./lib/config/pool-options');
const PoolClusterOptions = require('./lib/config/pool-cluster-options');
const ClusterOptions = require('./lib/config/cluster-options');
const Connection = require('./lib/connection');

module.exports.version = require('./package.json').version;
module.exports.SqlError = require('./lib/misc/errors').SqlError;
Expand All @@ -25,17 +26,25 @@ module.exports.defaultOptions = function defaultOptions(opts) {
};

module.exports.createConnection = function createConnection(opts) {
return new ConnectionCallback(new ConnOptions(opts));
const conn = new Connection(new ConnOptions(opts));
const connCallback = new ConnectionCallback(conn);
conn
.connect()
.then(
function () {
conn.emit('connect');
}.bind(conn)
)
.catch(conn.emit.bind(conn, 'connect'));
return connCallback;
};

exports.createPool = function createPool(opts) {
const options = new PoolOptions(opts);
const pool = new PoolCallback(options);
pool.initialize();
return pool;
return new PoolCallback(options);
};

exports.createPoolCluster = function createPoolCluster(opts) {
const options = new PoolClusterOptions(opts);
return new PoolClusterCallback(options);
const options = new ClusterOptions(opts);
return new ClusterCallback(options);
};
2 changes: 1 addition & 1 deletion documentation/promise-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ Specific options for pools are :
| **`noControlAfterUse`** | After giving back connection to pool (connection.end) connector will reset or rollback connection to ensure a valid state. This option permit to disable those controls|*boolean*| false|
| **`resetAfterUse`** | When a connection is given back to pool, reset the connection if the server allows it (only for MariaDB version >= 10.2.22 /10.3.13). If disabled or server version doesn't allows reset, pool will only rollback open transaction if any|*boolean*| true before version 3, false since|
| **`leakDetectionTimeout`** |Permit to indicate a timeout to log connection borrowed from pool. When a connection is borrowed from pool and this timeout is reached, a message will be logged to console indicating a possible connection leak. Another message will tell if the possible logged leak has been released. A value of 0 (default) meaning Leak detection is disable |*integer*| 0|

| **`pingTimeout`** | validation timeout (ping) for checking an connection not used recently from pool in ms. |*integer* | 500 |

## `createPoolCluster(options) → PoolCluster`

Expand Down
51 changes: 33 additions & 18 deletions lib/pool-cluster-callback.js → lib/cluster-callback.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const PoolCluster = require('./pool-cluster');
const util = require('util');
const Cluster = require('./cluster');

/**
* Create a new Cluster.
Expand All @@ -11,30 +10,33 @@ const util = require('util');
* @param args cluster argurments. see pool-cluster-options.
* @constructor
*/
function PoolClusterCallback(args) {
PoolCluster.call(this, args);
this.setCallback();

const initialGetConnection = this.getConnection.bind(this);
const initialEnd = this.end.bind(this);
class ClusterCallback {
#cluster;
constructor(args) {
this.#cluster = new Cluster(args);
this.#cluster._setCallback();
this.on = this.#cluster.on.bind(this.#cluster);
this.once = this.#cluster.once.bind(this.#cluster);
}

/**
* End cluster (and underlying pools).
*
* @param callback - not mandatory
*/
this.end = (callback) => {
end(callback) {
if (callback && typeof callback !== 'function') {
throw new Error('callback parameter must be a function');
}
const endingFct = callback ? callback : () => {};

initialEnd()
this.#cluster
.end()
.then(() => {
endingFct();
})
.catch(endingFct);
};
}

/**
* Get connection from available pools matching pattern, according to selector
Expand All @@ -43,7 +45,7 @@ function PoolClusterCallback(args) {
* @param selector node selector ('RR','RANDOM' or 'ORDER')
* @param callback callback function
*/
this.getConnection = (pattern, selector, callback) => {
getConnection(pattern, selector, callback) {
let pat = pattern,
sel = selector,
cal = callback;
Expand All @@ -55,12 +57,25 @@ function PoolClusterCallback(args) {
sel = null;
cal = selector;
}
const endingFct = cal ? cal : (conn) => {};
const endingFct = cal ? cal : (err, conn) => {};
this.#cluster.getConnection(pat, sel, endingFct);
}

initialGetConnection(pat, sel, endingFct);
};
}
add(id, config) {
this.#cluster.add(id, config);
}

of(pattern, selector) {
return this.#cluster.of(pattern, selector);
}

util.inherits(PoolClusterCallback, PoolCluster);
remove(pattern) {
this.#cluster.remove(pattern);
}

get __tests() {
return this.#cluster.__tests;
}
}

module.exports = PoolClusterCallback;
module.exports = ClusterCallback;
Loading

0 comments on commit 8d11d51

Please sign in to comment.