# ee-resource-pool

> resource pool with queueing and rate limiting

Latest version **0.1.9** (published 2014-05-24) · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install ee-resource-pool
pnpm add ee-resource-pool
yarn add ee-resource-pool
bun add ee-resource-pool
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.9 |
| Published | 2014-05-24 |
| First published | 2013-09-06 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=v0.10.17 |
| Dependencies | 7 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Michael van der Weg |
| Maintainers | ee |
| Keywords | ee, resource, pool |

## Links

- npm: https://www.npmjs.com/package/ee-resource-pool
- Repository: https://github.com/eventEmitter/ee-resource-pool
- Issues: https://github.com/eventEmitter/ee-resource-pool/issues
- npm.io page: https://npm.io/package/ee-resource-pool

## Dependencies (7)

- [ee-log](https://npm.io/package/ee-log.md) 0.2.x
- [ee-class](https://npm.io/package/ee-class.md) 1.0.x
- [ee-error](https://npm.io/package/ee-error.md) 0.1.x
- [ee-types](https://npm.io/package/ee-types.md) 0.1.x
- [ee-ttl-queue](https://npm.io/package/ee-ttl-queue.md) 0.1.x
- [ee-rate-limiter](https://npm.io/package/ee-rate-limiter.md) 0.1.x
- [ee-event-emitter](https://npm.io/package/ee-event-emitter.md) 0.1.x

## Alternatives

- [csv-to-markdown-table](https://npm.io/package/csv-to-markdown-table.md) — 47.0K weekly downloads
- [@sapphire/ratelimits](https://npm.io/package/@sapphire/ratelimits.md) — 4.4K weekly downloads
- [js-csvparser](https://npm.io/package/js-csvparser.md) — 2.0K weekly downloads
- [@adadapted/js-sdk](https://npm.io/package/@adadapted/js-sdk.md) — 251 weekly downloads
- [@grapecity/spread-sheets-sparklines](https://npm.io/package/@grapecity/spread-sheets-sparklines.md) — 103 weekly downloads

## Recent versions

- 0.1.9 (latest) — 2014-05-24
- 0.1.8 — 2014-05-23
- 0.1.7 — 2013-11-11
- 0.1.4 — 2013-10-31
- 0.1.3 — 2013-10-29
- 0.1.2 — 2013-10-29
- 0.1.1 — 2013-10-27
- 0.1.0 — 2013-09-06

## README

# ee-resource-pool

resource pool for managing shared resources 

## installation

	npm install ee-resource-pool
	
## build status

[![Build Status](https://travis-ci.org/eventEmitter/ee-resource-pool.png?branch=master)](https://travis-ci.org/eventEmitter/ee-resource-pool)


## usage

example of a pool of connections, there may never be more than 50 connections open. 


	var ResourcePool = require( "ee-resource-pool" );


	var pool = new ResourcePool( {
		  max: 50 					// max 50 connections
		, maxWaitingRequests: 1000 	// fail requests for a connection when there are already more then 1'000 requests waiting
		, timeout: 5000 			// fail requests for a connection after waiting 5 seconds
		, idle: 600000 				// close conenctions if they weren't used in the last 10 minutes
		, prefetch: 10 				// create always 10% more connections currently used, so we neevr have to wait for aconnection to be created
	} );


	// the pool can request more resources, but if you don't need 
	// special resources you dont listen for the event and the pool 
	// will create its own resource representation 
	// -> this is entirely optional
	pool.on( "resourceRequest", function( callback ){
		callback( new Connection() );
	} );


	// if the pool gets a free resource which isn't used by the queue it emits the «resource» event
	pool.on( "resource", function(){
		pool.get( function( err, resource ){
			// you should get that exact resource which triggered the event

		} );
	} );


	// this code is placed where you request the connections
	pool.get( function( err, connection ){
		if ( err ){
			if ( err.name === "CapacityOverloadException" ) {
				console.log( "the resource pool is over capacity ...", err );
			}
			else if ( err.name === "RequestTimeoutException" ){
				console.log( "timeout while waiting for a free connection ...", err );
			}
		}
		else {
			// use the connection
			// IMPORTANT: if you are finished using ti you have to call the «freeResource» or «closeResource» method 

			connection.query( "whatever", function( err, data ){
				if ( err ){
					console.log( "crap, my query failed" );
					connection.closeResource();
					connection.close();
				}
				else {
					console.log( "got some data :)" );
					connection.freeResource();
				}
			} );
		}
	} );



example of an image uploader which never should upload more than 5 images at the same time

	var ResourcePool = require( "ee-resource-pool" );


	var pool = new ResourcePool( {
		  timeout: 3600000 			// fail if we cannot upload an image within the next hour
		, ratelLimit: 5 			// max 5 uploads / second
	} );


	// the pool can request more resources
	pool.on( "resourceRequest", function( callback ){
		callback( new Uploader() );
	} );


	// this code is placed where you uploader your imges, it will rate liimit your request...
	pool.get( function( err, uploader ){
		if ( err ){
			if ( err.name === "RequestTimeoutException" ){
				console.log( "timeout while waiting for a free uploader ...", err );
			}
		}
		else {
			// use the uploader
			// IMPORTANT: if you are finished using ti you have to call the «freeResource» or «closeResource» method 

			uploader.upload.query( data, function( err ){
				if ( err ){
					console.log( "crap, the upload failed" );
					uploader.closeResource();
				}
				else {
					console.log( "upload finished :)" );
					uploader.freeResource();
				}
			} );
		}
	} );

---
_Source: https://npm.io/package/ee-resource-pool · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
