All Projects → binded → advisory-lock

binded / advisory-lock

Licence: other
Distributed locking using PostgreSQL advisory locks (Node.js)

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to advisory-lock

async
Synchronization and asynchronous computation package for Go
Stars: ✭ 104 (+131.11%)
Mutual labels:  promise, lock
run exclusive
⚡🔒 Wait queue for function execution 🔒 ⚡
Stars: ✭ 22 (-51.11%)
Mutual labels:  promise, lock
toxic-decorators
Library of Javascript decorators
Stars: ✭ 26 (-42.22%)
Mutual labels:  lock
mutex
Mutex lock implementation
Stars: ✭ 28 (-37.78%)
Mutual labels:  lock
bitmex-orderbook
The fastest order book implementation for the BitMEX WebSocket API.
Stars: ✭ 73 (+62.22%)
Mutual labels:  promise
queue-promise
A simple, dependency-free library for concurrent promise-based queues. Comes with with concurrency and timeout control.
Stars: ✭ 56 (+24.44%)
Mutual labels:  promise
doasync
Promisify functions and objects immutably
Stars: ✭ 27 (-40%)
Mutual labels:  promise
lifx-http-api
💡 Thin wrapper around the Lifx HTTP API (http://api.developer.lifx.com/)
Stars: ✭ 17 (-62.22%)
Mutual labels:  promise
parallel-dfs-dag
A parallel implementation of DFS for Directed Acyclic Graphs (https://research.nvidia.com/publication/parallel-depth-first-search-directed-acyclic-graphs)
Stars: ✭ 29 (-35.56%)
Mutual labels:  lock
safe
C++11 header only RAII guards for mutexes and locks.
Stars: ✭ 119 (+164.44%)
Mutual labels:  lock
executive
🕴Elegant command execution for Node.
Stars: ✭ 37 (-17.78%)
Mutual labels:  promise
scroll-padlock
🔒 CSS variables-based scrollbars locker, compatible with all reactive frameworks
Stars: ✭ 12 (-73.33%)
Mutual labels:  lock
replace-in-files
Replace text in one or more files or globs.
Stars: ✭ 21 (-53.33%)
Mutual labels:  promise
futura
Asynchronous Swift made easy. The project was made by Miquido. https://www.miquido.com/
Stars: ✭ 34 (-24.44%)
Mutual labels:  promise
purescript-promises
An alternative effect monad for PureScript.
Stars: ✭ 23 (-48.89%)
Mutual labels:  promise
bs-promise-monad
Monadic syntax to work with promise in ReasonML
Stars: ✭ 38 (-15.56%)
Mutual labels:  promise
django-admin-page-lock
Page Lock for Django Admin allows developers to implement customizable locking pages.
Stars: ✭ 13 (-71.11%)
Mutual labels:  lock
node-steamapi
A nice Steam API wrapper for nodejs
Stars: ✭ 112 (+148.89%)
Mutual labels:  promise
android-promise
A Javascript style Promise library for Android JVM
Stars: ✭ 23 (-48.89%)
Mutual labels:  promise
resloader
🎉A image preloaded plugin and can display the loaded image progress bar
Stars: ✭ 20 (-55.56%)
Mutual labels:  promise

advisory-lock

Build Status

Distributed* locking using PostgreSQL advisory locks.

Some use cases:

  • You have a clock process and want to make absolutely sure there will never be more than one process active at any given time.

    This sort of situation can otherwise arise if the clock process is scaled up by accident or during a deployment which keeps the old version running until the new version responds to a health check.

  • Running a database migration at server startup. If your app is scaled, multiple processes will simultaneously try to run the database migration which can lead to problems.

  • Leader election. Let's say you have a web app and want to post a message to Slack every 30 mins containing some statistic (e.g. new registrations in the last 30 mins). You might have 10 processes running but don't want to get 10 identical messages in Slack. You can use this library to elect a "master" process which is responsible for sending the message.

  • etc.

* Your PostgreSQL database being a central point of failure. For a high available distributed lock, have a look at ZooKeeper.

Install

npm install --save advisory-lock

CLI Usage

A withlock command line utility is provided to make to facilitate the common use case of ensuring only one instance of a process is running at any time.

withlock demo

withlock <lockName> [--db <connectionString>] -- <command>

Where <lockName> is the name of the lock, <command> (everything after --) is the command to run exclusively, once the lock is acquired. --db <connectionString> is optional and if not specified, the PG_CONNECTION_STRING environment variable will be used.

Example:

export PG_CONNECTION_STRING="postgres://[email protected]/mydb"
withlock dbmigration -- npm run knex migrate:latest

Usage

advisoryLock(connectionString)

  • connectionString must be a Postgres connection string

Returns a createMutex function.

The createMutex function also exposes a client property that can be used to terminate the database connection if necessary.

PS: Each call to advisoryLock(connectionString) creates a new PostgreSQL connection which is not automatically terminated, so if that is an issue for you, you can use createMutex.client.end() to end the connection when appropriate (e.g. after releasing a lock). This is however typically not needed since usually, advisoryLock() only needs to be called once.

createMutex(lockName)

  • lockName must be a unique identifier for the lock

Returns a mutex object containing the functions listed below. All object methods are really just functions attached to the object and are not bound to this so they can be safely destructured, e.g. const { withLock } = createMutext(lockName).

For a better understanding of what each functions does, see PosgtreSQL's manual.

mutex.withLock(fn)

  • fn Promise returning function or regular function to be executed once the lock is acquired

Like lock() but automatically release the lock after fn() resolves.

Returns a promise which resolves to the value fn resolves to.

Throws an error if the Postgres connection closes unexpectedly.

mutex.tryLock()

Returns a promise which resolves to true if the lock is free and false if the lock is taken. Doesn't "block".

mutex.lock()

Wait until we get exclusive lock.

mutex.unlock()

Release the exclusive lock.

mutex.tryLockShared()

Like tryLock() but for shared lock.

mutex.lockShared()

While held, this blocks any attempt to obtain an exclusive lock. (e.g.: calls to .lock() or .withLock())

mutex.unlockShared()

Release shared lock.

mutex.withLockShared(fn)

Same as withLock() but using a shared lock.

Example

import advisoryLock from 'advisory-lock'
const mutex = advisoryLock('postgres://user:pass@localhost:3475/dbname')('some-lock-name')

const doSomething = () => {
  // doSomething
  return Promise.resolve()
}

mutex
  .withLock(doSomething) // "blocks" until lock is free
  .catch((err) => {
    // this gets executed if the postgres connection closes unexpectedly, etc.
  })
  .then(() => {
    // lock is released now...
  })

// doesn't "block"
mutex.tryLock().then((obtainedLock) => {
  if (obtainedLock) {
    return doSomething().then(() => mutex.unlock())
  } else {
    throw new Error('failed to obtain lock')
  }
})

See ./test for more usage examples.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].