New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

node-async-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

node-async-semaphore

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

latest
Source
npmnpm
Version
1.0.3
Version published
Maintainers
1
Created
Source

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.

Keywords

async

FAQs

Package last updated on 26 Jan 2024

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