Class: PromisePool

PromisePool

A resource pooling class with a promise-based API.

Constructor

new PromisePool(opts)

Constructs a new pool with the provided factory options.

Parameters:
Name Type Description
opts PromisePool.Factory

The connection factory which specifies the functionality for the pool.

Source:

Namespaces

Factory

Members

(readonly) availableLength :number

The number of available (e.g. idle) resources in the pool.

Type:
  • number
Source:

(readonly) length :number

The total number of resources in the pool.

Type:
  • number
Source:

(readonly) max :number

The maximum number of resources this pool will create.

Type:
  • number
Source:

(readonly) min :number

The minimum number of resources the pool will keep at any given time.

Type:
  • number
Source:

(readonly) name :string

The name of the pool, as provided in the factory.

Type:
  • string
Source:

(readonly) waitingClientLength :number

The number of clients currently waiting for a resource to become available/be created.

Type:
  • number
Source:

Methods

acquire(callback, priority) → {Promise.<*>}

Request a new client. The callback will be called with a client when one becomes available.

Parameters:
Name Type Description
callback PromisePool.AcquireCallback

Callback function to be called after the acquire is successful. The function will receive the acquired item as the first parameter.

priority Number

Optional. Integer between 0 and (priorityRange - 1). Specifies the priority of the caller if there are no available resources. Lower numbers mean higher priority.

Source:
Returns:

A promise for the results of the acquire callback.

Type
Promise.<*>

destroy(obj)

Request the client to be destroyed. The factory's destroy handler will also be called.

This should be called within an acquire() block as an alternative to release().

Parameters:
Name Type Description
obj PromisePool.Client

The acquired item to be destoyed.

Source:

destroyAllNow() → {Promise}

Forcibly destroys all clients regardless of timeout.

Intended to be invoked as part of a drain. Does not prevent the creation of new clients as a result of subsequent calls to acquire.

Note that if factory.min > 0 and the pool is not draining, the pool will destroy all idle resources in the pool, but replace them with newly created resources up to the specified factory.min value. If this is not desired, set factory.min to zero before calling PromisePool#destroyAllNow().

Source:
Returns:

A promise to have all objects in the pool destroyed.

Type
Promise

drain() → {Promise}

Disallow any new requests and let the request backlog dissapate.

After all clients have finished, the pool will then destroy all pooled resources.

Source:
Returns:

A promise to let all clients finish and destroy all pooled objects.

Type
Promise

pooled(decorated, priority) → {function}

Decorates a function to use a acquired client from the object pool when called.

Parameters:
Name Type Description
decorated PromisePool.AcquireCallback

The decorated function, accepting a client as the first argument and returning a promise.

priority Number

Optional. Integer between 0 and (priorityRange - 1). Specifies the priority of the caller if there are no available resources. Lower numbers mean higher priority.

Source:
Returns:

A function wrapping decorated by first acquiring a client.

Type
function

release(obj)

Return the client to the pool, in case it is no longer required.

Parameters:
Name Type Description
obj PromisePool.Client

The acquired object to be put back to the pool.

Source:

Type Definitions

AcquireCallback(client) → {Promise.<*>}

Parameters:
Name Type Description
client PromisePool.Client

A newly acquired client from the pool.

Source:
Returns:

A promise for whatever result the callback wants to send out.

Type
Promise.<*>