npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-async-semaphore

v1.0.3

Published

A blazing-fast, perfectly typed JavaScript semaphore that manages real resources.

Downloads

80

Readme

node-async-semaphore: Advanced & Fast Semaphore for TypeScript

Node-Async-Semaphore is not just another counting mechanism. It's a highly optimized, TypeScript-based Semaphore solution that leverages a vector of resources instead of a mere permit count, making integration into projects both convenient and efficient. Whether you're designing pooling systems, rate limiters, or merely trying to regulate access, Semaphore has got you covered.

Features

  • 🚀 Performance: Experience optimized performance regardless of scale.
  • 🚦 Rate Limiting: Includes built-in rate limiting capabilities.
  • 💡 TypeScript Support: Written in TypeScript and perfectly typed, ensuring robust development.

Getting Started

Installation

Using npm:

npm install node-async-semaphore

or

Using yarn:

yarn add node-async-semaphore

Usage

Simple Semaphore without resource

import { Semaphore, voidResource } from 'node-async-semaphore';

const counterSemaphore = new Semaphore({
  permits: 3,
  resource: voidResource,
});

async function performConcurrentOperation(index: number) {
  await counterSemaphore.acquire();
  console.log(`Operation ${index} is running`);
  setTimeout(() => {
    console.log(`Finishing operation ${index}`);
    counterSemaphore.release();
  }, 1000);
}

for (let i = 1; i <= 5; i++) {
  performConcurrentOperation(i); // Only 3 operations will run concurrently due to the semaphore.
}

Resource-pooling

import { Semaphore } from 'node-async-semaphore';
import { createClient } from 'promise-redis';

const redisSemaphore = new Semaphore({
  permits: 3,
  resource: () => createClient(),
});

async function runRedisCommand() {
  const client = await redisSemaphore.acquire();
  console.log('Running Redis command...');
  await client.set('key', 'value');
  const result = await client.get('key');
  console.log(`Retrieved value: ${result}`);
  redisSemaphore.release(client);
}

for (let i = 0; i < 5; i++) {
  runRedisCommand();  // Only 3 Redis commands will be active at once due to the semaphore.
}

Rate-limiter

import { SimpleRateLimiter } from 'node-async-semaphore';

const rateLimiter = new SimpleRateLimiter({
  requests: 10,              // Allow 10 tasks every 5 seconds.
  interval: 5000,            // 5 seconds interval.
  uniformDistribution: false // Delay is not spread evenly across requests.
});

async function rateLimitedTask() {
  await rateLimiter.acquire();
  console.log("Task running at", new Date().toISOString());
  // The task logic goes here...
}

for (let i = 0; i < 15; i++) {
  rateLimitedTask();  // Only the first 10 tasks will run immediately. The next 5 will be queued with a delay.
}

License

This code is licensed under the MIT License.