Poolp -- a parameterized pool of objects
Summary
Poolp is a basic pool library that will help you manage often reused objects while limiting their concurrent use.
It allows you to define how many different objects can live at the same time, and how long they should live without being used.
Example
var pool = { iftypeof options !== 'object' return ; var myObj = options; ;}; pool;
Usage
new Poolp(factory, options)
Create a new pool.
- factory(options, callback): A function to create new objects.
- options: Options that should help you create your custom object, it's coming directly from the
take
function. - callback(err, obj): This must be called once your object is created or if an error occurred.
- options: Options that should help you create your custom object, it's coming directly from the
- options: An optional object containing your settings.
- maxObjects: Maximum number of objects to have at a time. (default: 5)
- maxTTL: Maximum time an object can spend idling before it is destroyed. (default: 30s)
- destroy: Function called when an object is being retired from the queue.
pool.take(options, callback)
Ask the pool for an object built with the specified options.
- options: It can be just anything. It will be provided as is to the
factory
, and will be used to look for existing objects in the queue. - callback(err, object, release): It will be called when the queue is ready to give you an answer.
- err: If your factory provides an error, you will find it here.
- object: The object provided by your factory.
- release: A function to tell the queue you are done with the object and it is available again for another
take
.
pool.tearDown()
By calling this, the pool will stop providing you with new or existing objects. The current objects handled by the pool will finish their work if needed, and be destroyed.
Credits
Special thanks to :
- @caolan for async
- @documentcloud for underscore