Simple resource pool (NodeJS)
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
.npmignore
.travis.yml
LICENSE
README.md
index.js
package.json

README.md

build status

About

Simple resource pool.

Installation

npm install common-pool

Example

var pool = require("common-pool");
var resourceCreate = function(callback) {
	someResource.create(function(error, resource) {
		callback(error, resource);
	});
};

var resourceDestroy = function(resource) {
	resource.destroy();
};

var maxPoolSize = 10;
var idleTimeout = 3600 * 1000;
var idleInterval = 30000; // Optional, 10000 by default

var resources = pool(resourceCreate, resourceDestroy, maxPoolSize, idleTimeout, idleInterval);

resources.take(function(error, resource) {
	if (error) {
		// Oh no!
		// Resource could not be created.
	} else {
		// Start working with resource.
		resource.doSomething(function(error) {
			if (error) {
				// Something wrong!
				// This is bad resource. Removing it from pool.
				resources.remove(resource);
			} else {
				// Action complete.
				// Returning resource to pool.
				resource.release(resource);
			}
		});
	}
});