rbt_mssql-pool-party
Version:
Extension of mssql that provides management of multiple connection pools, dsns, retries, and more
57 lines (49 loc) • 2.43 kB
JavaScript
;var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports, "__esModule", { value: true });exports["default"] = raceWarmupStrategy;var _debug = _interopRequireDefault(require("debug"));
var debug = (0, _debug["default"])('mssql-pool-party');
function rejectIfNoRemainingPools(reject, processed, total, resolved) {
if (processed >= total && !resolved) {
debug('warmup: failed, no pools healthy pools created');
reject(new Error("\n The connectionPoolFactory failed to create any pool(s) using\n the dsn(s) provided. Warmup has failed.\n "));
}
}
// The raceWarmupStrategy attempts to create connection pools in parallel.
// The first to succeed will resolve the promise, signaling that the warmup is done.
// If there is more than one dsn, pools will be created for the rest in the background
// and added to the ConnectionPoolParty pools collection as they are created.
function raceWarmupStrategy(dsns, connectionPoolFactory, onCreation, onError) {
// have we successfully created a pool?
var resolved = false;
// the number of pools we've created/failed
var numberProcessed = 0;
return new Promise(function (resolve, reject) {
// for each dsn, create a pool
dsns.forEach(function (dsn) {
connectionPoolFactory(dsn).
then(
// if we succeed in creating a pool, pass it to onCreation
// and resolve if we haven't already done so
function (pool) {
numberProcessed += 1;
debug('warmup: pool %d created %s%O', numberProcessed, pool.error && 'WITH ERROR\n' || '', pool.error);
onCreation(pool);
// we only want to resolve once
if (!resolved && !pool.error) {
resolved = true;
debug('warmup: resolved');
resolve();
}
rejectIfNoRemainingPools(reject, numberProcessed, dsns.length, resolved);
},
// normally, connectionPoolFactory should never reject,
// since we want it to create a pool even if it's unhealthy,
// so we can attempt to heal the pool later
function (err) {
numberProcessed += 1;
debug('warmup: connectionPoolFactory errored');
onError(err);
rejectIfNoRemainingPools(reject, numberProcessed, dsns.length, resolved);
});
});
});
}module.exports = exports.default;
//# sourceMappingURL=race-warmup-strategy.js.map