Package Exports
- redis-semaphore
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (redis-semaphore) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
redis-semaphore
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
new Mutex(redisClient, key [, { lockTimeout = 10000, acquireTimeout = 10000, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }])
redisClient
- required, configuredredis
clientkey
- required, key for locking resource (final key in redis:mutex:<key>
)timeouts
optionallockTimeout
- ms, time after mutex will be auto released (expired)acquireTimeout
- ms, max timeout for.acquire()
callretryInterval
- ms, time between acquire attempts if resource lockedrefreshInterval
- ms, auto-refresh interval
Example
const Mutex = require('redis-semaphore').Mutex
const Redis = require('ioredis')
// TypeScript
// import { Mutex } from 'redis-semaphore'
// import Redis from 'ioredis'
const redisClient = new Redis()
async function doSomething() {
const mutex = new Mutex(redisClient, 'lockingResource')
await mutex.acquire()
// critical code
await mutex.release()
}
Semaphore
new Semaphore(redisClient, key, maxCount [, { lockTimeout = 10000, acquireTimeout = 10000, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }])
redisClient
- required, configuredredis
clientkey
- required, key for locking resource (final key in redis:semaphore:<key>
)maxCount
- required, maximum simultaneously resource usage counttimeouts
optionallockTimeout
- ms, time after semaphore will be auto released (expired)acquireTimeout
- ms, max timeout for.acquire()
callretryInterval
- ms, time between acquire attempts if resource lockedrefreshInterval
- ms, auto-refresh interval
Example
const Semaphore = require('redis-semaphore').Semaphore
const Redis = require('ioredis')
// TypeScript
// import { Semaphore } from 'redis-semaphore'
// import Redis from '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
new FairSemaphore(redisClient, key, maxCount [, { lockTimeout = 10000, acquireTimeout = 10000, retryInterval = 10, refreshInterval = lockTimeout * 0.8 }])
redisClient
- required, configuredredis
clientkey
- required, key for locking resource (final key in redis:semaphore:<key>
)maxCount
- required, maximum simultaneously resource usage counttimeouts
optionallockTimeout
- ms, time after semaphore will be auto released (expired)acquireTimeout
- ms, max timeout for.acquire()
callretryInterval
- ms, time between acquire attempts if resource lockedrefreshInterval
- ms, auto-refresh interval
Example
const FairSemaphore = require('redis-semaphore').FairSemaphore
const Redis = require('ioredis')
// TypeScript
// import { FairSemaphore } from 'redis-semaphore'
// import Redis from '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