node-traffic
v1.2.1
Published
A nodejs semaphore implementation.
Downloads
2
Maintainers
Readme
Traffic
A node.js semaphore implementation.
Single node semaphore
Usage
var traffic = require('node-traffic');
traffic.enter(function criticalZone () {
// ...
traffic.leave();
});
Example with web servers
app.post('/resource', function (req, res) {
traffic.enter(function () {
// DB stuff...
traffic.leave();
res.respond();
});
});
Namespaced semaphores
traffic.enter('changeResource', function criticalZone () {
// ...
traffic.leave('changeResource');
});
Example for web servers
app.post('/resource/:id', function (req, res) {
traffic.enter('changeResource-' + req.params.id, function () {
// DB stuff...
traffic.leave('changeResource-' + req.params.id);
res.respond();
});
});
Distributed semaphore
Traffic allows to have a distributed semaphore across multiples nodes using Zookeeper.
Configure
var config = {
zkHost: 'localhost:2181', // host:port
zkRoot: '/', // znode path
zkTimeout: 4000 // 4 seconds
};
var traffic = require('node-traffic').distributed(config);
traffic.on('connectionTimeout', function (err) {
// handle Zookeeper connection timeout
});
zkRoot
will be created if it doesn't exists.
Usage
traffic.enter(function criticalZone () {
// ...
traffic.leave();
});
// Namespaced
traffic.enter('myNamespace', function () {
// ...
traffic.leave('myNamespace');
});
Note: Using namespaces creates znodes under config.zkRoot
.
Don't forget to close the Zookeeper connection...
process.on('exit',function(){
traffic.close();
});
Common patterns
If traffic
fails to connect to Zookeeper you can fallback into normal semaphores.
traffic.on('connectionTimeout', function (err) {
traffic = require('node-traffic');
});
Run tests
Install jasmine
npm install -g jasmine
Run specs
npm test
jasmine spec/semaphore_spec.js
jasmine spec/distributed_semaphore_spec.js