Socket
Book a DemoInstallSign in
Socket

@joao-sada/gcs-mutex-lock

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@joao-sada/gcs-mutex-lock

Simple, scalable, distributed mutex for serialising computations that is backed by GCS

0.0.1
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

gcs-mutex-lock

Simple, scalable, distributed mutex for serialising computations that is backed by GCS.

Installation

yarn add gcs-mutex-lock

Prerequisites

  • You have a project in Google Cloud Platform.
  • You have the Google Cloud SDK installed and configured.
  • The Cloud Storage API in the Google APIs Console is enabled.

Setting up your bucket

  • Create a bucket in which to store your lock file - gsutil mb gs://your-bucket-name.
  • Enable object versioning in your bucket - gsutil versioning set on gs://your-bucket-name.
  • Set a lifetime for lock files in this bucket - gsutil lifecycle set <config-json-file> gs://your-bucket-name. See this README for an explanation on this.
{
  "rule": [
    {
      "action": { "type": "Delete" },
      "condition": { "age": 1 }
    }
  ]
}

Example Usage

import MutexLock from 'gcs-mutex-lock'

const lock = new MutexLock({
  /* @google-cloud/storage options for new Storage() */
  storageOptions: {},
  bucket: 'mutex-locks',
  object: 'my-lock',
})

async function main() {
  const { success, err } = await lock.acquire()

  // In most cases the lock is probably already acquired.
  // The err is available for extra context.
  if (!success) {
    console.error('Failed to acquire lock', err)
    return
  }

  try {
    // serialised computation happens here.
  } finally {
    await lock.release()
  }
}

Custom Timeouts

import MutexLock from 'gcs-mutex-lock'

const lock = new MutexLock({
  // wait up to 1 minute when trying to acquire a lock
  timeout: 60_000,
})

WARNING: Failure to call release will hold the mutex and deadlock the application. Make sure to call release under all circumstances and handle exceptions accordingly.

API

new Mutex(options: Object)

Creates a new Mutex lock. See types for a complete set of options. By default acquire and release will wait indefinitely when trying to acquire or relinquish a lock. For convenience you can configure timeouts, backoff behaviour, retries etc.

mutex.acquire()

Returns a promise that will resolve as soon as the mutex is locked. If the lock is already in use then acquire() will wait until the mutex is available.

mutex.release()

Returns a promise that will resolve as soon as the mutex is unlocked. If the lock doesn't exist upon calling release() an error will be thrown, otherwise release() will wait until the mutex is unlocked.

Acknowledgements

This project is basically a port of Marc Cohen's gcslock library. I recommend reading their project's README for a better understanding of the limitations.

FAQs

Package last updated on 15 Apr 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

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.