semaphore
    DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/semaphore package

    1.1.0 • Public • Published

    semaphore.js

    Build Status

    Install: npm install semaphore

    Limit simultaneous access to a resource.

    // Create
    var sem = require('semaphore')(capacity);
     
    // Take
    sem.take(fn[, n=1])
    sem.take(n, fn)
     
    // Leave
    sem.leave([n])
     
    // Available
    sem.available([n])
    // Limit concurrent db access
    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);
            });
        });
    });
    // 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.");
            sem.leave();
        });
    });
    // Rate limit
    var sem = require('semaphore')(10);
    var server = require('http').createServer(req, res) {
        sem.take(function() {
            res.end(".");
            
            setTimeout(sem.leave, 500)
        });
    });

    License

    MIT

    Keywords

    none

    Install

    npm i semaphore

    DownloadsWeekly Downloads

    360,727

    Version

    1.1.0

    License

    none

    Last publish

    Collaborators

    • addaleax
    • abrkn