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

gcs-mutex-lock

v2.1.0

Published

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

Downloads

18

Readme

gcs-mutex-lock

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

Installation

yarn add gcs-mutex-lock

Prerequisites

Setting up your bucket

  1. Create a bucket in which to store your lock file - gsutil mb gs://your-bucket-name.
  2. Enable object versioning in your bucket - gsutil versioning set on gs://your-bucket-name.
  3. 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.