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

async-resource-semaphore

v2.0.1

Published

an implementation of semaphore for javascript

Downloads

130

Readme

npm version

Async Resource Semaphore

A typed semaphore implementation for javascript

JavaScript uses event loop, and with synchronous code, it is confident that two adjacent statements can be executed without context switching.

However, when dealing with promises and asynchronous code, this is no longer the case. When you have an async function that contains other promises or produces multiple events, you create an opportunity for JavaScript to do context switching. See the example below:

function main() {
  let data: undefine | Data;
  async function criticalSection() {
    if (data !== undefine) return;
    const res = await axios.post("/tickets");
    data = res.data;
  }

  void criticalSection(); // event B
  void criticalSection(); // event C
}

main(); // event A

In this case, the axios.post will be called twice.

  1. The main() function call adds event A to the event loop.
  2. JavaScript executes event A and produces events B and C.
  3. Event B will be executed next after event A finishes. Event B will await axios.post(), allowing JavaScript to execute the next event, which is event C.
  4. Event C will await at axios.post(), and JavaScript switches back to event B, assuming 5. axios response is done.
  5. Event B finishes executing, sets data.
  6. Event C finishes executing, sets data.

This lightweight JavaScript library aims to resolve this issue by creating a critical section that can only be accessed by a set amount of logical chains of events at a given time.

Usage

Interface

class Semaphore {
  constructor(storageOption: SemaphoreDataStorage): Semaphore;
  run<T>: (criticalSection: () => Promise<T>, identifier?: string): Promise<T>;
  take: (identifier?: string): Promise<void>;
  give: (identifier?: string): void;
}

interface SemaphoreDataStorage {
  getLimit: (identifier: string) => number;
  setLimit: (identifier: string, limit: number) => void;
}

Basic Usage

Default resource counter is set to 1. Only one logical chain of events can access the critical section.

import { AsyncSemaphore, SemaphoreDataMemeoryStorage } from "async-resource-semaphore";

const semaphore = new AsyncSemaphore(new SemaphoreDataMemeoryStorage()); // defaut resource counter is 1
const criticalSection = async () => {
  // criticial section
  // ...
};
void semaphore.run(criticalSection);
void semaphore.run(criticalSection); // this event will not excute untill the first one exit

You can use semaphore's take and give

import { AsyncSemaphore } from "async-resource-semaphore";

const semaphore = new AsyncSemaphore(new SemaphoreDataMemeoryStorage()); // defaut resource counter is 1
const criticalSection = async () => {
  await semaphore.take();
  // criticial section
  // ...
  semaphore.give();
};

void criticalSection();
void criticalSection();

When use semaphore's take and give, it is recommended that you use try and catch to handle error inside the critical section to alway return the resource.

If you use semaphore.run() then you dont need to worry about it. This is why run is prefered.

// remember to catch error inbetween take and give so it wont block when there is failure
await semaphore.take();
try {
  // criticial section
  // ...
} catch (_) {
  // handle errors
} finally {
  semaphore.give();
}

// or simply use the run()
await semaphore.run(async () => {
  // critical section
});

Initialize a Semaphore with a Custom Resource Limit

import { AsyncSemaphore } from "async-resource-semaphore";

const semaphore = new AsyncSemaphore(new SemaphoreDataMemeoryStorage(2)); // critical section can be accessed by at most two logical chain of events
const criticalSection = async () => {
  // criticial section
  // ...
};
void semaphore.run(criticalSection);
void semaphore.run(criticalSection); // this event can excute right away

Get the Resolved Response from criticalSection()

import { AsyncSemaphore } from "async-resource-semaphore";

const semaphore = new AsyncSemaphore(new SemaphoreDataMemeoryStorage());
const res = await semaphore.run(async () => "response");

Use Identifier on One Semaphore to Have Multiple Critical Sections with Their Own Resource Limit

import { AsyncSemaphore } from "async-resource-semaphore";

const semaphore = new AsyncSemaphore(new SemaphoreDataMemeoryStorage());
void semaphore.run(async () => await axios.post("/tickets", "tickets")); // A
void semaphore.run(async () => await axios.post("/replies", "replies")); // B
void semaphore.run(async () => await axios.post("/replies", "replies")); // C - this one will have to wait for B to exit

License

MIT License

Copyright (c) 2024 Gengyuan Huang

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.