generic-pool
Advanced tools
Comparing version 2.5.0 to 2.5.1
# Change Log | ||
## [2.5.1] - January 20 2017 | ||
- fix validateAsync logic to prevent duplicate object issuance under pressure (@felipou) | ||
## [2.5.0] - November 16 2016 | ||
@@ -125,3 +128,4 @@ - backport test suite change to node-tap | ||
[unreleased]: https://github.com/coopernurse/node-pool/compare/v2.5.0...HEAD | ||
[unreleased]: https://github.com/coopernurse/node-pool/compare/v2.5.1...HEAD | ||
[2.5.1]: https://github.com/coopernurse/node-pool/compare/v2.5.0...v2.5.1 | ||
[2.5.0]: https://github.com/coopernurse/node-pool/compare/v2.4.6...v2.5.0 | ||
@@ -128,0 +132,0 @@ [2.4.6]: https://github.com/coopernurse/node-pool/compare/v2.4.5...v2.4.6 |
@@ -151,2 +151,3 @@ /** | ||
this._availableObjects = [] | ||
this._asyncTestObjects = [] | ||
this._count = 0 | ||
@@ -269,3 +270,2 @@ this._removeIdleTimer = null | ||
var objWithTimeout = null | ||
var err = null | ||
var clientCb = null | ||
@@ -275,45 +275,66 @@ var waitingCount = this._waitingClients.size() | ||
this._log('dispense() clients=' + waitingCount + ' available=' + this._availableObjects.length, 'info') | ||
if (waitingCount > 0) { | ||
if (this._factory.validateAsync) { | ||
doWhileAsync(function () { | ||
return self._availableObjects.length > 0 | ||
}, function (next) { | ||
self._log('dispense() - reusing obj', 'verbose') | ||
objWithTimeout = self._availableObjects[0] | ||
self._factory.validateAsync(objWithTimeout.obj, function (valid) { | ||
if (!valid) { | ||
self.destroy(objWithTimeout.obj) | ||
next() | ||
} else { | ||
self._availableObjects.shift() | ||
self._inUseObjects.push(objWithTimeout.obj) | ||
clientCb = self._waitingClients.dequeue() | ||
clientCb(err, objWithTimeout.obj) | ||
} | ||
}) | ||
}, function () { | ||
if (self._count < self._factory.max) { | ||
self._createResource() | ||
} | ||
}) | ||
if (waitingCount < 1) { | ||
return | ||
} | ||
return | ||
if (this._factory.validateAsync) { | ||
doWhileAsync(function () { | ||
return self._availableObjects.length > 0 | ||
}, | ||
this._createAsyncValidator(), | ||
function () { | ||
if (self._count < self._factory.max) { | ||
self._createResource() | ||
} | ||
}) | ||
return | ||
} | ||
while (this._availableObjects.length > 0) { | ||
this._log('dispense() - reusing obj', 'verbose') | ||
objWithTimeout = this._availableObjects[0] | ||
if (!this._factory.validate(objWithTimeout.obj)) { | ||
this.destroy(objWithTimeout.obj) | ||
continue | ||
} | ||
while (this._availableObjects.length > 0) { | ||
this._log('dispense() - reusing obj', 'verbose') | ||
objWithTimeout = this._availableObjects[0] | ||
if (!this._factory.validate(objWithTimeout.obj)) { | ||
this.destroy(objWithTimeout.obj) | ||
continue | ||
this._availableObjects.shift() | ||
this._inUseObjects.push(objWithTimeout.obj) | ||
clientCb = this._waitingClients.dequeue() | ||
return clientCb(null, objWithTimeout.obj) | ||
} | ||
if (this._count < this._factory.max) { | ||
this._createResource() | ||
} | ||
} | ||
Pool.prototype._createAsyncValidator = function _createAsyncValidator () { | ||
var self = this | ||
return function asyncValidate (next) { | ||
self._log('dispense() - reusing obj', 'verbose') | ||
var objWithTimeout = self._availableObjects.shift() | ||
self._asyncTestObjects.push(objWithTimeout) | ||
self._factory.validateAsync(objWithTimeout.obj, function (valid) { | ||
var pos = self._asyncTestObjects.indexOf(objWithTimeout) | ||
self._asyncTestObjects.splice(pos, 1) | ||
if (!valid) { | ||
self.destroy(objWithTimeout.obj) | ||
return next() | ||
} | ||
this._availableObjects.shift() | ||
this._inUseObjects.push(objWithTimeout.obj) | ||
clientCb = this._waitingClients.dequeue() | ||
return clientCb(err, objWithTimeout.obj) | ||
} | ||
if (this._count < this._factory.max) { | ||
this._createResource() | ||
} | ||
if (self._waitingClients.size() < 1) { | ||
// there is no longer anyone waiting for a resource | ||
self._addResourceToAvailableObjects(objWithTimeout.obj) | ||
return | ||
} | ||
self._inUseObjects.push(objWithTimeout.obj) | ||
var clientCb = self._waitingClients.dequeue() | ||
clientCb(null, objWithTimeout.obj) | ||
}) | ||
} | ||
@@ -353,3 +374,3 @@ } | ||
} else { | ||
self.release(obj) | ||
self._addResourceToAvailableObjects(obj) | ||
} | ||
@@ -360,2 +381,18 @@ } | ||
Pool.prototype._addResourceToAvailableObjects = function (obj) { | ||
var objWithTimeout = { | ||
obj: obj, | ||
timeout: (new Date().getTime() + this._factory.idleTimeoutMillis) | ||
} | ||
if (this._factory.returnToHead) { | ||
this._availableObjects.splice(0, 0, objWithTimeout) | ||
} else { | ||
this._availableObjects.push(objWithTimeout) | ||
} | ||
this._dispense() | ||
this._scheduleRemoveIdle() | ||
} | ||
/** | ||
@@ -431,11 +468,3 @@ * @private | ||
this._inUseObjects.splice(index, 1) | ||
var objWithTimeout = { obj: obj, timeout: (new Date().getTime() + this._factory.idleTimeoutMillis) } | ||
if (this._factory.returnToHead) { | ||
this._availableObjects.splice(0, 0, objWithTimeout) | ||
} else { | ||
this._availableObjects.push(objWithTimeout) | ||
} | ||
this._log('timeout: ' + objWithTimeout.timeout, 'verbose') | ||
this._dispense() | ||
this._scheduleRemoveIdle() | ||
this._addResourceToAvailableObjects(obj) | ||
} | ||
@@ -476,7 +505,13 @@ | ||
// wait until all client requests have been satisfied. | ||
setTimeout(check, 100) | ||
} else if (self._availableObjects.length !== self._count) { | ||
// wait until all objects have been released. | ||
setTimeout(check, 100) | ||
} else if (callback) { | ||
return setTimeout(check, 100) | ||
} | ||
if (self._asyncTestObjects.length > 0) { | ||
// wait until any async tests have finished | ||
return setTimeout(check, 100) | ||
} | ||
if (self._availableObjects.length !== self._count) { | ||
// wait until in use object have been released. | ||
return setTimeout(check, 100) | ||
} | ||
if (callback) { | ||
invoke(callback) | ||
@@ -483,0 +518,0 @@ } |
{ | ||
"name": "generic-pool", | ||
"description": "Generic resource pooling for Node.JS", | ||
"version": "2.5.0", | ||
"version": "2.5.1", | ||
"author": "James Cooper <james@bitmechanic.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -248,3 +248,2 @@ var tap = require('tap') | ||
pool.acquire(function (err, client) { | ||
console.log(err) | ||
t.ok(err instanceof Error) | ||
@@ -251,0 +250,0 @@ t.ok(client === null) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
62663
11
1368
0