What is tarn?
The tarn.js package is a small, robust resource pool for JavaScript. It is primarily used for managing a pool of resources such as database connections, ensuring efficient resource utilization and scalability in applications. It provides features like acquiring and releasing resources, timeout handling, and error management.
What are tarn's main functionalities?
Resource Pool Management
This code demonstrates how to create a resource pool with minimum and maximum limits, and manage resource acquisition and release. It handles timeouts and resource reaping intervals.
const { Pool } = require('tarn');
const pool = new Pool({
create: () => Promise.resolve(new Resource()),
destroy: resource => Promise.resolve(resource.dispose()),
min: 2,
max: 10,
acquireTimeoutMillis: 30000,
idleTimeoutMillis: 30000,
reapIntervalMillis: 1000
});
pool.acquire().promise.then(resource => {
console.log('Resource acquired');
pool.release(resource);
});
Other packages similar to tarn
generic-pool
generic-pool is a robust resource pooling library similar to tarn. It supports priority queuing and more extensive configuration options for resource management. While tarn focuses on simplicity and specific use cases, generic-pool offers broader flexibility and customization.
pool2
pool2 is another resource pooling library that provides features like deferred resource creation and better error handling. It is designed to be a more modern and feature-rich alternative to older pooling libraries, offering a different API but similar core functionalities as tarn.
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