Pool

Pool

new Pool(options)

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

Source:
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 <optional>

min number of active resource items

max int <optional>

max number of active resource items

max_queue int <optional>

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

timeout int <optional>

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

idle int <optional>

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 lib.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);
}