1.1.3 • Published 12 days ago

superlock v1.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
12 days ago

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 { setTimeout } = require('timers/promises')
const { withLock } = require('superlock')

const lock = withLock()

const executions = await Promise.all(
  [...Array(10).keys()].map(index =>
    lock(async () => {
      await setTimeout(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 = withLock()

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

1.1.3

12 days ago

1.1.2

18 days ago

1.1.1

25 days ago

1.1.0

1 month ago

1.0.8

1 month ago

1.0.7

3 months ago

1.0.6

4 months ago

1.0.5

7 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

2 years ago

0.0.1

2 years ago

0.0.0

2 years ago