semaphore_with_arguments.js
Install:
npm install semaphore_with_arguments
Limit simultaneous access to a resource.
var sem = require('semaphore')(capacity);
sem.take(fn[, n=1])
sem.take(n, fn)
sem.leave([n])
var sem = require('semaphore')(1);
var server = require('http').createServer(req, res) {
sem.take(function() {
expensive_database_operation(function(err, res) {
sem.leave();
if (err) return res.end("Error");
return res.end(res);
});
});
});
var sem = require('semaphore')(2);
var server = require('http').createServer(req, res) {
res.write("Then good day, madam!");
sem.take(function() {
res.end("We hope to see you soon for tea.");
sem.leave();
});
});
var sem = require('semaphore')(10);
var server = require('http').createServer(req, res) {
sem.take(function() {
res.end(".");
setTimeout(sem.leave, 500)
});
});
var sem = require('semaphore')(10);
var server = require('http').createServer(req, res) {
var passMe = "String to pass";
sem.take({ aFabulousString: passMe }, function(args) {
res.end(".");
console.log(args.aFabulousString);
setTimeout(sem.leave, 500)
});
});
License
MIT