Comparing version 0.0.2 to 0.1.0
@@ -5,3 +5,3 @@ | ||
var codes = require('./codes'); | ||
var pool = require('..')(500); | ||
var pool = require('..')(100); | ||
require('./test'); | ||
@@ -11,8 +11,12 @@ | ||
if (err) throw err; | ||
pool.generator(function(done){ | ||
pool.constructor(function(done){ | ||
p.createPage(done); | ||
}); | ||
pool.populate(100); | ||
pool.populate(50); | ||
}); | ||
pool.destructor(function(page){ | ||
page.close(); | ||
}); | ||
app.get('/', function(req, res){ | ||
@@ -19,0 +23,0 @@ pool.acquire(function(err, page){ |
Running 30s test @ http://localhost:3000/ | ||
12 threads and 100 connections | ||
Thread Stats Avg Stdev Max +/- Stdev | ||
Latency 310.84ms 35.91ms 385.16ms 89.66% | ||
Req/Sec 25.23 4.51 38.00 80.32% | ||
9218 requests in 30.01s, 1.66MB read | ||
Requests/sec: 307.16 | ||
Transfer/sec: 56.69KB | ||
Latency 397.62ms 81.88ms 484.25ms 71.96% | ||
Req/Sec 19.51 4.13 32.00 75.48% | ||
7209 requests in 30.01s, 1.30MB read | ||
Requests/sec: 240.21 | ||
Transfer/sec: 44.34KB |
0.1.0 / 2013-12-31 | ||
================== | ||
* add min(), destruct(), destructor(), closes #1 | ||
0.0.2 / 2013-12-31 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -65,2 +65,16 @@ | ||
/** | ||
* Set `min`. | ||
* | ||
* @param {Number} min | ||
* @return {Pool} | ||
* @api public | ||
*/ | ||
Pool.prototype.min = function(min){ | ||
debug('min %d', min); | ||
this._min = min; | ||
return this; | ||
}; | ||
/** | ||
* Set generator `fn`. | ||
@@ -73,5 +87,6 @@ * | ||
Pool.prototype.constructor = | ||
Pool.prototype.generator = function(fn){ | ||
debug('generator %s', fn.name || '-'); | ||
this._generator = fn; | ||
this._constructor = fn; | ||
return this; | ||
@@ -81,2 +96,16 @@ }; | ||
/** | ||
* Set destructor `fn`. | ||
* | ||
* @param {Function} fn | ||
* @return {Pool} | ||
* @api public | ||
*/ | ||
Pool.prototype.destructor = function(fn){ | ||
debug('destructor %s', fn.name || '-'); | ||
this._destructor = fn; | ||
return this; | ||
}; | ||
/** | ||
* Populate `n` objects with optional `fn` | ||
@@ -96,2 +125,4 @@ * | ||
this.min(n); | ||
while (0 < n--) { | ||
@@ -145,3 +176,3 @@ batch.push(function(done){ | ||
if (this._generator) { | ||
if (this._constructor) { | ||
return this.generate(function(err, item){ | ||
@@ -173,2 +204,3 @@ if (err) return fn(err); | ||
if (this._max <= this.items.length) return this; | ||
if (this._min <= this.items.length) return this.destruct(obj); | ||
this.items.push(obj); | ||
@@ -198,3 +230,3 @@ debug('returned, total items: %d', this.items.length); | ||
/** | ||
* Generate an item `fn(err, generated)`. | ||
* Construct an item `fn(err, generated)`. | ||
* | ||
@@ -206,6 +238,7 @@ * @param {Function} fn | ||
Pool.prototype.construct = | ||
Pool.prototype.generate = function(fn){ | ||
var error = fn || this.emit.bind(this, 'error'); | ||
var fn = fn || function(){}; | ||
var gen = this._generator; | ||
var gen = this._constructor; | ||
var self = this; | ||
@@ -235,1 +268,16 @@ | ||
}; | ||
/** | ||
* Destruct `obj` with `fn(err, obj)`. | ||
* | ||
* @param {Object} obj | ||
* @return {Pool} | ||
* @api private | ||
*/ | ||
Pool.prototype.destruct = function(obj){ | ||
var fn = this._destructor; | ||
debug('destruct obj'); | ||
if (fn) fn(obj); | ||
return this; | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"repo": "yields/apool", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"dependencies": { | ||
@@ -8,0 +8,0 @@ "debug": "~0.7.4", |
# generic-pool | ||
# apool | ||
@@ -9,3 +9,3 @@ generic object pool for node. | ||
```bash | ||
$ npm install generic-pool | ||
$ npm install apool | ||
``` | ||
@@ -23,6 +23,15 @@ | ||
#### #generator(fn) | ||
#### #constructor(fn) | ||
Set the `generator` that the pool will use when it needs more objects. | ||
Set the `constructor` that the pool will use when it needs more objects. | ||
#### #destructor(fn) | ||
Set the `destructor` that will be used to destroy objects. | ||
#### #min(n) | ||
Set the `min` number of objects that will remain in the pool, defaulted to whatever | ||
is passed to `populate()`. | ||
#### #length() | ||
@@ -44,2 +53,9 @@ | ||
##### Events | ||
- `add`, emitted when an object is added. | ||
- `return`, emitted when an object is returned. | ||
- `acquired`, emitted when an object is acquired. | ||
- `populate`, emitted after `populate(n)`. | ||
## Example | ||
@@ -46,0 +62,0 @@ |
@@ -17,5 +17,5 @@ | ||
describe('generator()', function(){ | ||
it('should set generator', function(){ | ||
pool().generator(pool)._generator.should.eql(pool); | ||
describe('constructor()', function(){ | ||
it('should set constructor', function(){ | ||
pool().constructor(pool)._constructor.should.eql(pool); | ||
}) | ||
@@ -92,2 +92,9 @@ }) | ||
it('should set min', function(){ | ||
var p = pool(); | ||
p.generator(function(){ return 1; }); | ||
p.populate(10); | ||
p._min.should.eql(10); | ||
}); | ||
it('should not populate more than max', function(){ | ||
@@ -136,2 +143,21 @@ var p = pool(); | ||
}) | ||
it('should destruct an item when there are more items then needed', function(done){ | ||
var p = pool(); | ||
p.constructor(function(){ return {}; }); | ||
p.destructor(function(obj){ obj.destroyed = true; }); | ||
p.max(2); | ||
p.populate(1); | ||
p.acquire(function(err, a){ | ||
if (err) return done(err); | ||
p.acquire(function(err, b){ | ||
if (err) return done(err); | ||
p.return(a); | ||
p.return(b); | ||
p.items.should.eql([{}]); | ||
b.destroyed.should.be.true; | ||
done(); | ||
}) | ||
}); | ||
}) | ||
}) | ||
@@ -138,0 +164,0 @@ |
14870
473
97