@da440dil/js-locker
TypeScript icon, indicating that this package has built-in type declarations

0.10.0 • Public • Published

js-locker

Build Status Coverage Status

Distributed locking using Redis.

Supported Redis clients: node-redis v3 and v4, ioredis v4.

Example usage with node-redis v4:

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

npm run file benchmarks/benchmark.ts

Dependents (0)

Package Sidebar

Install

npm i @da440dil/js-locker

Weekly Downloads

2

Version

0.10.0

License

MIT

Unpacked Size

10.8 kB

Total Files

15

Last publish

Collaborators

  • da440dil