Skip to content

baryshev/common-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Simple resource pool (NodeJS)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published