Why yet another resource pool?
Tarn is focused on robustness and ability to recover from errors. Tarn has timeouts for all operations
that can fail or timeout so that you should never end up with pool full of crap. Tarn has a comprehensive
test suite and we are committed to adding tests and fixing all bugs that are found.
Tarn will always remain simple.
Install
npm install tarn
Usage
const { Pool, TimeoutError } = require('tarn');
const pool = new Pool({
create: cb => {
cb(null, new SomeResource());
},
validate: resource => {
return true;
},
destroy: someResource => {
someResource.cleanup();
},
log: (message, logLevel) => console.log(`${logLevel}: ${message}`)
min: 2,
max: 10,
acquireTimeoutMillis: 30000,
createTimeoutMillis: 30000,
destroyTimeoutMillis: 5000,
idleTimeoutMillis: 30000,
reapIntervalMillis: 1000,
createRetryIntervalMillis: 200,
propagateCreateError: false
});
const acquire = pool.acquire();
acquire.abort();
try {
const resource = await acquire.promise;
} catch (err) {
if (err instanceof TimeoutError) {
console.log('timeout');
}
}
pool.release(resource);
pool.numUsed();
pool.numFree();
pool.numPendingAcquires();
pool.numPendingCreates();
await pool.destroy();
pool.on('acquireRequest', eventId => {});
pool.on('acquireSuccess', (eventId, resource) => {});
pool.on('acquireFail', (eventId, err) => {});
pool.on('release', resource => {});
pool.on('createRequest', eventId => {});
pool.on('createSuccess', (eventId, resource) => {});
pool.on('createFail', (eventId, err) => {});
pool.on('destroyRequest', (eventId, resource) => {});
pool.on('destroySuccess', (eventId, resource) => {});
pool.on('destroyFail', (eventId, resource, err) => {});
pool.on('startReaping', () => {});
pool.on('stopReaping', () => {});
pool.on('poolDestroyRequest', eventId => {});
pool.on('poolDestroySuccess', eventId => {});
pool.removeListener(eventName, listener);
pool.removeAllListeners(eventName);
Changelog
Master
3.0.2 2021-11-29
- Valid resources with rejected acquires are returned to the pool #68
3.0.1 2020-10-25
- Added triggering missing createFail event on timeout error - fixes #57
3.0.0 2020-04-18
- Async validation support, now validation resource function can return a promise #45
- Fixed releasing abandoned resource after creation when create timeout #48
Released as major version, because async validation support did require lots of internal changes, which may cause subtle difference in behavior.
2.0.0 2019-06-02
- Accidentally published breaking changes in 1.2.0. Unpublished it and published again with correct version number 2.0.0 #33
1.2.0 2019-06-02 (UNPUBLISHED)
- Passing unknown options throws an error #19 #32
- Diagnostic event handlers to allow monitoring pool behaviour #14 #23
- Dropped node 6 support #25 #28
- pool.destroy() now always waits for all pending destroys to finish before resolving #29
1.1.5 2019-04-06
- Added changelog #22
- Handle opt.destroy() being a promise with destroyTimeout #16
- Explicitly silence bluebird warnings #17
- Add strict typings via TypeScript #10