Pewl
Generic pooling library
Useful for managing resources like database connections.
Install
$ npm install pewl
Usage
Here's a simple example of how to get up and running with pewl
.
const Pool = ; // create a Pool instance.const pool = ; // open the poolawait pool; // get a resource from the poolconst resource = await pool; // return the resource to the poolpool; // close the pool when finishedawait pool;
API
Pool([options])
Returns a new pool
instance.
options
Type: Object
acquireTimeout
Type: number
Default: Infinity
Duration to wait before rejecting acquire request.
attributes
Type: string[]
Attributes used to describe the Pool when using a Cluster.
concurrency
Type: number
Default: Infinity
Concurrency limit for making resource requests.
create
Type: Function
Function that returns a Promise that resolves with a resource.
destroy
Type: Function
Accepts a resource value and attempts to destroy it, returning a promise upon completion.
diesAfter
Type: number
Default: Infinity
Marks a resource as dead after this duration.
fifo
Type: boolean
Default: true
Treats the Pool like a stack (as opposed to a queue).
idlesAfter
Type: number
Default: 600000
Marks a resource as idle after this duration.
isDead
Type: Function
Accepts a Resource object and returns a boolean stating whether or not the resource is dead.
isIdle
Type: Function
Accepts a Resource object and returns a boolean stating whether or not the resource is idle.
max
Type: number
Default: Infinity
Maximum number of resources the pool can create.
maxIdle
Type: number
Default: Infinity
Maximum number of idle resources the pool can contain with respect to the min
and max
options.
min
Type: number
Default: 0
Minimum number of resources that must be in the pool at all times.
ping
Type: Function
Accepts a resource value and attempts to ping it, returning a promise upon completion.
pingInterval
Type: number
Default: 600000
Frequency to ping idle resources.
skimInterval
Type: number
Frequency to check for and dispose of dead and idle resources with respect to
the min
and max
options.
Pool#acquire([options])
Returns a promise that settles when a resource is acquired.
options
Type: Object
priority
Type: Number
Default: 0
Priority of operation. Operations with greater priority will be schedule first.
Pool#close()
Returns a promise that settles once all resources have been destroyed.
Pool#destroy(resource)
Returns a promise that settles once the suppled resource is destroyed.
Pool#open()
Returns a promise that settles once the pool is opened and filled to the min
value.
Pool#release(resource)
Releases a resource back into the pool.
Pool#available
Number of available resources.
Pool#borrowed
Number of borrowed resources.
Pool#idle
Number of idle resources.
Pool#size
Total number of resources in pool.
Cluster([options])
Returns a new cluster
instance.
options
Type: Object
Options to be applied to all pool
instances within the cluster.
acquireTimeout
Type: number
Default: Infinity
Duration to wait before rejecting acquire request.
concurrency
Type: number
Default: Infinity
Concurrency limit for making resource requests.
diesAfter
Type: number
Default: Infinity
Marks a resource as dead after this duration.
fifo
Type: boolean
Default: true
Treats the Pool like a stack (as opposed to a queue).
idlesAfter
Type: number
Default: 600000
Marks a resource as idle after this duration.
ping
Type: Function
Accepts a resource value and attempts to ping it, returning a promise upon completion.
pingInterval
Type: number
Default: 600000
Frequency to ping idle resources.
pools
Type: Pool[]
List of pool
instances.
skimInterval
Type: number
Frequency to check for and dispose of dead and idle resources with respect to
the min
and max
options.
Cluster#acquire([options])
Returns a promise that settles when a resource is acquired.
options
Type: Object
attributes
Type: string[]
List of attributes that the resource must have.
priority
Type: number
Default: 0
Priority of operation. Operations with greater priority will be schedule first.
Cluster#add(pool)
Adds a pool
instance to the cluster.
Cluster#close()
Returns a promise that settles once the cluster and all the pool
instances
have been closed.
Cluster#destroy(resource)
Destroys the provided resource.
Cluster#open()
Returns a promise that settles once all the pool
instances have been opened
and filled.
Cluster#release(resource)
Releases the resource back into the cluster.
Cluster#stats([attributes])
Gets a ClusterStats
object for the cluster resources.
attributes
Type: String[]
Attributes to apply to the stats.
Managing multiple Pools
const Pool = ; // create a pool of read-only resourcesconst readPool = attributes: 'r' ...; // create a pool of read-write resourcesconst readWritePool = attributes: 'r' 'w' ...; // create a cluster to manage both poolsconst cluster = pools: readPool readWritePool; // start all the things!await cluster; // acquire the first available resourceconst readOnlyResource = await cluster; // release the resource back into the clustercluster; // acquire a write capable resourceconst readWriteResource = await cluster; // destroy the write capable resourcecluster; // close all the things!await cluster;