DiscofyAPI/node_modules/mariadb/lib/pool-cluster-callback.js

67 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-03-25 17:23:36 +01:00
'use strict';
const PoolCluster = require('./pool-cluster');
const util = require('util');
/**
* Create a new Cluster.
* Cluster handle pools with patterns and handle failover / distributed load
* according to selectors (round robin / random / ordered )
*
* @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);
/**
* End cluster (and underlying pools).
*
* @param callback - not mandatory
*/
this.end = (callback) => {
if (callback && typeof callback !== 'function') {
throw new Error('callback parameter must be a function');
}
const endingFct = callback ? callback : () => {};
initialEnd()
.then(() => {
endingFct();
})
.catch(endingFct);
};
/**
* Get connection from available pools matching pattern, according to selector
*
* @param pattern pattern filter (not mandatory)
* @param selector node selector ('RR','RANDOM' or 'ORDER')
* @param callback callback function
*/
this.getConnection = (pattern, selector, callback) => {
let pat = pattern,
sel = selector,
cal = callback;
if (typeof pattern === 'function') {
pat = null;
sel = null;
cal = pattern;
} else if (typeof selector === 'function') {
sel = null;
cal = selector;
}
const endingFct = cal ? cal : (conn) => {};
initialGetConnection(pat, sel, endingFct);
};
}
util.inherits(PoolClusterCallback, PoolCluster);
module.exports = PoolClusterCallback;