Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vercel/redis-semaphore
Distributed mutex and semaphore based on Redis
https://github.com/vercel/redis-semaphore
Last synced: 3 months ago
JSON representation
Distributed mutex and semaphore based on Redis
- Host: GitHub
- URL: https://github.com/vercel/redis-semaphore
- Owner: vercel
- License: mit
- Fork: true (swarthy/redis-semaphore)
- Created: 2018-02-06T20:00:41.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-08-11T17:29:13.000Z (over 6 years ago)
- Last Synced: 2024-09-18T03:38:11.669Z (4 months ago)
- Language: JavaScript
- Size: 54.7 KB
- Stars: 9
- Watchers: 10
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# redis-semaphore
[![NPM version][npm-image]][npm-url]
[![Build status][ci-image]][ci-url]
[![Dependency Status][daviddm-image]][daviddm-url]
[![Coverage Status][coverage-image]][coverage-url]
[![Code Climate][codeclimate-image]][codeclimate-url]
[![Known Vulnerabilities][snyk-image]][snyk-url][Mutex]() and [Semaphore]() implementations based on [Redis](https://redis.io/) ready for distributed systems
## Features
* Fail-safe (all actions performed by LUA scripts (atomic))
## Usage
### Mutex
##### 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:`)
* `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
```javascript
const Mutex = require('redis-semaphore').Mutex
const redis = require('redis')const redisClient = redis.createClient()
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 = acquireTimeout * 0.8 }])
* `redisClient` - **required**, configured `redis` client
* `key` - **required**, key for locking resource (final key in redis: `semaphore:`)
* `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
```javascript
const Semaphore = require('redis-semaphore').Semaphore
const redis = require('redis')const redisClient = redis.createClient()
async function doSomething() {
const semaphore = new Semaphore(redisClient, 'lockingResource', 5)
await semaphore.acquire()
// maximum 5 simultaneously executions
await semaphore.release()
}
```## Installation
```bash
npm install --save redis-semaphore
# or
yarn add redis-semaphore
```## License
MIT
[npm-image]: https://img.shields.io/npm/v/redis-semaphore.svg?style=flat-square
[npm-url]: https://npmjs.org/package/redis-semaphore
[ci-image]: https://img.shields.io/travis/swarthy/redis-semaphore/master.svg?style=flat-square
[ci-url]: https://travis-ci.org/swarthy/redis-semaphore
[daviddm-image]: http://img.shields.io/david/swarthy/redis-semaphore.svg?style=flat-square
[daviddm-url]: https://david-dm.org/swarthy/redis-semaphore
[codeclimate-image]: https://img.shields.io/codeclimate/github/swarthy/redis-semaphore.svg?style=flat-square
[codeclimate-url]: https://codeclimate.com/github/swarthy/redis-semaphore
[snyk-image]: https://snyk.io/test/npm/redis-semaphore/badge.svg
[snyk-url]: https://snyk.io/test/npm/redis-semaphore
[coverage-image]: https://coveralls.io/repos/github/swarthy/redis-semaphore/badge.svg?branch=master
[coverage-url]: https://coveralls.io/r/swarthy/redis-semaphore?branch=master