🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

fair-semaphore

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fair-semaphore

A semaphore that unlocks waiters in a fair manner, by key, to allow balancing between different consumers

1.0.3
latest
Source
npm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

A fair semaphore

Build Status NPM version Dependency Status

With this module, you can block work from multiple incoming sources and the work will be unblocked in a fair order. One source cannot cause starvation of the other sources.

A fair queue could be used to schedule incoming requests from users, to make sure that no user reduces the performance of other users on the same server. Or, the queue could be used to balance outgoing requests to a downstream server, making sure that one user does not consume all of the connections in a pool.

A simple example

In this example, source 1 will request two items of work before source 2 can try to request anything. But, when the work is unblocked, the work from source 2 will be unlocked first so that source 1 does not starve source 2.

const FairSemaphore = require('fair-semaphore');
const semaphore = new FairSemaphore();

semaphore.take('source1', function () {
  console.log('Inside first request from source 1');
  semaphore.leave();
});

semaphore.take('source1', function () {
  console.log('Inside second request from source 1');
  semaphore.leave();
});

semaphore.take('source2', function () {
  console.log('Inside first request from source 2');
  semaphore.leave();
});

When the above code is executed, the output will be

Inside first request from source 1
Inside first request from source 2
Inside second request from source 1

Notice that the request for source 2 was allowed before the second request from source 1, so that source 1's double requests did not starve source 2.

Keywords

semaphore

FAQs

Package last updated on 07 Oct 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts