Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@20i/distributed-lock

Package Overview
Dependencies
Maintainers
10
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@20i/distributed-lock

Small library to sync time to a time server

  • 1.0.37
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
10
Weekly downloads
 
Created
Source

@20i/distributed-lock

This library talks to our aws distributed lock servers and provides automatic locking/unlocking on functions

import DistributedLock from "@20i/distributed-lock"

async function test() {
    const dl = new DistributedLock("your-project-api-key")
    const result = await dl.acquireLock({
        lockName: "my-lock",
        fn: async () => {
            await doSomeThinkingThatTakesAWhile()
            // during this time, if any other client wants to get 'my-lock', it will wait until this long fn is done
            return 10
        }
    })

    console.log(result) // 10
}

As soon as the function is done, the lock will be removed!

Be warned that locks have an expiration time of 10 seconds by default. However, if you expect that your function will take longer, you can provide a custom expiration time!

import DistributedLock from "@20i/distributed-lock"

async function test() {
    const dl = new DistributedLock("your-project-api-key")
    const result = await dl.acquireLock({
        lockName: "my-lock",
        expirationTimeMs: 20000,    // "my-lock" will now expire 20 seconds after it is created!
        fn: async () => {
            await doSomeThinkingThatTakesAWhile()
            // during this time, if any other client wants to get my-lock, it will wait until this long fn is done
            return 10
        }
    })

    console.log(result) // 10
}

You can also provide a timeout if you do not wish to wait past a certain amount of time.

import DistributedLock from "@20i/distributed-lock"

async function test() {
    const dl = new DistributedLock("your-project-api-key")
    const result = await dl.acquireLock({
        lockName: "my-lock",
        timeout: 5000,  // if someone else is using "my-lock" and we end up waiting for than 5000 ms, we will stop trying!
        fn: async () => {
            await doSomeThinkingThatTakesAWhile()
            // during this time, if any other client wants to get my-lock, it will wait until this long fn is done
            return 10
        }
    })

    console.log(result) // 10
}

FAQs

Package last updated on 20 Mar 2020

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc