block-sequence
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -11,6 +11,7 @@ var EventEmitter = require('events') | ||
var driver = _.get(config, 'driver') | ||
var prime = _.has(config, 'prime') ? config.prime : true | ||
var size = _.get(config, 'size') || 1000 | ||
var retries = _.get(config, 'retry.limit') || 1000 | ||
var interval = _.get(config, 'retry.interval') || 100 | ||
var template = _.get(config, 'template') && dot.template(config.template) | ||
var template = _.has(config, 'template') && dot.template(config.template) | ||
var padding = _.defaultsDeep(_.get(config, 'padding') || {}, { size: 0, chars: '0' }) | ||
@@ -24,6 +25,5 @@ var queue = async.queue(getId, 1) | ||
setImmediate(function() { | ||
var initOnce = _.once(function init() { | ||
if (!_.has(config, 'driver')) return self.emit('error', new Error('driver is required')) | ||
if (!_.has(config, 'sequence.name')) return self.emit('error', new Error('sequence name is required')) | ||
driver.ensure(config.sequence, function(err, _sequence) { | ||
@@ -36,2 +36,5 @@ if (err) return self.emit('error', err) | ||
function charge() { | ||
@@ -78,2 +81,3 @@ debug('Charging block from sequence: %s with %d ids', sequence.name, size) | ||
this.next = function(cb) { | ||
initOnce() | ||
queue.push(undefined, cb) | ||
@@ -83,2 +87,3 @@ if (queue.paused) self.emit('blocking', sequence || config.sequence) | ||
if (prime) setImmediate(initOnce) | ||
EventEmitter.call(this); | ||
@@ -85,0 +90,0 @@ } |
{ | ||
"name": "block-sequence", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "A sequential id generator, which grabs blocks of ids rather than just one at a time", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -50,3 +50,3 @@ # block-sequence | ||
1. Ids cannot be used to sort records by age (you probably shouldn't do this anyway) | ||
2. When block-sequence initialises it charges it's id blocks immediately. If you node app crashes and restarts repeatedly you will end up burning through ids very quickly. | ||
2. When block-sequence initialises it charges its id blocks immediately. If you node app crashes and restarts repeatedly you will end up burning through ids very quickly. You can disable this behaviour by setting "prime" to false in the block configuration, however doing so will mean the first id request will incur a small delay. Even when setting "prime" to false you may burn through ids very quickly if something your application crashes and restarts repeatedly after the first id request. | ||
@@ -71,2 +71,3 @@ ## How are the sequences stored? | ||
"block": { | ||
"prime": true, | ||
"size": 100, | ||
@@ -94,3 +95,3 @@ "retry": { | ||
``` | ||
The above configuration will configure a block array containing two blocks. The blocks of 100 ids each. When the first block drains completely the array will start drawing from the second block and the first block will be recharged. The recharging process will automatically retry up to 1000 times at 100ms intervals. | ||
The above configuration will prime a block array containing two blocks of 100 ids each. When the first block drains completely the array will start drawing from the second block and the first block will be recharged. The recharging process will automatically retry up to 1000 times at 100ms intervals. | ||
@@ -97,0 +98,0 @@ The padding and [doT.js](http://olado.github.io/doT/index.html) template will cause ```blockArray.next``` to yield an id similar to ```jbs-vac-0000001234-l``` |
@@ -9,5 +9,6 @@ var bsr = require('block-sequence-reference') | ||
var driver | ||
var store = {} | ||
beforeEach(function(done) { | ||
bsr({}, function(err, _driver) { | ||
bsr({ store: store }, function(err, _driver) { | ||
if (err) return done(err) | ||
@@ -37,2 +38,76 @@ driver = _driver | ||
it('should prime blocks by default', function(done) { | ||
var blocked = false | ||
var block = new Block({ | ||
driver: driver, | ||
size: 10, | ||
sequence: { | ||
name: 'block-tests' | ||
} | ||
}) | ||
block.on('ready', function() { | ||
block.next(function(err, id) { | ||
assert.ifError(err) | ||
assert.equal(id, 1) | ||
assert.ok(!blocked) | ||
done() | ||
}) | ||
}).on('blocking', function() { | ||
blocked = true | ||
}) | ||
}) | ||
it('should prime blocks when explicitly enabled', function(done) { | ||
var blocked = false | ||
var block = new Block({ | ||
driver: driver, | ||
prime: true, | ||
size: 10, | ||
sequence: { | ||
name: 'block-tests' | ||
} | ||
}) | ||
block.on('ready', function() { | ||
block.next(function(err, id) { | ||
assert.ifError(err) | ||
assert.equal(id, 1) | ||
assert.ok(!blocked) | ||
done() | ||
}) | ||
}).on('blocking', function() { | ||
blocked = true | ||
}) | ||
}) | ||
it('should not prime blocks when explicitly disabled', function(done) { | ||
var blocked = false | ||
var block = new Block({ | ||
driver: driver, | ||
prime: false, | ||
size: 10, | ||
sequence: { | ||
name: 'block-tests' | ||
} | ||
}) | ||
block.on('ready', function() { | ||
assert.ok(false, 'Block should not have been primed') | ||
}).on('blocking', function() { | ||
blocked = true | ||
}) | ||
setTimeout(function() { | ||
block.removeAllListeners('ready') | ||
block.next(function(err, id) { | ||
assert.ifError(err) | ||
assert.equal(id, 1) | ||
assert.ok(blocked) | ||
done() | ||
}) | ||
}, 200) | ||
}) | ||
it('should provide sequential ids', function(done) { | ||
@@ -174,3 +249,2 @@ var block = new Block({ | ||
} | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21180
443
116