Pool

Pool

new Pool(options, min, max, max_queue, timeout, idle)

Description:
  • Create a resource pool, create and close callbacks must be given which perform allocation and deallocation of the resources like db connections.

Parameters:
Name Type Description
options object

defines the following properties

Properties
Name Type Attributes Description
create function <optional>

method to be called to return a new resource item, takes 1 argument, a callback as function(err, item)

destroy function <optional>

method to be called to destroy a resource item

reset function <optional>

method to be called just before releasing an item back to the resource pool, this is a chance to reset the item to the initial state

validate function <optional>

method to verify active resource item, return false if it needs to be destroyed

init function <optional>

method to cal on pool.init, it may be called multiple times

shutdown function <optional>

method to call on pool shutdown to clear other resources

min int

min number of active resource items

max int

max number of active resource items

max_queue int

how big the waiting queue can be, above this all requests will be rejected immediately

timeout int

number of milliseconds to wait for the next available resource item, cannot be 0

idle int

number of milliseconds before starting to destroy all active resources above the minimum, 0 to disable.

If no create implementation callback is given then all operations are basically noop still calling the callbacks.

Example
var pool = new Pool({
    min: 1,
    max: 5,
    create: function(cb) {
        someDb.connect((err) => { cb(err, this) }
    },
    destroy: function(client) {
        client.close() }
    })
});

pool.use((err, client) => {
    ...
    pool.release(client);
});

const { err, client } = await pool.ause();
if (!err) {
    ...
    pool.release(client);
}