Package Exports
- semaphore
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (semaphore) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
semaphore.js
Limit simultaneous access to a resource.
// Create semaphore
var sem = require('semaphore')(capacity);
// Take semaphore
sem.take(function[, number])
sem.take(number, function)
// Leave semaphore
sem.leave([number])
// Prevent database from dying by only allowing 1 request at a time
var cat = {
};
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);
});
});
});
// Only serve 2 clients at a time.
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.");
});
});