generic-pool
Advanced tools
Comparing version 2.0.3 to 2.0.4
@@ -168,3 +168,3 @@ var PriorityQueue = function(size) { | ||
// check if they have timed out | ||
for (i = 0, al = availableObjects.length; i < al && (refreshIdle || (count - factory.min)) > toRemove.length ; i += 1) { | ||
for (i = 0, al = availableObjects.length; i < al && (refreshIdle || (count - factory.min > toRemove.length)); i += 1) { | ||
timeout = availableObjects[i].timeout; | ||
@@ -171,0 +171,0 @@ if (now >= timeout) { |
{ | ||
"name": "generic-pool", | ||
"description": "Generic resource pooling for Node.JS", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"author": "James Cooper <james@bitmechanic.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
226
README.md
@@ -23,2 +23,5 @@ [![build status](https://secure.travis-ci.org/coopernurse/node-pool.png)](http://travis-ci.org/coopernurse/node-pool) | ||
2.0.4 - July 27 2013 | ||
- Merged #64 - Fix for not removing idle objects (contributed by PiotrWpl) | ||
2.0.3 - January 16 2013 | ||
@@ -97,46 +100,50 @@ - Merged #56/#57 - Add optional refreshIdle flag. If false, idle resources at the pool minimum will not be | ||
// Create a MySQL connection pool with | ||
// a max of 10 connections, a min of 2, and a 30 second max idle time | ||
var poolModule = require('generic-pool'); | ||
var pool = poolModule.Pool({ | ||
name : 'mysql', | ||
create : function(callback) { | ||
var Client = require('mysql').Client; | ||
var c = new Client(); | ||
c.user = 'scott'; | ||
c.password = 'tiger'; | ||
c.database = 'mydb'; | ||
c.connect(); | ||
// parameter order: err, resource | ||
// new in 1.0.6 | ||
callback(null, c); | ||
}, | ||
destroy : function(client) { client.end(); }, | ||
max : 10, | ||
// optional. if you set this, make sure to drain() (see step 3) | ||
min : 2, | ||
// specifies how long a resource can stay idle in pool before being removed | ||
idleTimeoutMillis : 30000, | ||
// if true, logs via console.log - can also be a function | ||
log : true | ||
}); | ||
```js | ||
// Create a MySQL connection pool with | ||
// a max of 10 connections, a min of 2, and a 30 second max idle time | ||
var poolModule = require('generic-pool'); | ||
var pool = poolModule.Pool({ | ||
name : 'mysql', | ||
create : function(callback) { | ||
var Client = require('mysql').Client; | ||
var c = new Client(); | ||
c.user = 'scott'; | ||
c.password = 'tiger'; | ||
c.database = 'mydb'; | ||
c.connect(); | ||
// parameter order: err, resource | ||
// new in 1.0.6 | ||
callback(null, c); | ||
}, | ||
destroy : function(client) { client.end(); }, | ||
max : 10, | ||
// optional. if you set this, make sure to drain() (see step 3) | ||
min : 2, | ||
// specifies how long a resource can stay idle in pool before being removed | ||
idleTimeoutMillis : 30000, | ||
// if true, logs via console.log - can also be a function | ||
log : true | ||
}); | ||
``` | ||
### Step 2 - Use pool in your code to acquire/release resources | ||
// acquire connection - callback function is called | ||
// once a resource becomes available | ||
pool.acquire(function(err, client) { | ||
if (err) { | ||
// handle error - this is generally the err from your | ||
// factory.create function | ||
} | ||
else { | ||
client.query("select * from foo", [], function() { | ||
// return object back to pool | ||
pool.release(client); | ||
}); | ||
} | ||
}); | ||
```js | ||
// acquire connection - callback function is called | ||
// once a resource becomes available | ||
pool.acquire(function(err, client) { | ||
if (err) { | ||
// handle error - this is generally the err from your | ||
// factory.create function | ||
} | ||
else { | ||
client.query("select * from foo", [], function() { | ||
// return object back to pool | ||
pool.release(client); | ||
}); | ||
} | ||
}); | ||
``` | ||
### Step 3 - Drain pool during shutdown (optional) | ||
@@ -158,9 +165,11 @@ | ||
idle resources have timed out. For example, you can call: | ||
```js | ||
// Only call this once in your application -- at the point you want | ||
// to shutdown and stop using this pool. | ||
pool.drain(function() { | ||
pool.destroyAllNow(); | ||
}); | ||
``` | ||
// Only call this once in your application -- at the point you want | ||
// to shutdown and stop using this pool. | ||
pool.drain(function() { | ||
pool.destroyAllNow(); | ||
}); | ||
If you do this, your node process will exit gracefully. | ||
@@ -208,33 +217,35 @@ | ||
// create pool with priorityRange of 3 | ||
// borrowers can specify a priority 0 to 2 | ||
var pool = poolModule.Pool({ | ||
name : 'mysql', | ||
create : function(callback) { | ||
// do something | ||
}, | ||
destroy : function(client) { | ||
// cleanup. omitted for this example | ||
}, | ||
max : 10, | ||
idleTimeoutMillis : 30000, | ||
priorityRange : 3 | ||
}); | ||
```js | ||
// create pool with priorityRange of 3 | ||
// borrowers can specify a priority 0 to 2 | ||
var pool = poolModule.Pool({ | ||
name : 'mysql', | ||
create : function(callback) { | ||
// do something | ||
}, | ||
destroy : function(client) { | ||
// cleanup. omitted for this example | ||
}, | ||
max : 10, | ||
idleTimeoutMillis : 30000, | ||
priorityRange : 3 | ||
}); | ||
// acquire connection - no priority - will go at end of line | ||
pool.acquire(function(err, client) { | ||
pool.release(client); | ||
}); | ||
// acquire connection - no priority - will go at end of line | ||
pool.acquire(function(err, client) { | ||
pool.release(client); | ||
}); | ||
// acquire connection - high priority - will go into front slot | ||
pool.acquire(function(err, client) { | ||
pool.release(client); | ||
}, 0); | ||
// acquire connection - high priority - will go into front slot | ||
pool.acquire(function(err, client) { | ||
pool.release(client); | ||
}, 0); | ||
// acquire connection - medium priority - will go into middle slot | ||
pool.acquire(function(err, client) { | ||
pool.release(client); | ||
}, 1); | ||
// acquire connection - medium priority - will go into middle slot | ||
pool.acquire(function(err, client) { | ||
pool.release(client); | ||
}, 1); | ||
// etc.. | ||
// etc.. | ||
``` | ||
@@ -247,5 +258,7 @@ ## Draining | ||
pool.drain(function() { | ||
pool.destroyAllNow(); | ||
}); | ||
```js | ||
pool.drain(function() { | ||
pool.destroyAllNow(); | ||
}); | ||
``` | ||
@@ -260,7 +273,9 @@ One side-effect of calling `drain()` is that subsequent calls to `acquire()` | ||
var privateFn, publicFn; | ||
publicFn = pool.pooled(privateFn = function(client, arg, cb) { | ||
// Do something with the client and arg. Client is auto-released when cb is called | ||
cb(null, arg); | ||
}); | ||
```js | ||
var privateFn, publicFn; | ||
publicFn = pool.pooled(privateFn = function(client, arg, cb) { | ||
// Do something with the client and arg. Client is auto-released when cb is called | ||
cb(null, arg); | ||
}); | ||
``` | ||
@@ -271,15 +286,17 @@ Keeping both private and public versions of each function allows for pooled | ||
var privateTop, privateBottom, publicTop, publicBottom; | ||
publicBottom = pool.pooled(privateBottom = function(client, arg, cb) { | ||
//Use client, assumed auto-release | ||
}); | ||
```js | ||
var privateTop, privateBottom, publicTop, publicBottom; | ||
publicBottom = pool.pooled(privateBottom = function(client, arg, cb) { | ||
//Use client, assumed auto-release | ||
}); | ||
publicTop = pool.pooled(privateTop = function(client, cb) { | ||
// e.g., open a database transaction | ||
privateBottom(client, "arg", function(err, retVal) { | ||
if(err) { return cb(err); } | ||
// e.g., close a transaction | ||
cb(); | ||
}); | ||
publicTop = pool.pooled(privateTop = function(client, cb) { | ||
// e.g., open a database transaction | ||
privateBottom(client, "arg", function(err, retVal) { | ||
if(err) { return cb(err); } | ||
// e.g., close a transaction | ||
cb(); | ||
}); | ||
}); | ||
``` | ||
@@ -290,16 +307,17 @@ ## Pool info | ||
// returns factory.name for this pool | ||
pool.getName() | ||
```js | ||
// returns factory.name for this pool | ||
pool.getName() | ||
// returns number of resources in the pool regardless of | ||
// whether they are free or in use | ||
pool.getPoolSize() | ||
// returns number of resources in the pool regardless of | ||
// whether they are free or in use | ||
pool.getPoolSize() | ||
// returns number of unused resources in the pool | ||
pool.availableObjectsCount() | ||
// returns number of unused resources in the pool | ||
pool.availableObjectsCount() | ||
// returns number of callers waiting to acquire a resource | ||
pool.waitingClientsCount() | ||
// returns number of callers waiting to acquire a resource | ||
pool.waitingClientsCount() | ||
``` | ||
## Run Tests | ||
@@ -306,0 +324,0 @@ |
50419
9
1014
344