pax_global_header00006660000000000000000000000064130106036210014502gustar00rootroot0000000000000052 comment=a7c0140dccabdac3aceb380507d101ecdc09d84a node-pool-3.1.1/000077500000000000000000000000001301060362100134005ustar00rootroot00000000000000node-pool-3.1.1/.eslintrc.js000066400000000000000000000001321301060362100156330ustar00rootroot00000000000000module.exports = { "extends": "standard", "plugins": [ "standard" ] };node-pool-3.1.1/.gitignore000066400000000000000000000000701301060362100153650ustar00rootroot00000000000000fabfile.pyc node-pool.iml node-pool.tmproj node_modules node-pool-3.1.1/.travis.yml000066400000000000000000000002451301060362100155120ustar00rootroot00000000000000language: node_js node_js: - "4" - "5" - "6" - "7" install: - make install script: - make lint - make test sudo: false matrix: fast_finish: true node-pool-3.1.1/CHANGELOG.md000066400000000000000000000162261301060362100152200ustar00rootroot00000000000000# Change Log ## [3.1.0] - November 6 2016 - Inject dependencies into Pool to allow easier user extension ## [3.0.1] - November 1 2016 - Passthrough Pool's promise impl to deferreds so they are used internally and exposed correctly on pool.acquire (@eide) ## [3.0.0] - October 30 2016 - This is pretty big and the migration guide in the README has more detailed set of changes! - switch support to nodejs v4 and above - change external interfaces to use promises instead of callbacks - remove logging - decouple create/destroy operations from acquire/release operations - pool should now be created via `poolCreate` factory method instead of constructor. - huge internal rewrite and flow control changes - Pool is now an eventEmitter ## [2.4.3] - October 15 2016 - Use domain.bind to preserve domain context (@LewisJEllis) ## [2.4.2] - March 26 2016 - Travis now runs and fails lint checks (@kevinburke) - fixed bug #128 where using async validation incorrectly tracked resource state (@johnjdooley and @robfyfe) - fixed broken readme example that had aged badly ## [2.4.1] - February 20 2016 - Documented previously created/fixed bug #122 (thanks @jasonrhodes) - Improved Makefile and test runner docs thanks (@kevinburke) - fixed bug documented in #121 where pool could make incorrect decisions about which resources were eligible for removal. (thanks @mikemorris) ## [2.4.0] - January 18 2016 - Merged #118 - closes #110 - optional eslinting for test and lib using "standard" ruleset - Merged #114 - closes #113 - "classes" now used internally instead of object literals and exports support being called as a constructor (along with old factory behaviour) (contributed by @felixfbecker) - Move history from README.md to CHANGELOG.md and reformat - Closes #122 - fixes trapped connection bug when destroying a connection while others are in use ## [2.3.1] - January 7 2016 - Documentation fixes and widened number of nodejs versions tested on travis ## [2.3.0] - January 1 2016 - Merged #105 - allow asynchronous validate functions (contributed by @felipou) ## [2.2.2] - December 13 2015 - Merged #106 - fix condition where non "resource pool" created objects could be returned to the pool. (contributed by @devzer01) ## [2.2.1] - October 30 2015 - Merged #104 - fix #103 - condition where pool can create > specified max number of connections (contributed by @devzer01) ## [2.2.0] - March 26 2015 - Merged #92 - add getMaxPoolSize function (contributed by platypusMaximus) ## [2.1.1] - July 5 2015 - fix README error about priority queueing (spotted by @kmdm) ## [2.1.0] - June 19 2014 - Merged #72 - Add optional returnToHead flag, if true, resources are returned to head of queue (stack like behaviour) upon release (contributed by calibr), also see #68 for further discussion. ## [2.0.4] - July 27 2013 - Merged #64 - Fix for not removing idle objects (contributed by PiotrWpl) ## [2.0.3] - January 16 2013 - Merged #56/#57 - Add optional refreshIdle flag. If false, idle resources at the pool minimum will not be destroyed/re-created. (contributed by wshaver) - Merged #54 - Factory can be asked to validate pooled objects (contributed by tikonen) ## [2.0.2] - October 22 2012 - Fix #51, #48 - createResource() should check for null clientCb in err case (contributed by pooyasencha) - Merged #52 - fix bug of infinite wait when create object aync error (contributed by windyrobin) - Merged #53 - change the position of dispense and callback to ensure the time order (contributed by windyrobin) ## [2.0.1] - August 29 2012 - Fix #44 - leak of 'err' and 'obj' in createResource() - Add devDependencies block to package.json - Add travis-ci.org integration ## [2.0.0] - July 31 2012 - Non-backwards compatible change: remove adjustCallback - acquire() callback must accept two params: (err, obj) - Add optional 'min' param to factory object that specifies minimum number of resources to keep in pool - Merged #38 (package.json/Makefile changes - contributed by strk) ## [1.0.12] - June 27 2012 - Merged #37 (Clear remove idle timer after destroyAllNow - contributed by dougwilson) ## [1.0.11] - June 17 2012 - Merged #36 ("pooled" method to perform function decoration for pooled methods - contributed by cosbynator) ## [1.0.10] - May 3 2012 - Merged #35 (Remove client from availbleObjects on destroy(client) - contributed by blax) ## [1.0.9] - Dec 18 2011 - Merged #25 (add getName() - contributed by BryanDonovan) - Merged #27 (remove sys import - contributed by botker) - Merged #26 (log levels - contributed by JoeZ99) ## [1.0.8] - Nov 16 2011 - Merged #21 (add getter methods to see pool size, etc. - contributed by BryanDonovan) ## [1.0.7] - Oct 17 2011 - Merged #19 (prevent release on the same obj twice - contributed by tkrynski) - Merged #20 (acquire() returns boolean indicating whether pool is full - contributed by tilgovi) ## [1.0.6] - May 23 2011 - Merged #13 (support error variable in acquire callback - contributed by tmcw) - Note: This change is backwards compatible. But new code should use the two parameter callback format in pool.create() functions from now on. - Merged #15 (variable scope issue in dispense() - contributed by eevans) ## [1.0.5] - Apr 20 2011 - Merged #12 (ability to drain pool - contributed by gdusbabek) ## [1.0.4] - Jan 25 2011 - Fixed #6 (objects reaped with undefined timeouts) - Fixed #7 (objectTimeout issue) ## [1.0.3] - Dec 9 2010 - Added priority queueing (thanks to sylvinus) - Contributions from Poetro - Name changes to match conventions described here: http://en.wikipedia.org/wiki/Object_pool_pattern - borrow() renamed to acquire() - returnToPool() renamed to release() - destroy() removed from public interface - added JsDoc comments - Priority queueing enhancements ## [1.0.2] - Nov 9 2010 - First NPM release ======= [unreleased]: https://github.com/coopernurse/node-pool/compare/v3.1.0...HEAD [3.1.0]: https://github.com/coopernurse/node-pool/compare/v3.0.1...v3.1.0 [3.0.1]: https://github.com/coopernurse/node-pool/compare/v3.0.0...v3.0.1 [3.0.0]: https://github.com/coopernurse/node-pool/compare/v2.4.3...v3.0.0 [2.4.3]: https://github.com/coopernurse/node-pool/compare/v2.4.2...v2.4.3 [2.4.2]: https://github.com/coopernurse/node-pool/compare/v2.4.1...v2.4.2 [2.4.1]: https://github.com/coopernurse/node-pool/compare/v2.4.0...v2.4.1 [2.4.0]: https://github.com/coopernurse/node-pool/compare/v2.3.1...v2.4.0 [2.3.1]: https://github.com/coopernurse/node-pool/compare/v2.3.0...v2.3.1 [2.3.0]: https://github.com/coopernurse/node-pool/compare/v2.2.2...v2.3.0 [2.2.2]: https://github.com/coopernurse/node-pool/compare/v2.2.1...v2.2.2 [2.2.1]: https://github.com/coopernurse/node-pool/compare/v2.2.0...v2.2.1 [2.2.0]: https://github.com/coopernurse/node-pool/compare/v2.1.1...v2.2.0 [2.1.1]: https://github.com/coopernurse/node-pool/compare/v2.1.0...v2.1.1 [2.1.0]: https://github.com/coopernurse/node-pool/compare/v2.0.4...v2.1.0 [2.0.4]: https://github.com/coopernurse/node-pool/compare/v2.0.3...v2.0.4 [2.0.3]: https://github.com/coopernurse/node-pool/compare/v2.0.2...v2.0.3 [2.0.2]: https://github.com/coopernurse/node-pool/compare/v2.0.1...v2.0.2 [2.0.1]: https://github.com/coopernurse/node-pool/compare/v2.0.0...v2.0.1 [2.0.0]: https://github.com/coopernurse/node-pool/compare/v1.0.12...v2.0.0 node-pool-3.1.1/Makefile000066400000000000000000000002341301060362100150370ustar00rootroot00000000000000.PHONY: all clean install check test lint all: clean: rm -rf node_modules install: npm install check: npm test test: npm test lint: npm run lint node-pool-3.1.1/README.md000066400000000000000000000345501301060362100146660ustar00rootroot00000000000000[![build status](https://secure.travis-ci.org/coopernurse/node-pool.png)](http://travis-ci.org/coopernurse/node-pool) # Generic Pool ## About Generic resource pool with Promise based API. Can be used to reuse or throttle usage of expensive resources such as database connections. **V3 upgrade warning** Version 3 contains many breaking changes. The differences are mostly minor and I hope easy to accomodate. There is a very rough and basic [upgrade guide](https://gist.github.com/sandfox/5ca20648b60a0cb959638c0cd6fcd02d) I've written, improvements and other attempts most welcome. If you are after the older version 2 of this library you should look at the [current github branch](https://github.com/coopernurse/node-pool/tree/v2.4) for it. ## History The history has been moved to the [CHANGELOG](CHANGELOG.md) ## Installation ```sh $ npm install generic-pool [--save] ``` ## Example Here is an example using a fictional generic database driver that doesn't implement any pooling whatsoever itself. ```js var genericPool = require('generic-pool'); var DbDriver = require('some-db-driver'); /** * Step 1 - Create pool using a factory object */ const factory = { create: function(){ return new Promise(function(resolve, reject{ var client = DbDriver.createClient() client.on('connected', function(){ resolve(client) }) }) } destroy: function(client){ return new Promise(function(resolve){ client.on('end', function(){ resolve() }) client.disconnect() }) } } var opts = { max: 10, // maximum size of the pool min: 2 // minimum size of the pool } var myPool = genericPool.createPool(factory, opts) /** * Step 2 - Use pool in your code to acquire/release resources */ // acquire connection - Promise is resolved // once a resource becomes available const resourcePromise = myPool.acquire() resourcePromise.then(function(client) { client.query("select * from foo", [], function() { // return object back to pool pool.release(client); }); }) .catch(function(err){ // handle error - this is generally a timeout or maxWaitingClients // error }); /** * Step 3 - Drain pool during shutdown (optional) */ // Only call this once in your application -- at the point you want // to shutdown and stop using this pool. pool.drain(function() { pool.clear(); }); ``` ## Documentation ### Creating a pool Whilst it is possible to directly instantiate the Pool class directly, it is recommended to use the `createPool` function exported by module as the constructor method signature may change in the future. ### createPool The createPool function takes two arguments: - `factory` : an object containing functions to create/destroy/test resources for the `Pool` - `opts` : an optional object/dictonary to allow configuring/altering behaviour the of the `Pool` ```js const genericPool = require('generic-pool') const pool = genericPool.createPool(factory, opts) ``` **factory** Can be any object/instance but must have the following properties: - `create` : a function that the pool will call when it wants a new resource. It should return a Promise that either resolves to a `resource` or rejects to an `Error` if it is unable to create a resourse for whatever. - `destroy`: a function that the pool will call when it wants to destroy a resource. It should accept one argument `resource` where `resource` is whatever `factory.create` made. The `destroy` function should return a `Promise` that resolves once it has destroyed the resource. optionally it can also have the following property: - `validate`: a function that the pool will call if it wants to validate a resource. It should accept one argument `resource` where `resource` is whatever `factory.create` made. Should return a `Promise` that resolves a `boolean` where `true` indicates the resource is still valid or `false` if the resource is invalid. _Note: The values returned from `create`, `destroy`, and `validate` are all wrapped in a `Promise.resolve` by the pool before being used internally._ **opts** An optional object/dictionary with the any of the following properties: - `max`: maximum number of resources to create at any given time. (default=1) - `min`: minimum number of resources to keep in pool at any given time. If this is set >= max, the pool will silently set the min to equal `max`. (default=0) - `maxWaitingClients`: maximum number of queued requests allowed, additional `acquire` calls will be callback with an `err` in a future cycle of the event loop. - `testOnBorrow`: `boolean`: should the pool validate resources before giving them to clients. Requires that either `factory.validate` or `factory.validateAsync` to be specified - `idleTimeoutMillis`: max milliseconds a resource can stay unused in the pool without being borrowed before it should be destroyed (default 30000) - `reapIntervalMillis`: interval to check for idle resources (default 1000). (remove me!) - `acquireTimeoutMillis`: max milliseconds an `acquire` call will wait for a resource before timing out. (default no limit), if supplied should non-zero positive integer. - `fifo` : if true the oldest resources will be first to be allocated. If false the most recently released resources will be the first to be allocated. This in effect turns the pool's behaviour from a queue into a stack. `boolean`, (default true) - `priorityRange`: int between 1 and x - if set, borrowers can specify their relative priority in the queue if no resources are available. see example. (default 1) - `autostart`: boolean, should the pool start creating resources etc once the constructor is called, (default true) - `evictionRunIntervalMillis`: How often to run eviction checks. Default: 0 (does not run). - `numTestsPerRun`: Number of resources to check each eviction run. Default: 3. - `softIdleTimeoutMillis`: amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any), with the extra condition that at least "min idle" object instances remain in the pool. Default -1 (nothing can get evicted) - `idleTimeoutMillis`: the minimum amount of time that an object may sit idle in the pool before it is eligible for eviction due to idle time. Supercedes `softIdleTimeoutMillis` Default: 30000 - `Promise`: Promise lib, a Promises/A+ implementation that the pool should use. Defaults to whatever `global.Promise` is (usually native promises). ### pool.acquire ```js const onfulfilled = function(resource){ resource.doStuff() // release/destroy/etc } pool.acquire().then(onfulfilled) //or const priority = 2 pool.acquire(priority).then(onfulfilled) ``` This function is for when you want to "borrow" a resource from the pool. `acquire` takes one optional argument: - `priority`: optional, number, see **Priority Queueing** below. and returns a `Promise` Once a resource in the pool is available, the promise will be resolved with a `resource` (whatever `factory.create` makes for you). If the Pool is unable to give a resource (e.g timeout) then the promise will be rejected with an `Error` ### pool.release ```js pool.release(resource) ``` This function is for when you want to return a resource to the pool. `release` takes one required argument: - `resource`: a previously borrowed resource and returns a `Promise`. This promise will resolve once the `resource` is accepted by the pool, or reject if the pool is unable to accept the `resource` for any reason (e.g `resource` is not a resource or object that came from the pool). If you do not care the outcome it is safe to ignore this promise. ### pool.destroy This function is for when you want to return a resource to the pool but want it destroyed rather than being made available to other resources. E.g you may know the resource has timed out or crashed. `destroy` takes one required argument: - `resource`: a previously borrowed resource and returns a `Promise`. This promise will resolve once the `resource` is accepted by the pool, or reject if the pool is unable to accept the `resource` for any reason (e.g `resource` is not a resource or object that came from the pool). If you do not care the outcome it is safe to ignore this promise. ### pool.on The pool is an event emitter. Below are the events it emits and any args for those events - `factoryCreateError` : emitted when a promise returned by `factory.create` is rejected. If this event has no listeners then the `error` will be silently discarded - `error`: whatever `reason` the promise was rejected with. - `factoryDestroyError` : emitted when a promise returned by `factory.destroy` is rejected. If this event has no listeners then the `error` will be silently discarded - `error`: whatever `reason` the promise was rejected with. ## Idle Object Eviction The pool has an evictor (off by default) which will inspect idle items in the pool and `destroy` them if they are too old. By default the evictor does not run, to enable it you must set the `evictionRunIntervalMillis` option to a non-zero value. Once enable the evictor will check at most `numTestsPerEvictionRun` each time, this is to stop it blocking your application if you have lots of resources in the pool. ## Draining If you are shutting down a long-lived process, you may notice that node fails to exit for 30 seconds or so. This is a side effect of the idleTimeoutMillis behavior -- the pool has a setTimeout() call registered that is in the event loop queue, so node won't terminate until all resources have timed out, and the pool stops trying to manage them. This behavior will be more problematic when you set factory.min > 0, as the pool will never become empty, and the setTimeout calls will never end. In these cases, use the pool.drain() function. This sets the pool into a "draining" state which will gracefully wait until all idle resources have timed out. For example, you can call: If you do this, your node process will exit gracefully. ## Priority Queueing The pool supports optional priority queueing. This becomes relevant when no resources are available and the caller has to wait. `acquire()` accepts an optional priority int which specifies the caller's relative position in the queue. Each priority slot has it's own internal queue created for it. When a resource is available for borrowing, the first request in the highest priority queue will be given it. Specifying a `priority` to `acquire` that is outside the `priorityRange` set at `Pool` creation time will result in the `priority` being converted the lowest possible `priority` ```js // create pool with priorityRange of 3 // borrowers can specify a priority 0 to 2 var opts = { priorityRange : 3 } var pool = genericPool.createPool(someFactory,opts); // acquire connection - no priority specified - will go onto lowest priority queue pool.acquire().thenfunction(client) { pool.release(client); }); // acquire connection - high priority - will go into highest priority queue pool.acquire(0).then(function(client) { pool.release(client); }); // acquire connection - medium priority - will go into 'mid' priority queue pool.acquire(1).then(function(client) { pool.release(client); }); // etc.. ``` ## Draining If you are shutting down a long-lived process, you may notice that node fails to exit for 30 seconds or so. This is a side effect of the idleTimeoutMillis behavior -- the pool has a setTimeout() call registered that is in the event loop queue, so node won't terminate until all resources have timed out, and the pool stops trying to manage them. This behavior will be more problematic when you set factory.min > 0, as the pool will never become empty, and the setTimeout calls will never end. In these cases, use the pool.drain() function. This sets the pool into a "draining" state which will gracefully wait until all idle resources have timed out. For example, you can call: If you do this, your node process will exit gracefully. If you know you would like to terminate all the available resources in your pool before any timeouts they might have are reached, you can use `clear()` in conjunction with `drain()`: ```js const p = pool.drain() .then(function() { return pool.clear(); }); ``` The `promise` returned will resolve once all waiting clients have acquired and return resources, and any available resources have been destroyed One side-effect of calling `drain()` is that subsequent calls to `acquire()` will throw an Error. ## Pooled function decoration This has now been extracted out it's own module [generic-pool-decorator](https://github.com/sandfox/generic-pool-decorator) ## Pool info The following properties will let you get information about the pool: ```js // returns number of resources in the pool regardless of // whether they are free or in use pool.size // returns number of unused resources in the pool pool.available // returns number of callers waiting to acquire a resource pool.pending // returns number of maxixmum number of resources allowed by ppol pool.max // returns number of minimum number of resources allowed by ppol pool.min ``` ## Run Tests $ npm install $ npm test The tests are run/written using Tap. Most are ports from the old espresso tests and are not in great condition. Most cases are inside `test/generic-pool-test.js` with newer cases in their own files (legacy reasons). ## Linting We use eslint and the `standard` ruleset. ## License (The MIT License) Copyright (c) 2010-2016 James Cooper <james@bitmechanic.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-pool-3.1.1/index.js000066400000000000000000000006431301060362100150500ustar00rootroot00000000000000const Pool = require('./lib/Pool') const Deque = require('./lib/Deque') const PriorityQueue = require('./lib/PriorityQueue') const DefaultEvictor = require('./lib/DefaultEvictor') module.exports = { Pool: Pool, Deque: Deque, PriorityQueue: PriorityQueue, DefaultEvictor: DefaultEvictor, createPool: function(factory, config){ return new Pool(DefaultEvictor, Deque, PriorityQueue, factory, config) } } node-pool-3.1.1/lib/000077500000000000000000000000001301060362100141465ustar00rootroot00000000000000node-pool-3.1.1/lib/DefaultEvictor.js000066400000000000000000000006201301060362100174220ustar00rootroot00000000000000'use strict' class DefaultEvictor { evict (config, pooledResource, availableObjectsCount) { const idleTime = Date.now() - pooledResource.lastIdleTime if (config.softIdleTimeoutMillis < idleTime && config.min < availableObjectsCount) { return true } if (config.idleTimeoutMillis < idleTime) { return true } return false } } module.exports = DefaultEvictor node-pool-3.1.1/lib/Deferred.js000066400000000000000000000016531301060362100162310ustar00rootroot00000000000000'use strict' /** * This is apparently a bit like a Jquery deferred, hence the name */ class Deferred { constructor (Promise) { this._state = Deferred.PENDING this._resolve = undefined this._reject = undefined this._promise = new Promise((resolve, reject) => { this._resolve = resolve this._reject = reject }) } get state () { return this._state } get promise () { return this._promise } reject (reason) { if (this._state !== Deferred.PENDING) { return } this._state = Deferred.REJECTED this._reject(reason) } resolve (value) { if (this._state !== Deferred.PENDING) { return } this._state = Deferred.FULFILLED this._resolve(value) } } // TODO: should these really live here? or be a seperate 'state' enum Deferred.PENDING = 'PENDING' Deferred.FULFILLED = 'FULFILLED' Deferred.REJECTED = 'REJECTED' module.exports = Deferred node-pool-3.1.1/lib/Deque.js000066400000000000000000000040431301060362100155500ustar00rootroot00000000000000'use strict' const DoublyLinkedList = require('./DoublyLinkedList') const DequeIterator = require('./DequeIterator') /** * DoublyLinkedList backed double ended queue * implements just enough to keep the Pool */ class Deque { constructor () { this._list = new DoublyLinkedList() } /** * removes and returns the first element from the queue * @return {[type]} [description] */ shift () { if (this._length === 0) { return undefined } const node = this._list.head this._list.remove(node) return node.data } /** * adds one elemts to the beginning of the queue * @param {[type]} element [description] * @return {[type]} [description] */ unshift (element) { const node = DoublyLinkedList.createNode(element) this._list.insertBeginning(node) } /** * adds one to the end of the queue * @param {[type]} element [description] * @return {[type]} [description] */ push (element) { const node = DoublyLinkedList.createNode(element) this._list.insertEnd(node) } /** * removes and returns the last element from the queue */ pop () { if (this._length === 0) { return undefined } const node = this._list.tail this._list.remove(node) return node.data } [Symbol.iterator] () { return new DequeIterator(this._list) } iterator () { return new DequeIterator(this._list) } reverseIterator () { return new DequeIterator(this._list, true) } /** * get a reference to the item at the head of the queue * @return {element} [description] */ get head () { if (this._list.length === 0) { return undefined } const node = this._list.head return node.data } /** * get a reference to the item at the tail of the queue * @return {element} [description] */ get tail () { if (this._list.length === 0) { return undefined } const node = this._list.tail return node.data } get length () { return this._list.length } } module.exports = Deque node-pool-3.1.1/lib/DequeIterator.js000066400000000000000000000006131301060362100172610ustar00rootroot00000000000000'use strict' const DoublyLinkedListIterator = require('./DoublyLinkedListIterator') /** * Thin wrapper around an underlying DDL iterator */ class DequeIterator extends DoublyLinkedListIterator { next () { const result = super.next() // unwrap the node... if (result.value) { result.value = result.value.data } return result } } module.exports = DequeIterator node-pool-3.1.1/lib/DoublyLinkedList.js000066400000000000000000000035541301060362100177340ustar00rootroot00000000000000'use strict' /** * A Doubly Linked List, because there aren't enough in the world... * this is pretty much a direct JS port of the one wikipedia * https://en.wikipedia.org/wiki/Doubly_linked_list * * For most usage 'insertBeginning' and 'insertEnd' should be enough * * nodes are expected to something like a POJSO like * { * prev: null, * next: null, * something: 'whatever you like' * } */ class DoublyLinkedList { constructor () { this.head = null this.tail = null this.length = 0 } insertBeginning (node) { if (this.head === null) { this.head = node this.tail = node node.prev = null node.next = null this.length++ } else { this.insertBefore(this.head, node) } } insertEnd (node) { if (this.tail === null) { this.insertBeginning(node) } else { this.insertAfter(this.tail, node) } } insertAfter (node, newNode) { newNode.prev = node newNode.next = node.next if (node.next === null) { this.tail = newNode } else { node.next.prev = newNode } node.next = newNode this.length++ } insertBefore (node, newNode) { newNode.prev = node.prev newNode.next = node if (node.prev === null) { this.head = newNode } else { node.prev.next = newNode } node.prev = newNode this.length++ } remove (node) { if (node.prev === null) { this.head = node.next } else { node.prev.next = node.next } if (node.next === null) { this.tail = node.prev } else { node.next.prev = node.prev } node.prev = null node.next = null this.length-- } // FIXME: this should not live here and has become a dumping ground... static createNode (data) { return { prev: null, next: null, data: data } } } module.exports = DoublyLinkedList node-pool-3.1.1/lib/DoublyLinkedListIterator.js000066400000000000000000000057171301060362100214510ustar00rootroot00000000000000'use strict' /** * Creates an interator for a DoublyLinkedList starting at the given node * It's internal cursor will remains relative to the last "iterated" node as that * node moves through the list until it either iterates to the end of the list, * or the the node it's tracking is removed from the list. Until the first 'next' * call it tracks the head/tail of the linked list. This means that one can create * an iterator on an empty list, then add nodes, and then the iterator will follow * those nodes. Because the DoublyLinkedList nodes don't track their owning "list" and * it's highly inefficient to walk the list for every iteration, the iterator won't know * if the node has been detached from one List and added to another list, or if the iterator * * The created object is an es6 compatible iterator */ class DoublyLinkedListIterator { /** * @param {Object} doublyLinkedListNode a node that is part of a doublyLinkedList * @param {Boolean} reverse is this a reverse iterator? default: false */ constructor (doublyLinkedList, reverse) { this._list = doublyLinkedList // NOTE: these key names are tied to the DoublyLinkedListIterator this._direction = reverse === true ? 'prev' : 'next' this._startPosition = reverse === true ? 'tail' : 'head' this._started = false this._cursor = null this._done = false } _start () { this._cursor = this._list[this._startPosition] this._started = true } _advanceCursor () { if (this._started === false) { this._started = true this._cursor = this._list[this._startPosition] return } this._cursor = this._cursor[this._direction] } reset () { this._done = false this._started = false this._cursor = null } remove () { if (this._started === false || this._done === true || this._isCursorDetached()) { return false } this._list.remove(this._cursor) } next () { if (this._done === true) { return { done: true } } this._advanceCursor() // if there is no node at the cursor or the node at the cursor is no longer part of // a doubly linked list then we are done/finished/kaput if (this._cursor === null || this._isCursorDetached()) { this._done = true return { done: true } } return { value: this._cursor, done: false } } /** * Is the node detached from a list? * NOTE: you can trick/bypass/confuse this check by removing a node from one DoublyLinkedList * and adding it to another. * TODO: We can make this smarter by checking the direction of travel and only checking * the required next/prev/head/tail rather than all of them * @param {[type]} node [description] * @return {Boolean} [description] */ _isCursorDetached () { return this._cursor.prev === null && this._cursor.next === null && this._list.tail !== this._cursor && this._list.head !== this._cursor } } module.exports = DoublyLinkedListIterator node-pool-3.1.1/lib/Pool.js000066400000000000000000000425251301060362100154250ustar00rootroot00000000000000'use strict' const EventEmitter = require('events').EventEmitter const factoryValidator = require('./factoryValidator') const PoolOptions = require('./PoolOptions') const ResourceRequest = require('./ResourceRequest') const ResourceLoan = require('./ResourceLoan') const PooledResource = require('./PooledResource') /** * TODO: move me */ const FACTORY_CREATE_ERROR = 'factoryCreateError' const FACTORY_DESTROY_ERROR = 'factoryDestroyError' class Pool extends EventEmitter { /** * Generate an Object pool with a specified `factory` and `config`. * * @param {Object} factory * Factory to be used for generating and destroying the items. * @param {Function} factory.create * Should create the item to be acquired, * and call it's first callback argument with the generated item as it's argument. * @param {Function} factory.destroy * Should gently close any resources that the item is using. * Called before the items is destroyed. * @param {Function} factory.validate * Test if a resource is still valid .Should return a promise that resolves to a boolean, true if resource is still valid and false * If it should be removed from pool. */ constructor (Evictor, Deque, PriorityQueue, factory, options) { super() factoryValidator(factory) this._config = new PoolOptions(options) // TODO: fix up this ugly glue-ing this._Promise = this._config.Promise this._factory = factory this._draining = false this._started = false /** * Holds waiting clients * @type {PriorityQueue} */ this._waitingClientsQueue = new PriorityQueue(this._config.priorityRange) /** * Collection of promises for resource creation calls made by the pool to factory.create * @type {Set} */ this._factoryCreateOperations = new Set() /** * Collection of promises for resource destruction calls made by the pool to factory.destroy * @type {Set} */ this._factoryDestroyOperations = new Set() /** * A queue/stack of pooledResources awaiting acquisition * TODO: replace with LinkedList backed array * @type {Array} */ this._availableObjects = new Deque() /** * Collection of references for any resource that are undergoing validation before being acquired * @type {Set} */ this._testOnBorrowResources = new Set() /** * Collection of references for any resource that are undergoing validation before being returned * @type {Set} */ this._testOnReturnResources = new Set() /** * Collection of promises for any validations currently in process * @type {Set} */ this._validationOperations = new Set() /** * All objects associated with this pool in any state (except destroyed) * @type {PooledResourceCollection} */ this._allObjects = new Set() /** * Loans keyed by the borrowed resource * @type {Map} */ this._resourceLoans = new Map() /** * Infinitely looping iterator over available object * @type {DLLArrayIterator} */ this._evictionIterator = this._availableObjects.iterator() this._evictor = new Evictor() /** * handle for setTimeout for next eviction run * @type {[type]} */ this._scheduledEviction = null // create initial resources (if factory.min > 0) if (this._config.autostart === true) { this.start() } } _destroy (pooledResource) { // FIXME: do we need another state for "in destruction"? pooledResource.invalidate() this._allObjects.delete(pooledResource) // NOTE: this maybe very bad promise usage? const destroyPromise = this._factory.destroy(pooledResource.obj) const wrappedDestroyPromise = this._Promise.resolve(destroyPromise) this._factoryDestroyOperations.add(wrappedDestroyPromise) wrappedDestroyPromise .catch((reason) => { this.emit(FACTORY_DESTROY_ERROR, reason) }) .then(() => { this._factoryDestroyOperations.delete(wrappedDestroyPromise) }) // TODO: maybe ensuring minimum pool size should live outside here this._ensureMinimum() } /** * Attempt to move an available resource into test and then onto a waiting client * @return {Boolean} could we move an available resource into test */ _testOnBorrow () { if (this._availableObjects.length < 1) { return false } const pooledResource = this._availableObjects.shift() // Mark the resource as in test pooledResource.test() this._testOnBorrowResources.add(pooledResource) const validationPromise = this._factory.validate(pooledResource.obj) const wrappedValidationPromise = this._Promise.resolve(validationPromise) this._validationOperations.add(wrappedValidationPromise) wrappedValidationPromise.then((isValid) => { this._validationOperations.delete(wrappedValidationPromise) this._testOnBorrowResources.delete(pooledResource) if (isValid === false) { pooledResource.invalidate() this._destroy(pooledResource) this._dispense() return } this._dispatchPooledResourceToNextWaitingClient(pooledResource) }) return true } /** * Attempt to move an available resource to a waiting client * @return {Boolean} [description] */ _dispatchResource () { if (this._availableObjects.length < 1) { return false } const pooledResource = this._availableObjects.shift() this._dispatchPooledResourceToNextWaitingClient(pooledResource) return } /** * Attempt to resolve an outstanding resource request using an available resource from * the pool, or creating new ones * * @private */ _dispense () { /** * Local variables for ease of reading/writing * these don't (shouldn't) change across the execution of this fn */ const numWaitingClients = this._waitingClientsQueue.length // If there aren't any waiting requests then there is nothing to do // so lets short-circuit if (numWaitingClients < 1) { return } // If we are doing test-on-borrow see how many more resources need to be moved into test // to help satisfy waitingClients if (this._config.testOnBorrow === true) { // how many available resources do we need to shift into test const desiredNumberOfResourcesToMoveIntoTest = numWaitingClients - this._testOnBorrowResources.size const actualNumberOfResourcesToMoveIntoTest = Math.min(this._availableObjects.length, desiredNumberOfResourcesToMoveIntoTest) for (let i = 0; actualNumberOfResourcesToMoveIntoTest > i; i++) { this._testOnBorrow() } } // Do we still have outstanding requests that can't be fulfilled by resources in-test and available const potentiallyAllocableResources = this._availableObjects.length + this._testOnBorrowResources.size + this._testOnReturnResources.size + this._factoryCreateOperations.size const resourceShortfall = numWaitingClients - potentiallyAllocableResources const spareResourceCapacity = this._config.max - (this._allObjects.size + this._factoryCreateOperations.size) const actualNumberOfResourcesToCreate = Math.min(spareResourceCapacity, resourceShortfall) for (let i = 0; actualNumberOfResourcesToCreate > i; i++) { this._createResource() } // if we aren't testing-on-borrow then lets try to allocate what we can if (this._config.testOnBorrow === false) { const actualNumberOfResourcesToDispatch = Math.min(this._availableObjects.length, numWaitingClients) for (let i = 0; actualNumberOfResourcesToDispatch > i; i++) { this._dispatchResource() } } } /** * Dispatches a pooledResource to the next waiting client (if any) else * puts the PooledResource back on the available list * @param {[type]} pooledResource [description] * @return {[type]} [description] */ _dispatchPooledResourceToNextWaitingClient (pooledResource) { const clientResourceRequest = this._waitingClientsQueue.dequeue() if (clientResourceRequest === undefined) { // While we were away either all the waiting clients timed out // or were somehow fulfilled. put our pooledResource back. this._addPooledResourceToAvailableObjects(pooledResource) // TODO: do need to trigger anything before we leave? return false } const loan = new ResourceLoan(pooledResource, this._Promise) this._resourceLoans.set(pooledResource.obj, loan) pooledResource.allocate() clientResourceRequest.resolve(pooledResource.obj) return true } /** * @private */ _createResource () { // An attempt to create a resource const factoryPromise = this._factory.create() const wrappedFactoryPromise = this._Promise.resolve(factoryPromise) this._factoryCreateOperations.add(wrappedFactoryPromise) wrappedFactoryPromise .then((resource) => { this._factoryCreateOperations.delete(wrappedFactoryPromise) this._handleNewResource(resource) }) .catch((reason) => { this._factoryCreateOperations.delete(wrappedFactoryPromise) this.emit(FACTORY_CREATE_ERROR, reason) this._dispense() }) } _handleNewResource (resource) { const pooledResource = new PooledResource(resource) this._allObjects.add(pooledResource) // TODO: check we aren't exceding our maxPoolSize before doing this._dispatchPooledResourceToNextWaitingClient(pooledResource) } /** * @private */ _ensureMinimum () { if (this._draining === true) { return } if (this._count < this._config.min) { const diff = this._config.min - this._count for (let i = 0; i < diff; i++) { this._createResource() } } } _evict () { const testsToRun = Math.min(this._config.numTestsPerEvictionRun, this._availableObjects.length) const evictionConfig = { softIdleTimeoutMillis: this._config.softIdleTimeoutMillis, idleTimeoutMillis: this._config.idleTimeoutMillis, min: this._config.min } for (let testsHaveRun = 0; testsHaveRun < testsToRun;) { const iterationResult = this._evictionIterator.next() // Safety check incase we could get stuck in infinite loop because we // somehow emptied the array after chekcing it's length if (iterationResult.done === true && this._availableObjects.length < 1) { this._evictionIterator.reset() return } // if this happens it should just mean we reached the end of the // list and can reset the cursor. if (iterationResult.done === true && this._availableObjects.length > 0) { this._evictionIterator.reset() break } const resource = iterationResult.value const shouldEvict = this._evictor.evict(evictionConfig, resource, this._availableObjects.length) testsHaveRun++ if (shouldEvict === true) { // take it out of the _availableObjects list this._evictionIterator.remove() this._destroy(resource) } } } _scheduleEvictorRun () { // Start eviction if set if (this._config.evictionRunIntervalMillis > 0) { this._scheduledEviction = setTimeout(() => { this._evict() this._scheduleEvictorRun() }, this._config.evictionRunIntervalMillis) } } _descheduleEvictorRun () { clearTimeout(this._scheduledEviction) this._scheduledEviction = null } start () { if (this._draining === true) { return } if (this._started === true) { return } this._started = true this._scheduleEvictorRun() this._ensureMinimum() } /** * Request a new resource. The callback will be called, * when a new resource is available, passing the resource to the callback. * TODO: should we add a seperate "acquireWithPriority" function * * @param {Function} callback * Callback function to be called after the acquire is successful. * If there is an error preventing the acquisition of resource, an error will * be the first parameter, else it will be null. * The acquired resource will be the second parameter. * * @param {Number} priority * Optional. Integer between 0 and (priorityRange - 1). Specifies the priority * of the caller if there are no available resources. Lower numbers mean higher * priority. * * @returns {Promise} */ acquire (priority) { if (this._draining) { return this._Promise.reject(new Error('pool is draining and cannot accept work')) } // TODO: should we defer this check till after this event loop incase "the situation" changes in the meantime if (this._config.maxWaitingClients !== undefined && this._waitingClientsQueue.length >= this._config.maxWaitingClients) { return this._Promise.reject(new Error('max waitingClients count exceeded')) } const resourceRequest = new ResourceRequest(this._config.acquireTimeoutMillis, this._Promise) this._waitingClientsQueue.enqueue(resourceRequest, priority) this._dispense() return resourceRequest.promise } /** * Return the resource to the pool when it is no longer required. * * @param {Object} obj * The acquired object to be put back to the pool. */ release (resource) { // check for an outstanding loan const loan = this._resourceLoans.get(resource) if (loan === undefined) { return this._Promise.reject(new Error('Resource not currently part of this pool')) } this._resourceLoans.delete(resource) loan.resolve() const pooledResource = loan.pooledResource pooledResource.deallocate() this._addPooledResourceToAvailableObjects(pooledResource) this._dispense() return this._Promise.resolve() } /** * Request the resource to be destroyed. The factory's destroy handler * will also be called. * * This should be called within an acquire() block as an alternative to release(). * * @param {Object} resource * The acquired resource to be destoyed. */ destroy (resource) { // check for an outstanding loan const loan = this._resourceLoans.get(resource) if (loan === undefined) { return this._Promise.reject(new Error('Resource not currently part of this pool')) } this._resourceLoans.delete(resource) loan.resolve() const pooledResource = loan.pooledResource pooledResource.deallocate() this._destroy(pooledResource) return this._Promise.resolve() } _addPooledResourceToAvailableObjects (pooledResource) { pooledResource.idle() if (this._config.fifo === true) { this._availableObjects.push(pooledResource) } else { this._availableObjects.unshift(pooledResource) } } /** * Disallow any new acquire calls and let the request backlog dissapate. * Resolves once all resources are returned to pool and available... * @returns {Promise} */ drain () { // disable the ability to put more work on the queue. this._draining = true return this.__allResourceRequestsSettled() .then(() => { return this.__allResourcesReturned() }) } __allResourceRequestsSettled () { if (this._waitingClientsQueue.length > 0) { // wait for last waiting client to be settled // FIXME: what if they can "resolve" out of order....? return this._waitingClientsQueue.tail.promise.then(() => {}, () => {}) } return this._Promise.resolve() } // FIXME: this is a horrific mess __allResourcesReturned () { const ps = [] this._resourceLoans.forEach(function (loan) { ps.push(loan.promise) }) return this._Promise.all(ps) } /** * Forcibly destroys all available resources regardless of timeout. Intended to be * invoked as part of a drain. Does not prevent the creation of new * resources as a result of subsequent calls to acquire. * * Note that if factory.min > 0, the pool will destroy all idle resources * in the pool, but replace them with newly created resources up to the * specified factory.min value. If this is not desired, set factory.min * to zero before calling clear() * */ clear () { this._descheduleEvictorRun() for (const resource of this._availableObjects) { this._destroy(resource) } // TODO: we should handle factory.destroy rejecting... return this._Promise.all(this._factoryDestroyOperations) } /** * The combined count of the currently created objects and those in the * process of being created * Does NOT include resources in the process of being destroyed * sort of legacy... * @return {Number} */ get _count () { return this._allObjects.size + this._factoryCreateOperations.size } /** * see _count above * @return {Number} [description] */ get size () { return this._count } /** * number of available resources * @return {Number} [description] */ get available () { return this._availableObjects.length } /** * number of resources that are currently acquired * @return {[type]} [description] */ get borrowed () { return this._resourceLoans.size } /** * number of waiting acquire calls * @return {[type]} [description] */ get pending () { return this._waitingClientsQueue.length } /** * maximum size of the pool * @return {[type]} [description] */ get max () { return this._config.max } /** * minimum size of the pool * @return {[type]} [description] */ get min () { return this._config.min } } module.exports = Pool node-pool-3.1.1/lib/PoolDefaults.js000066400000000000000000000011771301060362100171130ustar00rootroot00000000000000'use strict' /** * Create the default settings used by the pool * * @class */ class PoolDefaults { constructor () { this.fifo = true this.priorityRange = 1 this.testOnBorrow = false this.testOnReturn = false this.autostart = true this.evictionRunIntervalMillis = 0 this.numTestsPerEvictionRun = 3 this.softIdleTimeoutMillis = -1 this.idleTimeoutMillis = 30000 // FIXME: no defaults! this.acquireTimeoutMillis = null this.maxWaitingClients = null this.min = null this.max = null // FIXME: this seems odd? this.Promise = Promise } } module.exports = PoolDefaults node-pool-3.1.1/lib/PoolOptions.js000066400000000000000000000073621301060362100170010ustar00rootroot00000000000000'use strict' const PoolDefaults = require('./PoolDefaults') class PoolOptions { /** * @param {Object} config * configuration for the pool * @param {Number} config.max * Maximum number of items that can exist at the same time. Default: 1. * Any further acquire requests will be pushed to the waiting list. * @param {Number} config.min * Minimum number of items in pool (including in-use). Default: 0. * When the pool is created, or a resource destroyed, this minimum will * be checked. If the pool resource count is below the minimum, a new * resource will be created and added to the pool. * @param {Number} config.maxWaitingClients * maximum number of queued requests allowed after which acquire calls will be rejected * @param {Number} config.acquireTimeoutMillis * Delay in milliseconds after which the an `acquire` call will fail. optional. * Default: undefined. Should be positive and non-zero * @param {Number} config.priorityRange * The range from 1 to be treated as a valid priority * @param {Bool} [config.fifo=true] * Sets whether the pool has LIFO (last in, first out) behaviour with respect to idle objects. * if false then pool has FIFO behaviour * @param {Bool} [config.autostart=true] * Should the pool start creating resources etc once the constructor is called * @param {Number} opts.evictionRunIntervalMillis * How often to run eviction checks. Default: 0 (does not run). * @param {Number} opts.numTestsPerEvictionRun * Number of resources to check each eviction run. Default: 3. * @param {Number} opts.softIdleTimeoutMillis * amount of time an object may sit idle in the pool before it is eligible * for eviction by the idle object evictor (if any), with the extra condition * that at least "min idle" object instances remain in the pool. Default -1 (nothing can get evicted) * @param {Number} opts.idleTimeoutMillis * the minimum amount of time that an object may sit idle in the pool before it is eligible for eviction * due to idle time. Supercedes "softIdleTimeoutMillis" Default: 30000 * @param {Promise} [config.Promise=Promise] * What promise implementation should the pool use, defaults to native promises. */ constructor (opts) { const poolDefaults = new PoolDefaults() opts = opts || {} this.fifo = (typeof opts.fifo === 'boolean') ? opts.fifo : poolDefaults.fifo this.priorityRange = opts.priorityRange || poolDefaults.priorityRange this.testOnBorrow = (typeof opts.testOnBorrow === 'boolean') ? opts.testOnBorrow : poolDefaults.testOnBorrow this.testOnReturn = (typeof opts.testOnReturn === 'boolean') ? opts.testOnReturn : poolDefaults.testOnReturn this.autostart = (typeof opts.autostart === 'boolean') ? opts.autostart : poolDefaults.autostart if (opts.acquireTimeoutMillis) { this.acquireTimeoutMillis = parseInt(opts.acquireTimeoutMillis, 10) } if (opts.maxWaitingClients) { this.maxWaitingClients = parseInt(opts.maxWaitingClients, 10) } this.max = parseInt(opts.max, 10) this.min = parseInt(opts.min, 10) this.max = Math.max(isNaN(this.max) ? 1 : this.max, 1) this.min = Math.min(isNaN(this.min) ? 0 : this.min, this.max) this.evictionRunIntervalMillis = opts.evictionRunIntervalMillis || poolDefaults.evictionRunIntervalMillis this.numTestsPerEvictionRun = opts.numTestsPerEvictionRun || poolDefaults.numTestsPerEvictionRun this.softIdleTimeoutMillis = opts.softIdleTimeoutMillis || poolDefaults.softIdleTimeoutMillis this.idleTimeoutMillis = opts.idleTimeoutMillis || poolDefaults.idleTimeoutMillis this.Promise = (typeof opts.Promise === 'object') ? opts.Promise : poolDefaults.Promise } } module.exports = PoolOptions node-pool-3.1.1/lib/PooledResource.js000066400000000000000000000017541301060362100174450ustar00rootroot00000000000000'use strict' const PooledResourceStateEnum = require('./PooledResourceStateEnum') /** * @class * @private */ class PooledResource { constructor (resource) { this.creationTime = Date.now() this.lastReturnTime = null this.lastBorrowTime = null this.lastIdleTime = null this.obj = resource this.state = PooledResourceStateEnum.IDLE } // mark the resource as "allocated" allocate () { this.lastBorrowTime = Date.now() this.state = PooledResourceStateEnum.ALLOCATED } // mark the resource as "deallocated" deallocate () { this.lastReturnTime = Date.now() this.state = PooledResourceStateEnum.IDLE } invalidate () { this.state = PooledResourceStateEnum.INVALID } test () { this.state = PooledResourceStateEnum.VALIDATION } idle () { this.lastIdleTime = Date.now() this.state = PooledResourceStateEnum.IDLE } returning () { this.state = PooledResourceStateEnum.RETURNING } } module.exports = PooledResource node-pool-3.1.1/lib/PooledResourceStateEnum.js000066400000000000000000000005161301060362100212660ustar00rootroot00000000000000'use strict' const PooledResourceStateEnum = { ALLOCATED: 'ALLOCATED', // In use IDLE: 'IDLE', // In the queue, not in use. INVALID: 'INVALID', // Failed validation RETURNING: 'RETURNING', // Resource is in process of returning VALIDATION: 'VALIDATION' // Currently being tested } module.exports = PooledResourceStateEnum node-pool-3.1.1/lib/PriorityQueue.js000066400000000000000000000025711301060362100173370ustar00rootroot00000000000000'use strict' const Queue = require('./Queue') /** * @class * @private */ class PriorityQueue { constructor (size) { this._size = Math.max(+size | 0, 1) this._slots = [] // initialize arrays to hold queue elements for (let i = 0; i < this._size; i++) { this._slots.push(new Queue()) } } get length () { let _length = 0 for (let i = 0, slots = this._slots.length; i < slots; i++) { _length += this._slots[i].length } return _length } enqueue (obj, priority) { // Convert to integer with a default value of 0. priority = priority && +priority | 0 || 0 if (priority) { if (priority < 0 || priority >= this._size) { priority = (this._size - 1) // put obj at the end of the line } } this._slots[priority].push(obj) } dequeue () { for (let i = 0, sl = this._slots.length; i < sl; i += 1) { if (this._slots[i].length) { return this._slots[i].shift() } } return } get head () { for (let i = 0, sl = this._slots.length; i < sl; i += 1) { if (this._slots[i].length > 0) { return this._slots[i].head } } return } get tail () { for (let i = this._slots.length - 1; i >= 0; i--) { if (this._slots[i].length > 0) { return this._slots[i].tail } } return } } module.exports = PriorityQueue node-pool-3.1.1/lib/Queue.js000066400000000000000000000017631301060362100155770ustar00rootroot00000000000000'use strict' const DoublyLinkedList = require('./DoublyLinkedList') const Deque = require('./Deque') /** * Sort of a internal queue for holding the waiting * resource requets for a given "priority". * Also handles managing timeouts rejections on items (is this the best place for this?) * This is the last point where we know which queue a resourceRequest is in * */ class Queue extends Deque { /** * Adds the obj to the end of the list for this slot * we completely override the parent method because we need access to the * node for our rejection handler * @param {[type]} item [description] */ push (resourceRequest) { const node = DoublyLinkedList.createNode(resourceRequest) resourceRequest.promise.catch(this._createTimeoutRejectionHandler(node)) this._list.insertEnd(node) } _createTimeoutRejectionHandler (node) { return (reason) => { if (reason.name === 'TimeoutError') { this._list.remove(node) } } } } module.exports = Queue node-pool-3.1.1/lib/ResourceLoan.js000066400000000000000000000011201301060362100170770ustar00rootroot00000000000000'use strict' const Deferred = require('./Deferred') /** * Plan is to maybe add tracking via Error objects * and other fun stuff! */ class ResourceLoan extends Deferred { /** * * @param {PooledResource} pooledResource the PooledResource this loan belongs to * @return {[type]} [description] */ constructor (pooledResource, Promise) { super(Promise) this._creationTimestamp = Date.now() this.pooledResource = pooledResource } reject () { /** * Loans can only be resolved at the moment */ } } module.exports = ResourceLoan node-pool-3.1.1/lib/ResourceRequest.js000066400000000000000000000025641301060362100176530ustar00rootroot00000000000000'use strict' const Deferred = require('./Deferred') const errors = require('./errors') function fbind (fn, ctx) { return function bound () { return fn.apply(ctx, arguments) } } /** * Wraps a users request for a resource * Basically a promise mashed in with a timeout * @private */ class ResourceRequest extends Deferred { /** * [constructor description] * @param {Number} ttl timeout */ constructor (ttl, Promise) { super(Promise) this._creationTimestamp = Date.now() this._timeout = null if (ttl !== undefined) { this.setTimeout(ttl) } } setTimeout (delay) { if (this._state !== ResourceRequest.PENDING) { return } const ttl = parseInt(delay, 10) if (isNaN(ttl) || ttl <= 0) { throw new Error('delay must be a positive int') } const age = Date.now() - this._creationTimestamp if (this._timeout) { this.removeTimeout() } this._timeout = setTimeout(fbind(this._fireTimeout, this), Math.max(ttl - age, 0)) } removeTimeout () { clearTimeout(this._timeout) this._timeout = null } _fireTimeout () { this.reject(new errors.TimeoutError('ResourceRequest timed out')) } reject (reason) { this.removeTimeout() super.reject(reason) } resolve (value) { this.removeTimeout() super.resolve(value) } } module.exports = ResourceRequest node-pool-3.1.1/lib/errors.js000066400000000000000000000010671301060362100160240ustar00rootroot00000000000000'use strict' class ExtendableError extends Error { constructor (message) { super(message) this.name = this.constructor.name this.message = message if (typeof Error.captureStackTrace === 'function') { Error.captureStackTrace(this, this.constructor) } else { this.stack = (new Error(message)).stack } } } /* eslint-disable no-useless-constructor */ class TimeoutError extends ExtendableError { constructor (m) { super(m) } } /* eslint-enable no-useless-constructor */ module.exports = { TimeoutError: TimeoutError } node-pool-3.1.1/lib/factoryValidator.js000066400000000000000000000006511301060362100200230ustar00rootroot00000000000000 module.exports = function (factory) { if (typeof factory.create !== 'function') { throw new TypeError('factory.create must be a function') } if (typeof factory.destroy !== 'function') { throw new TypeError('factory.destroy must be a function') } if (typeof factory.validate !== 'undefined' && typeof factory.validate !== 'function') { throw new TypeError('factory.validate must be a function') } } node-pool-3.1.1/package.json000066400000000000000000000033231301060362100156670ustar00rootroot00000000000000{ "name": "generic-pool", "description": "Generic resource pooling for Node.JS", "version": "3.1.1", "author": "James Cooper ", "contributors": [ { "name": "James Cooper", "email": "james@bitmechanic.com" }, { "name": "Peter Galiba", "email": "poetro@poetro.hu", "url": "http://poetro.hu/" }, { "name": "Gary Dusbabek" }, { "name": "Tom MacWright", "url": "http://www.developmentseed.org/" }, { "name": "Douglas Christopher Wilson", "email": "doug@somethingdoug.com", "url": "http://somethingdoug.com/" }, { "name": "calibr" }, { "name": "Justin Robinson", "email": "jrobinson@redventures.com>" }, { "name": "Nayana Hettiarachchi", "email": "nayana@corp-gems.com" }, { "name": "Felipe Machado", "email": "felipou@gmail.com" }, { "name": "Felix Becker", "email": "felix.b@outlook.com" }, { "name": "sandfox", "email": "james.butler@sandfox.co.uk" }, { "name": "Lewis J Ellis", "email": "me@lewisjellis.com" } ], "keywords": [ "pool", "pooling", "throttle" ], "main": "index.js", "repository": { "type": "git", "url": "http://github.com/coopernurse/node-pool.git" }, "devDependencies": { "eslint": "^3.4.0", "eslint-config-standard": "^6.0.0", "eslint-plugin-promise": "^3.3.0", "eslint-plugin-standard": "^2.0.0", "tap": "^8.0.0" }, "engines": { "node": ">= 4" }, "scripts": { "lint": "eslint lib test", "lint-fix": "eslint --fix lib test", "test": "tap test/*-test.js " }, "license": "MIT" } node-pool-3.1.1/test/000077500000000000000000000000001301060362100143575ustar00rootroot00000000000000node-pool-3.1.1/test/doubly-linked-list-iterator-test.js000066400000000000000000000064311301060362100232400ustar00rootroot00000000000000const tap = require('tap') const DLL = require('../lib/DoublyLinkedList') const Iterator = require('../lib/DoublyLinkedListIterator') tap.test('iterates forward', function (t) { const dll = new DLL() const node1 = DLL.createNode({id: 1}) const node2 = DLL.createNode({id: 2}) const node3 = DLL.createNode({id: 3}) const node4 = DLL.createNode({id: 4}) dll.insertBeginning(node1) dll.insertBeginning(node2) dll.insertBeginning(node3) dll.insertBeginning(node4) const iterator = new Iterator(dll) const iterationResult1 = iterator.next() t.notOk(iterationResult1.done) t.same(iterationResult1.value, node4) iterator.next() iterator.next() const iterationResult4 = iterator.next() t.notOk(iterationResult4.done) t.same(iterationResult4.value, node1) const iterationResult5 = iterator.next() t.ok(iterationResult5.done) t.end() }) tap.test('iterates backwards', function (t) { const dll = new DLL() const node1 = DLL.createNode({id: 1}) const node2 = DLL.createNode({id: 2}) const node3 = DLL.createNode({id: 3}) const node4 = DLL.createNode({id: 4}) dll.insertBeginning(node1) dll.insertBeginning(node2) dll.insertBeginning(node3) dll.insertBeginning(node4) const iterator = new Iterator(dll, true) const iterationResult1 = iterator.next() t.notOk(iterationResult1.done) t.same(iterationResult1.value, node1) iterator.next() iterator.next() const iterationResult4 = iterator.next() t.notOk(iterationResult4.done) t.same(iterationResult4.value, node4) const iterationResult5 = iterator.next() t.ok(iterationResult5.done) t.end() }) tap.test('iterates forward when adding nodes after creating iterator', function (t) { const dll = new DLL() const node1 = DLL.createNode({id: 1}) const node2 = DLL.createNode({id: 2}) const iterator = new Iterator(dll) dll.insertBeginning(node1) dll.insertBeginning(node2) const iterationResult1 = iterator.next() t.notOk(iterationResult1.done) t.same(iterationResult1.value, node2) const iterationResult2 = iterator.next() t.notOk(iterationResult2.done) t.same(iterationResult2.value, node1) const iterationResult3 = iterator.next() t.ok(iterationResult3.done) t.end() }) tap.test('iterates backwards when adding nodes after creating iterator', function (t) { const dll = new DLL() const node1 = DLL.createNode({id: 1}) const node2 = DLL.createNode({id: 2}) const iterator = new Iterator(dll, true) dll.insertBeginning(node1) dll.insertBeginning(node2) const iterationResult1 = iterator.next() t.notOk(iterationResult1.done) t.same(iterationResult1.value, node1) const iterationResult2 = iterator.next() t.notOk(iterationResult2.done) t.same(iterationResult2.value, node2) const iterationResult3 = iterator.next() t.ok(iterationResult3.done) t.end() }) tap.test('stops iterating when node is detached', function (t) { const dll = new DLL() const iterator = new Iterator(dll) const node1 = DLL.createNode({id: 1}) const node2 = DLL.createNode({id: 2}) dll.insertBeginning(node1) dll.insertBeginning(node2) const iterationResult1 = iterator.next() t.notOk(iterationResult1.done) t.same(iterationResult1.value, node2) dll.remove(node1) const iterationResult3 = iterator.next() t.ok(iterationResult3.done) t.end() }) node-pool-3.1.1/test/doubly-linked-list-test.js000066400000000000000000000011411301060362100214020ustar00rootroot00000000000000var tap = require('tap') var DLL = require('../lib/DoublyLinkedList') tap.test('operations', function (t) { var dll = new DLL() var item1 = {id: 1} var item2 = {id: 2} var item3 = {id: 3} var item4 = {id: 4} dll.insertBeginning(DLL.createNode(item1)) t.equal(dll.head.data, item1) dll.insertEnd(DLL.createNode(item2)) t.equal(dll.tail.data, item2) dll.insertAfter(dll.tail, DLL.createNode(item3)) t.equal(dll.tail.data, item3) dll.insertBefore(dll.tail, DLL.createNode(item4)) t.equal(dll.tail.data, item3) dll.remove(dll.tail) t.equal(dll.tail.data, item4) t.end() }) node-pool-3.1.1/test/generic-pool-acquiretimeout-test.js000066400000000000000000000027311301060362100233160ustar00rootroot00000000000000'use strict' const tap = require('tap') const createPool = require('../').createPool tap.test('acquireTimeout handles timed out acquire calls', function (t) { const factory = { create: function () { return new Promise(function (resolve) { setTimeout(function () { resolve({}) }, 100) }) }, destroy: function () { return Promise.resolve() } } const config = { acquireTimeoutMillis: 20, idleTimeoutMillis: 150, log: false } const pool = createPool(factory, config) pool.acquire() .then(function (resource) { t.fail('wooops') }) .catch(function (err) { t.match(err, /ResourceRequest timed out/) return pool.drain() }) .then(function () { return pool.clear() }) .then(function () { }) .then(t.end) .catch(t.error) }) tap.test('acquireTimeout handles non timed out acquire calls', function (t) { const myResource = {} const factory = { create: function () { return new Promise(function (resolve) { setTimeout(function () { resolve(myResource) }, 10) }) }, destroy: function () { return Promise.resolve() } } const config = { acquireTimeoutMillis: 400 } const pool = createPool(factory, config) pool.acquire() .then(function (resource) { t.equal(resource, myResource) pool.release(resource) return pool.drain() }) .then(function () { return pool.clear() }) .then(t.end) .catch(t.error) }) node-pool-3.1.1/test/generic-pool-test.js000066400000000000000000000331761301060362100202670ustar00rootroot00000000000000'use strict' const tap = require('tap') const createPool = require('../').createPool const utils = require('./utils') const ResourceFactory = utils.ResourceFactory // tap.test('Pool expands only to max limit', function (t) { // const resourceFactory = new ResourceFactory() // const config = { // max: 1 // } // const pool = createPool(resourceFactory, config) // // NOTES: // // - request a resource // // - once we have it, request another and check the pool is fool // pool.acquire(function (err, obj) { // t.error(err) // const poolIsFull = !pool.acquire(function (err, obj) { // t.error(err) // t.equal(1, resourceFactory.created) // pool.release(obj) // utils.stopPool(pool) // t.end() // }) // t.ok(poolIsFull) // t.equal(1, resourceFactory.created) // pool.release(obj) // }) // }) // tap.test('Pool respects min limit', function (t) { // const resourceFactory = new ResourceFactory() // const config // min: 1, // max: 2 // } // const pool = createPool(resourceFactory, config) // // FIXME: this logic only works because we know it takes ~1ms to create a resource // // we need better hooks into the pool probably to observe this... // setTimeout(function () { // t.equal(resourceFactory.created, 1) // utils.stopPool(pool) // t.end() // }, 10) // }) tap.test('min and max limit defaults', function (t) { const resourceFactory = new ResourceFactory() const pool = createPool(resourceFactory) t.equal(1, pool.max) t.equal(0, pool.min) utils.stopPool(pool) t.end() }) tap.test('malformed min and max limits are ignored', function (t) { const resourceFactory = new ResourceFactory() const config = { min: 'asf', max: [] } const pool = createPool(resourceFactory, config) t.equal(1, pool.max) t.equal(0, pool.min) utils.stopPool(pool) t.end() }) tap.test('min greater than max sets to max', function (t) { const resourceFactory = new ResourceFactory() const config = { min: 5, max: 3 } const pool = createPool(resourceFactory, config) t.equal(3, pool.max) t.equal(3, pool.min) utils.stopPool(pool) t.end() }) tap.test('supports priority on borrow', function (t) { // NOTE: this test is pretty opaque about what it's really testing/expecting... let borrowTimeLow = 0 let borrowTimeHigh = 0 let borrowCount = 0 const resourceFactory = new ResourceFactory() const config = { max: 1, priorityRange: 2 } const pool = createPool(resourceFactory, config) function lowPriorityOnFulfilled (obj) { const time = Date.now() if (time > borrowTimeLow) { borrowTimeLow = time } borrowCount++ pool.release(obj) } function highPriorityOnFulfilled (obj) { const time = Date.now() if (time > borrowTimeHigh) { borrowTimeHigh = time } borrowCount++ pool.release(obj) } const operations = [] for (let i = 0; i < 10; i++) { const op = pool.acquire(1).then(lowPriorityOnFulfilled) operations.push(op) } for (let i = 0; i < 10; i++) { const op = pool.acquire(0).then(highPriorityOnFulfilled) operations.push(op) } Promise.all(operations).then(function () { t.equal(20, borrowCount) t.equal(true, borrowTimeLow >= borrowTimeHigh) utils.stopPool(pool) t.end() }) .catch(t.threw) }) // FIXME: bad test! // pool.destroy makes no obligations to user about when it will destroy the resource // we should test that "destroyed" objects are not acquired again instead // tap.test('removes correct object on reap', function (t) { // const resourceFactory = new ResourceFactory() // const config // max: 2 // } // const pool = createPool(resourceFactory, config) // const op1 = pool.acquire() // .then(function (client) { // return new Promise(function (resolve, reject) { // // should be removed second // setTimeout(function () { // pool.destroy(client) // resolve() // }, 5) // }) // }) // const op2 = pool.acquire() // .then(function (client) { // pool.destroy(client) // }) // Promise.all([op1, op2]).then(function () { // t.equal(1, resourceFactory.bin[0].id) // t.equal(0, resourceFactory.bin[1].id) // utils.stopPool(pool) // t.end() // }) // .catch(t.threw) // }) tap.test('evictor removes instances on idletimeout', function (t) { const resourceFactory = new ResourceFactory() const config = { min: 2, max: 2, idleTimeoutMillis: 50, evictionRunIntervalMillis: 10 } const pool = createPool(resourceFactory, config) setTimeout(function () { pool.acquire() .then((res) => { t.ok(res.id > 1) return pool.release(res) }) .then(() => { utils.stopPool(pool) t.end() }) }, 120) }) tap.test('tests drain', function (t) { const count = 5 let acquired = 0 const resourceFactory = new ResourceFactory() const config = { max: 2, idletimeoutMillis: 300000 } const pool = createPool(resourceFactory, config) const operations = [] function onAcquire (client) { acquired += 1 t.equal(typeof client.id, 'number') setTimeout(function () { pool.release(client) }, 250) } // request 5 resources that release after 250ms for (let i = 0; i < count; i++) { const op = pool.acquire().then(onAcquire) operations.push(op) } // FIXME: what does this assertion prove? t.notEqual(count, acquired) Promise.all(operations) .then(function () { return pool.drain() }) .then(function () { t.equal(count, acquired) // short circuit the absurdly long timeouts above. pool.clear() }) .then(function () { // subsequent calls to acquire should resolve an error. return pool.acquire().then(t.fail, function (e) { t.type(e, Error) }) }) .then(function () { t.end() }) }) tap.test('handle creation errors', function (t) { let created = 0 const resourceFactory = { create: function () { created++ if (created < 5) { return Promise.reject(new Error('Error occurred.')) } else { return Promise.resolve({ id: created }) } }, destroy: function (client) {} } const config = { max: 1 } const pool = createPool(resourceFactory, config) // FIXME: this section no longer proves anything as factory // errors no longer bubble up through the acquire call // we need to make the Pool an Emitter // ensure that creation errors do not populate the pool. // for (const i = 0; i < 5; i++) { // pool.acquire(function (err, client) { // t.ok(err instanceof Error) // t.ok(client === null) // }) // } let called = false pool.acquire() .then(function (client) { t.equal(typeof client.id, 'number') called = true pool.release(client) }) .then(function () { t.ok(called) t.equal(pool.pending, 0) utils.stopPool(pool) t.end() }) .catch(t.threw) }) tap.test('handle creation errors for delayed creates', function (t) { let attempts = 0 const resourceFactory = { create: function () { attempts++ if (attempts <= 5) { return Promise.reject(new Error('Error occurred.')) } else { return Promise.resolve({ id: attempts }) } }, destroy: function (client) { return Promise.resolve() } } const config = { max: 1 } const pool = createPool(resourceFactory, config) let errorCount = 0 pool.on('factoryCreateError', function (err) { t.ok(err instanceof Error) errorCount++ }) let called = false pool.acquire() .then(function (client) { t.equal(typeof client.id, 'number') called = true pool.release(client) }) .then(function () { t.ok(called) t.equal(errorCount, 5) t.equal(pool.pending, 0) utils.stopPool(pool) t.end() }) .catch(t.threw) }) tap.test('getPoolSize', function (t) { let assertionCount = 0 const resourceFactory = new ResourceFactory() const config = { max: 2 } const pool = createPool(resourceFactory, config) const borrowedResources = [] t.equal(pool.size, 0) assertionCount += 1 pool.acquire() .then(function (obj) { borrowedResources.push(obj) t.equal(pool.size, 1) assertionCount += 1 }) .then(function () { return pool.acquire() }) .then(function (obj) { borrowedResources.push(obj) t.equal(pool.size, 2) assertionCount += 1 }) .then(function () { pool.release(borrowedResources.shift()) pool.release(borrowedResources.shift()) }) .then(function () { return pool.acquire() }) .then(function (obj) { // should still be 2 t.equal(pool.size, 2) assertionCount += 1 pool.release(obj) }) .then(function () { t.equal(assertionCount, 4) utils.stopPool(pool) t.end() }) .catch(t.threw) }) tap.test('availableObjectsCount', function (t) { let assertionCount = 0 const resourceFactory = new ResourceFactory() const config = { max: 2 } const pool = createPool(resourceFactory, config) const borrowedResources = [] t.equal(pool.available, 0) assertionCount += 1 pool.acquire() .then(function (obj) { borrowedResources.push(obj) t.equal(pool.available, 0) assertionCount += 1 }).then(function () { return pool.acquire() }) .then(function (obj) { borrowedResources.push(obj) t.equal(pool.available, 0) assertionCount += 1 }) .then(function () { pool.release(borrowedResources.shift()) t.equal(pool.available, 1) assertionCount += 1 pool.release(borrowedResources.shift()) t.equal(pool.available, 2) assertionCount += 1 }) .then(function () { return pool.acquire() }) .then(function (obj) { t.equal(pool.available, 1) assertionCount += 1 pool.release(obj) t.equal(pool.available, 2) assertionCount += 1 }) .then(function () { t.equal(assertionCount, 7) utils.stopPool(pool) t.end() }) .catch(t.threw) }) // FIXME: bad test! // pool.destroy makes no obligations to user about when it will destroy the resource // we should test that "destroyed" objects are not acquired again instead // tap.test('removes from available objects on destroy', function (t) { // let destroyCalled = false // const factory = { // create: function () { return Promise.resolve({}) }, // destroy: function (client) { destroyCalled = true; return Promise.resolve() } // } // const config // max: 2 // } // const pool = createPool(factory, config) // pool.acquire().then(function (obj) { // pool.destroy(obj) // }) // .then(function () { // t.equal(destroyCalled, true) // t.equal(pool.available, 0) // utils.stopPool(pool) // t.end() // }) // .catch(t.threw) // }) // FIXME: bad test! // pool.destroy makes no obligations to user about when it will destroy the resource // we should test that "destroyed" objects are not acquired again instead // tap.test('removes from available objects on validation failure', function (t) { // const destroyCalled = false // const validateCalled = false // const count = 0 // const factory = { // create: function () { return Promise.resolve({count: count++}) }, // destroy: function (client) { destroyCalled = client.count }, // validate: function (client) { // validateCalled = true // return Promise.resolve(client.count > 0) // } // } // const config // max: 2, // testOnBorrow: true // } // const pool = createPool(factory, config) // pool.acquire() // .then(function (obj) { // pool.release(obj) // t.equal(obj.count, 0) // }) // .then(function () { // return pool.acquire() // }) // .then(function (obj2) { // pool.release(obj2) // t.equal(obj2.count, 1) // }) // .then(function () { // t.equal(validateCalled, true) // t.equal(destroyCalled, 0) // t.equal(pool.available, 1) // utils.stopPool(pool) // t.end() // }) // .catch(t.threw) // }) tap.test('do schedule again if error occured when creating new Objects async', function (t) { // NOTE: we're simulating the first few resource attempts failing let resourceCreationAttempts = 0 const factory = { create: function () { resourceCreationAttempts++ if (resourceCreationAttempts < 2) { return Promise.reject(new Error('Create Error')) } return Promise.resolve({}) }, destroy: function (client) {} } const config = { max: 1 } const pool = createPool(factory, config) // pool.acquire(function () {}) pool.acquire().then(function (obj) { t.equal(pool.available, 0) pool.release(obj) utils.stopPool(pool) t.end() }).catch(t.threw) }) tap.test('returns only valid object to the pool', function (t) { const pool = createPool({ create: function () { return Promise.resolve({ id: 'validId' }) }, destroy: function (client) {}, max: 1 }) pool.acquire().then(function (obj) { t.equal(pool.available, 0) t.equal(pool.borrowed, 1) // Invalid release pool.release({}) t.equal(pool.available, 0) t.equal(pool.borrowed, 1) // Valid release pool.release(obj) t.equal(pool.available, 1) t.equal(pool.borrowed, 0) utils.stopPool(pool) t.end() }).catch(t.threw) }) tap.test('validate acquires object from the pool', function (t) { const pool = createPool({ create: function () { return Promise.resolve({ id: 'validId' }) }, validate: function (resource) { return Promise.resolve(true) }, destroy: function (client) {}, max: 1 }) pool.acquire() .then(function (obj) { t.equal(pool.available, 0) t.equal(pool.borrowed, 1) pool.release(obj) utils.stopPool(pool) t.end() }) .catch(t.threw) }) node-pool-3.1.1/test/resource-request-test.js000066400000000000000000000027521301060362100212150ustar00rootroot00000000000000var tap = require('tap') var ResourceRequest = require('../lib/ResourceRequest') tap.test('can be created', function (t) { var create = function () { var request = new ResourceRequest(undefined, Promise) // eslint-disable-line no-unused-vars } t.doesNotThrow(create) t.end() }) tap.test('times out when created with a ttl', function (t) { var reject = function (err) { t.match(err, /ResourceRequest timed out/) t.end() } var resolve = function (r) { t.fail('should not resolve') } var request = new ResourceRequest(10, Promise) // eslint-disable-line no-unused-vars request.promise.then(resolve, reject) }) tap.test('calls resolve when resolved', function (t) { var resource = {} var resolve = function (r) { t.equal(r, resource) t.end() } var reject = function (err) { t.error(err) } var request = new ResourceRequest(undefined, Promise) request.promise.then(resolve, reject) request.resolve(resource) }) tap.test('removeTimeout removes the timeout', function (t) { var reject = function (err) { t.error(err) } var request = new ResourceRequest(10, Promise) request.promise.then(undefined, reject) request.removeTimeout() setTimeout(function () { t.end() }, 20) }) tap.test('does nothing if resolved more than once', function (t) { var request = new ResourceRequest(undefined, Promise) t.doesNotThrow(function () { request.resolve({}) }) t.doesNotThrow(function () { request.resolve({}) }) t.end() }) node-pool-3.1.1/test/utils.js000066400000000000000000000013501301060362100160540ustar00rootroot00000000000000/** * Generic class for handling creation of resources * for testing */ var ResourceFactory = function ResourceFactory () { this.created = 0 this.destroyed = 0 this.bin = [] } ResourceFactory.prototype.create = function () { var id = this.created++ var resource = { id: id } return Promise.resolve(resource) } ResourceFactory.prototype.destroy = function (resource) { this.destroyed++ this.bin.push(resource) return Promise.resolve() } exports.ResourceFactory = ResourceFactory /** * drains and terminates the pool * * @param {[type]} pool [description] * @return {[type]} [description] */ exports.stopPool = function (pool) { return pool.drain() .then(function () { return pool.clear() }) }