A mutex/semaphore implementation made easy to use.

Overview

Last version Coverage Status NPM Status

A mutex/semaphore implementation made easy to use.

Why

superlock aims to be:

  • Simple: Designed for usage with async and await
  • Powerful: Mutex & Semaphore patterns supported
  • Secure: Auto lock release toa void dead locks
  • Lightweight: No dependencies, just ~50 LOC
  • Well-tested: 100% code coverage

Install

$ npm install superlock --save

Usage

as mutex

The lock is a mutex by default:

const { withLock } = require('superlock')
const delay = require('delay')

const lock = withLock()

const executions = await Promise.all(
  [...Array(10).keys()].map(index =>
    lock(async () => {
      await delay(Math.random() * 100)
      return index
    })
  )
)

console.log(executions)

as semaphore

Just call withLock(n) being n the maximum of concurrency desired for the lock.

API

withLock([concurrency=1])

It returns a function that can be used to wrap any code you want to execute with concurrency control:

const { withLock } = require('superlock')

const lock = createLock()

await lock(() => {
  /* your code execution */
})

The lock will be automatically released after your code execution is done even if an error occurred, avoiding deadlock situations.

concurrency

Type: number
Default: 1

It sets the maximum of concurrency allowed for the lock.

.isLocked()

Type: boolean

It returns false if there is at least one free concurrency slots in the lock.

License

superlock © Kiko Beats, released under the MIT License.
Authored and maintained by Kiko Beats with help from contributors.

kikobeats.com · GitHub Kiko Beats · Twitter @Kikobeats

You might also like...

Want to preserve your loved one's voices for eternity? Narrate anything using the a voice imprint that can be made in as little as 10 minutes of audio samples. Easy as pie.

Want to preserve your loved one's voices for eternity? Narrate anything using the a voice imprint that can be made in as little as 10 minutes of audio samples. Easy as pie.

Want to preserve your loved one's voices for eternity? Narrate anything using the a voice imprint that can be made in as little as 10 minutes of audio samples. Easy as pie.

Nov 29, 2022

💸 getting sponsored made easy with widgets !

Ponsor getting sponsored made easy through widgets Tech Stack A lot of different tech and soft wares were used to create Ponsor, here is a list of the

Jan 4, 2023

quASAR: ASAR manipulation made easy

quASAR: ASAR manipulation made easy

quASAR quASAR: ASAR manipulation made easy This project is a proof-of-concept for manipulating ASAR files for code injection in Electron apps. This ca

Sep 25, 2022

Mercure Provider - Real-time Made Easy

Mercure Provider - Real-time Made Easy

@setten/mercure is a Mercure client for AdonisJS. Mercure allows you to use Server Sent Events to push data to your clients using Http. Note You must

Nov 29, 2022

End-to-end typesafe APIs in Astro wesbites made easy

End-to-end typesafe APIs in Astro wesbites made easy

Astro x tRPC 🚀 End-to-end typesafe APIs in Astro wesbites made easy View Demo · Report Bug · Request Feature 👋 Introducing astro-trpc astro-trpc is

Dec 30, 2022

🕹 GBA emulator on your React project - easy and powerful to use!

🕹 GBA emulator on your React project - easy and powerful to use!

GBA emulator on your React project - easy and powerful to use! Just three steps to set it up ✨ 1 - Apply GbaProvider import { GbaProvider } from 'reac

Dec 23, 2022

An affordable and easy-to-use monitoring tool for your AWS serverless applications.

An affordable and easy-to-use monitoring tool for your AWS serverless applications.

AWS Serverless Applications Monitoring Tool Table of Contents Motivation for Project Getting Started AWS End Users Installation and Setup Lambda Metri

Sep 21, 2022

Jaxit is an easy-to-use library that makes an interactive terminal for your programs.

Jaxit Jaxit is an easy-to-use library that makes an interactive terminal for your programs. Jaxit was made by Codeverse, so check on Codeverse's Profi

Dec 11, 2022
Releases(v1.0.1)
Owner
Kiko Beats
Software writer.
Kiko Beats
jQuery based scrolling Bar, for PC and Smartphones (touch events). It is modern slim, easy to integrate, easy to use. Tested on Firefox/Chrome/Maxthon/iPhone/Android. Very light <7ko min.js and <1Ko min.css.

Nice-Scrollbar Responsive jQuery based scrolling Bar, for PC and Smartphones (touch events). It is modern slim, easy to integrate, easy to use. Tested

Renan LAVAREC 2 Jan 18, 2022
An easy to implement marquee JQuery plugin with pause on hover support. I know its easy because even I can use it.

Simple-Marquee Copyright (C) 2016 Fabian Valle An easy to implement marquee plugin. I know its easy because even I can use it. Forked from: https://gi

null 16 Aug 29, 2022
A lightweight Nano Node implementation made for wallets, exchanges and other services.

About This is a Light Nano Node implementation made for Wallets, Exchanges and other services. This Node has been built to be compatible with the offi

Nano - Light Net 7 Jun 25, 2022
A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, making it easy and fast.

RESPRESS A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, ma

Yousef Wadi 9 Aug 29, 2022
A quotaless, partially limitless, and fast Node.js Multiplayer Piano server implementation that efficiently makes use of the protocol and uWebSockets.js

speedymppserver A quotaless, partially limitless, and fast Node.js Multiplayer Piano server implementation that efficiently makes use of the protocol

Lapis 4 Oct 14, 2022
Website to present projects made by me and that are part of my personal portfolio. It was made using React, HTML y Scss (CSS).

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Portfolio Jesús Leal 0 Dec 23, 2021
A showcase of problems once hard or impossible to solve with CSS alone, now made trivially easy with Flexbox.

Solved by Flexbox A showcase of problems once hard or impossible to solve with CSS alone, now made trivially easy with Flexbox. View Site Viewing the

Philip Walton 13k Jan 2, 2023
Installing husky made easy as woof!

auto-husky Installing husky made easy as woof! ?? ?? Table of contents ?? Installation ?? Usage ?? Versioning ?? Installation You can install the pack

G.Script 24 Jun 28, 2022
Multiple users using the same leetcode account made easy!

LeetcodeSessionManager About This extension helps is multiple people using the same LeetCode account. It gets tiring when you create sessions(sub-acco

PAWAN JENU 34 Dec 20, 2022
🔑 Permission management made easy

Perman.JS ?? Permission management made easy Get support » Report Bug · Request Feature · Webpage ?? Perman Permission management made easy ?? Install

Barış DEMİRCİ 7 Sep 10, 2022