Comparing version 3.11.5 to 3.11.6-canary.9a386018
@@ -16,13 +16,26 @@ 'use strict'; | ||
let index = 0; | ||
return clusterIds => clusterIds[index++ % clusterIds.length]; | ||
return (clusterIds) => clusterIds[index++ % clusterIds.length]; | ||
}, | ||
RANDOM() { | ||
return clusterIds => | ||
return (clusterIds) => | ||
clusterIds[Math.floor(Math.random() * clusterIds.length)]; | ||
}, | ||
ORDER() { | ||
return clusterIds => clusterIds[0]; | ||
return (clusterIds) => clusterIds[0]; | ||
} | ||
}; | ||
const getMonotonicMilliseconds = function () { | ||
let ms; | ||
if (typeof process.hrtime === 'function') { | ||
ms = process.hrtime(); | ||
ms = ms[0] * 1e3 + ms[1] * 1e-6; | ||
} else { | ||
ms = process.uptime() * 1000; | ||
} | ||
return Math.floor(ms); | ||
}; | ||
class PoolNamespace { | ||
@@ -38,11 +51,24 @@ constructor(cluster, pattern, selector) { | ||
if (clusterNode === null) { | ||
return cb(new Error('Pool does Not exists.')); | ||
let err = new Error('Pool does Not exist.'); | ||
err.code = 'POOL_NOEXIST'; | ||
if (this._cluster._findNodeIds(this._pattern, true).length !== 0) { | ||
err = new Error('Pool does Not have online node.'); | ||
err.code = 'POOL_NONEONLINE'; | ||
} | ||
return cb(err); | ||
} | ||
return this._cluster._getConnection(clusterNode, (err, connection) => { | ||
if (err) { | ||
if ( | ||
this._cluster._canRetry && | ||
this._cluster._findNodeIds(this._pattern).length !== 0 | ||
) { | ||
this._cluster.emit('warn', err); | ||
return this.getConnection(cb); | ||
} | ||
return cb(err); | ||
} | ||
if (connection === 'retry') { | ||
return this.getConnection(cb); | ||
} | ||
return cb(null, connection); | ||
@@ -84,5 +110,5 @@ }); | ||
* pool cluster execute | ||
* @param {*} sql | ||
* @param {*} values | ||
* @param {*} cb | ||
* @param {*} sql | ||
* @param {*} values | ||
* @param {*} cb | ||
*/ | ||
@@ -129,2 +155,3 @@ execute(sql, values, cb) { | ||
this._removeNodeErrorCount = config.removeNodeErrorCount || 5; | ||
this._restoreNodeTimeout = config.restoreNodeTimeout || 0; | ||
this._defaultSelector = config.defaultSelector || 'RR'; | ||
@@ -162,3 +189,4 @@ this._closed = false; | ||
errorCount: 0, | ||
pool: new Pool({ config: new PoolConfig(config) }) | ||
pool: new Pool({ config: new PoolConfig(config) }), | ||
_offlineUntil: 0 | ||
}; | ||
@@ -170,2 +198,14 @@ this._serviceableNodeIds.push(id); | ||
remove(pattern) { | ||
const foundNodeIds = this._findNodeIds(pattern, true); | ||
for (let i = 0; i < foundNodeIds.length; i++) { | ||
const node = this._getNode(foundNodeIds[i]); | ||
if (node) { | ||
this._removeNode(node); | ||
} | ||
} | ||
} | ||
getConnection(pattern, selector, cb) { | ||
@@ -190,3 +230,3 @@ let namespace; | ||
? callback | ||
: err => { | ||
: (err) => { | ||
if (err) { | ||
@@ -200,2 +240,3 @@ throw err; | ||
} | ||
this._closed = true; | ||
@@ -205,3 +246,3 @@ | ||
let waitingClose = 0; | ||
const onEnd = err => { | ||
const onEnd = (err) => { | ||
if (!calledBack && (err || --waitingClose <= 0)) { | ||
@@ -217,2 +258,3 @@ calledBack = true; | ||
} | ||
if (waitingClose === 0) { | ||
@@ -223,22 +265,41 @@ process.nextTick(onEnd); | ||
_findNodeIds(pattern) { | ||
if (typeof this._findCaches[pattern] !== 'undefined') { | ||
return this._findCaches[pattern]; | ||
_findNodeIds(pattern, includeOffline) { | ||
let currentTime = 0; | ||
let foundNodeIds = this._findCaches[pattern]; | ||
if (typeof this._findCaches[pattern] === 'undefined') { | ||
if (pattern === '*') { | ||
// all | ||
foundNodeIds = this._serviceableNodeIds; | ||
} else if (this._serviceableNodeIds.indexOf(pattern) !== -1) { | ||
// one | ||
foundNodeIds = [pattern]; | ||
} else { | ||
// wild matching | ||
const keyword = pattern.substring(pattern.length - 1, 0); | ||
foundNodeIds = this._serviceableNodeIds.filter((id) => | ||
id.startsWith(keyword) | ||
); | ||
} | ||
} | ||
let foundNodeIds; | ||
if (pattern === '*') { | ||
// all | ||
foundNodeIds = this._serviceableNodeIds; | ||
} else if (this._serviceableNodeIds.indexOf(pattern) !== -1) { | ||
// one | ||
foundNodeIds = [pattern]; | ||
} else { | ||
// wild matching | ||
const keyword = pattern.substring(pattern.length - 1, 0); | ||
foundNodeIds = this._serviceableNodeIds.filter(id => | ||
id.startsWith(keyword) | ||
); | ||
this._findCaches[pattern] = foundNodeIds; | ||
if (includeOffline) { | ||
return foundNodeIds; | ||
} | ||
this._findCaches[pattern] = foundNodeIds; | ||
return foundNodeIds; | ||
return foundNodeIds.filter((nodeId) => { | ||
const node = this._getNode(nodeId); | ||
if (!node._offlineUntil) { | ||
return true; | ||
} | ||
if (!currentTime) { | ||
currentTime = getMonotonicMilliseconds(); | ||
} | ||
return node._offlineUntil <= currentTime; | ||
}); | ||
} | ||
@@ -251,18 +312,36 @@ | ||
_increaseErrorCount(node) { | ||
if (++node.errorCount >= this._removeNodeErrorCount) { | ||
const index = this._serviceableNodeIds.indexOf(node.id); | ||
if (index !== -1) { | ||
this._serviceableNodeIds.splice(index, 1); | ||
delete this._nodes[node.id]; | ||
this._clearFindCaches(); | ||
node.pool.end(); | ||
this.emit('remove', node.id); | ||
} | ||
const errorCount = ++node.errorCount; | ||
if (this._removeNodeErrorCount > errorCount) { | ||
return; | ||
} | ||
if (this._restoreNodeTimeout > 0) { | ||
node._offlineUntil = | ||
getMonotonicMilliseconds() + this._restoreNodeTimeout; | ||
this.emit('offline', node.id); | ||
return; | ||
} | ||
this._removeNode(node); | ||
this.emit('remove', node.id); | ||
} | ||
_decreaseErrorCount(node) { | ||
if (node.errorCount > 0) { | ||
--node.errorCount; | ||
let errorCount = node.errorCount; | ||
if (errorCount > this._removeNodeErrorCount) { | ||
errorCount = this._removeNodeErrorCount; | ||
} | ||
if (errorCount < 1) { | ||
errorCount = 1; | ||
} | ||
node.errorCount = errorCount - 1; | ||
if (node._offlineUntil) { | ||
node._offlineUntil = 0; | ||
this.emit('online', node.id); | ||
} | ||
} | ||
@@ -274,9 +353,2 @@ | ||
this._increaseErrorCount(node); | ||
if (this._canRetry) { | ||
// REVIEW: this seems wrong? | ||
this.emit('warn', err); | ||
// eslint-disable-next-line no-console | ||
console.warn(`[Error] PoolCluster : ${err}`); | ||
return cb(null, 'retry'); | ||
} | ||
return cb(err); | ||
@@ -291,2 +363,12 @@ } | ||
_removeNode(node) { | ||
const index = this._serviceableNodeIds.indexOf(node.id); | ||
if (index !== -1) { | ||
this._serviceableNodeIds.splice(index, 1); | ||
delete this._nodes[node.id]; | ||
this._clearFindCaches(); | ||
node.pool.end(); | ||
} | ||
} | ||
_clearFindCaches() { | ||
@@ -293,0 +375,0 @@ this._findCaches = {}; |
{ | ||
"name": "mysql2", | ||
"version": "3.11.5", | ||
"version": "3.11.6-canary.9a386018", | ||
"description": "fast mysql driver. Implements core protocol, prepared statements, ssl and compression in native JS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -63,3 +63,3 @@ 'use strict'; | ||
this.Promise = thePromise || Promise; | ||
inheritEvents(poolCluster, this, ['warn', 'remove']); | ||
inheritEvents(poolCluster, this, ['warn', 'remove' , 'online', 'offline']); | ||
} | ||
@@ -160,3 +160,3 @@ | ||
} | ||
})(['add']); | ||
})(['add', 'remove']); | ||
@@ -163,0 +163,0 @@ function createPromisePoolCluster(opts) { |
@@ -55,2 +55,4 @@ import { EventEmitter } from 'events'; | ||
remove(pattern: string): void; | ||
end(): void; | ||
@@ -83,2 +85,4 @@ | ||
on(event: string, listener: (...args: any[]) => void): this; | ||
on(event: 'online', listener: (nodeId: number) => void): this; | ||
on(event: 'offline', listener: (nodeId: number) => void): this; | ||
on(event: 'remove', listener: (nodeId: number) => void): this; | ||
@@ -85,0 +89,0 @@ on(event: 'warn', listener: (err: Error) => void): this; |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
507572
13876
29
1