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

@upstash/lock

Package Overview
Dependencies
Maintainers
6
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@upstash/lock

A distributed lock implementation using Upstash Redis

  • 0.1.0
  • npm
  • Socket score

Version published
Weekly downloads
2.2K
decreased by-16.91%
Maintainers
6
Weekly downloads
 
Created
Source

@upstash/lock

Distributed Lock using Upstash Redis
upstash.com

@upstash/lock offers a distributed lock implementation using Upstash Redis.

Quick Start

NPM

npm install @upstash/lock

PNPM

pnpm add @upstash/lock

Bun

bun add @upstash/lock

To create the Redis instance, you can use the Redis.fromEnv() method to use an Upstash Redis instance from environment variables. More options can be found here.

Example Usage

import { Lock } from '@upstash/lock';
import { Redis } from "@upstash/redis";

async function handleOperation() {
  const lock = new Lock({
    id: "unique-lock-id",
    redis: Redis.fromEnv(),
  });

  if (await lock.acquire()) {
    // Perform your critical section that requires mutual exclusion
    await criticalSection();
    await lock.release();
  } else {
    // handle lock acquisition failure
  }
}

API

Lock
new Lock({
  id: string,
  redis: Redis, // ie. Redis.fromEnv(), new Redis({...})
  lease?: number, // default: 10000 ms
  retry?: {
    attempts?: number, // default: 3
    delay?: number, // default: 100 ms
  },
})
Lock#acquire

Attempts to acquire the lock. Returns true if the lock is acquired, false otherwise.

You can pass a config object to override the default lease and retry options.

async acquire(config?: LockAcquireConfig): Promise<boolean>
Lock#release

Attempts to release the lock. Returns true if the lock is released, false otherwise.

async release(): Promise<boolean>
Lock#extend

Attempts to extend the lock lease. Returns true if the lock lease is extended, false otherwise.

async extend(amt: number): Promise<boolean>
Lock#getStatus

Returns whether the lock is ACQUIRED or FREE.

async getStatus(): Promise<LockStatus>
OptionDefault ValueDescription
lease10000The lease duration in milliseconds. After this expires, the lock will be released
retry.attempts3The number of attempts to acquire the lock.
retry.delay100The delay between attempts in milliseconds.

Keywords

FAQs

Package last updated on 23 Nov 2023

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