What is generic-pool?
The generic-pool npm package is a resource pooling library that allows users to manage a pool of resources such as database connections, network connections, or any other resource that is expensive to create and can be reused. It helps to limit the number of resources created and manages the allocation and deallocation of these resources efficiently.
What are generic-pool's main functionalities?
Creating a pool of resources
This code sample demonstrates how to create a pool with a maximum of 10 resources and a minimum of 2. The 'create' function is used to create a new resource, and the 'destroy' function is used to clean up a resource when it is no longer needed.
const genericPool = require('generic-pool');
const pool = genericPool.createPool({
create: () => createMyResource(),
destroy: (resource) => destroyMyResource(resource)
}, {
max: 10,
min: 2
});
Acquiring and releasing resources
This code sample shows how to acquire a resource from the pool and then release it back to the pool once it is no longer needed. The 'acquire' method returns a promise that resolves with a resource when one becomes available.
pool.acquire().then(resource => {
// use the resource
pool.release(resource);
}).catch(err => {
// handle error
});
Draining the pool and shutting down
This code sample illustrates how to drain the pool of all its resources and then shut it down completely. The 'drain' method returns a promise that resolves once all the resources have been returned to the pool and are no longer in use.
pool.drain().then(() => pool.clear());
Other packages similar to generic-pool
bottleneck
Bottleneck is a rate limiter that can be used to throttle function calls. It is similar to generic-pool in that it helps manage resource usage, but it focuses on limiting the rate of operations rather than managing a pool of reusable resources.
pool2
Pool2 is another resource pooling library that provides similar functionalities to generic-pool. It offers features like resource creation, destruction, and timeout handling. Pool2 is designed to be a more modern and extensible version of generic-pool with additional features like priority queuing.
About
Generic resource pool. Can be used to reuse or throttle expensive resources such as
database connections.
Installation
$ npm install generic-pool
Example
// Create a MySQL connection pool with
// a max of 10 connections and a 30 second max idle time
var poolModule = require('pool');
var pool = poolModule.Pool({
name : 'mysql',
create : function(callback) {
var Client = require('mysql').Client;
var c = new Client();
c.user = 'scott';
c.password = 'tiger';
c.database = 'mydb';
c.connect();
callback(c);
},
destroy : function(client) { client.end(); },
max : 10,
idleTimeoutMillis : 30000
});
// borrow connection - callback function is called
// once a resource becomes available
pool.borrow(function(client) {
client.query("select * from foo", [], function() {
// return object back to pool
pool.returnToPool(client);
});
});
Documentation
Pool() accepts an object with these slots:
name : name of pool (string, optional)
create : function that returns a new resource
should call callback() with the created resource
destroy : function that accepts a resource and destroys it
max :
Run Tests
$ npm install expresso
$ expresso -I lib test/*.js