🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

redis-semaphore

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redis-semaphore

Distributed mutex and semaphore based on Redis

1.0.4
Source
npm
Version published
Weekly downloads
214K
2.07%
Maintainers
1
Weekly downloads
 
Created
Source

redis-semaphore

NPM version Build status Dependency Status Coverage Status Code Climate Known Vulnerabilities

Mutex and Semaphore implementations based on Redis ready for distributed systems

Features

  • Fail-safe (all actions performed by LUA scripts (atomic))

Usage

Installation

npm install --save redis-semaphore ioredis
# or
yarn add redis-semaphore ioredis

Mutex

See RedisLabs: Locks with timeouts

new Mutex(redisClient, key [, { lockTimeout = 10000, acquireTimeout = 10000, retryInterval = 10, refreshInterval = acquireTimeout * 0.8 }])
  • redisClient - required, configured redis client
  • key - required, key for locking resource (final key in redis: mutex:<key>)
  • timeouts optional
    • lockTimeout - ms, time after mutex will be auto released (expired)
    • acquireTimeout - ms, max timeout for .acquire() call
    • retryInterval - ms, time between acquire attempts if resource locked
    • refreshInterval - ms, auto-refresh interval

Example

const Mutex = require('redis-semaphore').Mutex
const Redis = require('ioredis')

const redisClient = new Redis()

async function doSomething() {
  const mutex = new Mutex(redisClient, 'lockingResource')
  await mutex.acquire()
  // critical code
  await mutex.release()
}

Semaphore

See RedisLabs: Basic counting sempahore

new Semaphore(redisClient, key, maxCount [, { lockTimeout = 10000, acquireTimeout = 10000, retryInterval = 10, refreshInterval = acquireTimeout * 0.8 }])
  • redisClient - required, configured redis client
  • key - required, key for locking resource (final key in redis: semaphore:<key>)
  • maxCount - required, maximum simultaneously resource usage count
  • timeouts optional
    • lockTimeout - ms, time after semaphore will be auto released (expired)
    • acquireTimeout - ms, max timeout for .acquire() call
    • retryInterval - ms, time between acquire attempts if resource locked
    • refreshInterval - ms, auto-refresh interval

Example

const Semaphore = require('redis-semaphore').Semaphore
const Redis = require('ioredis')

const redisClient = new Redis()

async function doSomething() {
  const semaphore = new Semaphore(redisClient, 'lockingResource', 5)
  await semaphore.acquire()
  // maximum 5 simultaneously executions
  await semaphore.release()
}

Fair Semaphore

See RedisLabs: Fair semaphore

new FairSemaphore(redisClient, key, maxCount [, { lockTimeout = 10000, acquireTimeout = 10000, retryInterval = 10, refreshInterval = acquireTimeout * 0.8 }])
  • redisClient - required, configured redis client
  • key - required, key for locking resource (final key in redis: semaphore:<key>)
  • maxCount - required, maximum simultaneously resource usage count
  • timeouts optional
    • lockTimeout - ms, time after semaphore will be auto released (expired)
    • acquireTimeout - ms, max timeout for .acquire() call
    • retryInterval - ms, time between acquire attempts if resource locked
    • refreshInterval - ms, auto-refresh interval

Example

const FairSemaphore = require('redis-semaphore').FairSemaphore
const Redis = require('ioredis')

const redisClient = new Redis()

async function doSomething() {
  const semaphore = new FairSemaphore(redisClient, 'lockingResource', 5)
  await semaphore.acquire()
  // maximum 5 simultaneously executions
  await semaphore.release()
}

License

MIT

Keywords

redis

FAQs

Package last updated on 05 Dec 2018

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