Skip to content

Commit

Permalink
Merge branch 'sushantdhiman-fix-evict'
Browse files Browse the repository at this point in the history
  • Loading branch information
sandfox committed Sep 14, 2017
2 parents 33ea5ef + 3338a5a commit 5312d10
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -141,7 +141,7 @@ An optional object/dictionary with the any of the following properties:
- `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)
- `autostart`: boolean, should the pool start creating resources, initialize the evictor, etc once the constructor is called. If false, the pool can be started by calling `pool.start`, otherwise the first call to `acquire` will start the pool. (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)
Expand Down Expand Up @@ -219,6 +219,14 @@ The pool is an event emitter. Below are the events it emits and any args for tho
- `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.

### pool.start

```js
pool.start()
```

If `autostart` is `false` then this method can be used to start the pool and therefore begin creation of resources, start the evictor, and any other internal logic.

## 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.
Expand Down
4 changes: 4 additions & 0 deletions lib/Pool.js
Expand Up @@ -392,6 +392,10 @@ class Pool extends EventEmitter {
* @returns {Promise}
*/
acquire (priority) {
if (this._started === false && this._config.autostart === false) {
this.start()
}

if (this._draining) {
return this._Promise.reject(new Error('pool is draining and cannot accept work'))
}
Expand Down
26 changes: 26 additions & 0 deletions test/generic-pool-test.js
Expand Up @@ -667,3 +667,29 @@ tap.test('destroy should redispense', function (t) {
})
.catch(t.threw)
})

tap.test('evictor start with acquire when autostart is false', function (t) {
const pool = createPool({
create: function () {
return Promise.resolve({ id: 'validId' })
},
validate: function (resource) {
return Promise.resolve(true)
},
destroy: function (client) {}
}, {
evictionRunIntervalMillis: 10000,
autostart: false
})

t.equal(pool._scheduledEviction, null)

pool.acquire()
.then(function (obj) {
t.notEqual(pool._scheduledEviction, null)
pool.release(obj)
utils.stopPool(pool)
t.end()
})
.catch(t.threw)
})

0 comments on commit 5312d10

Please sign in to comment.