https://github.com/da440dil/js-locker
Distributed locking on Node.js
https://github.com/da440dil/js-locker
distributed distributed-lock distributed-locking javascript lock locking locks nodejs redis typescript
Last synced: 2 months ago
JSON representation
Distributed locking on Node.js
- Host: GitHub
- URL: https://github.com/da440dil/js-locker
- Owner: da440dil
- License: mit
- Created: 2019-03-10T11:20:08.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-01-04T09:06:16.000Z (over 3 years ago)
- Last Synced: 2025-03-17T22:04:54.662Z (2 months ago)
- Topics: distributed, distributed-lock, distributed-locking, javascript, lock, locking, locks, nodejs, redis, typescript
- Language: TypeScript
- Homepage:
- Size: 147 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# js-locker
[](https://travis-ci.com/da440dil/js-locker)
[](https://coveralls.io/github/da440dil/js-locker?branch=master)Distributed locking using [Redis](https://redis.io/).
Supported Redis clients: [node-redis](https://github.com/NodeRedis/node-redis) v3 and v4, [ioredis](https://github.com/luin/ioredis) v4.
[Example](./examples/example.ts) usage with [node-redis](https://github.com/NodeRedis/node-redis) v4:
```typescript
import { createClient } from 'redis';
import { createLocker } from '@da440dil/js-locker';async function main() {
const client = createClient();
await client.connect();// Create new locker.
const locker = createLocker(client);// Try to apply lock.
const lock = await locker.lock('some-key', 1000);
if (!lock.ok) {
console.log('Failed to apply lock, retry after %dms', lock.ttl);
return client.quit();
}
console.log('Lock applied');// some code here
// Optionally try to extend lock.
const result = await lock.lock(1000);
if (!result.ok) {
console.log('Failed to extend lock, retry after %dms', result.ttl);
return client.quit();
}
console.log('Lock extended');// Try to release lock.
const ok = await lock.unlock();
if (!ok) {
console.log('Failed to release lock');
return client.quit();
}
console.log('Lock released');await client.quit();
}main().catch((err) => {
console.error(err);
process.exit(1);
});
``````
npm run file examples/example.ts
```[Benchmarks](./benchmarks)
```
npm run file benchmarks/benchmark.ts
```