@lunarade/locki
v1.0.13
Published
A redis-backed lock broker that manages groups of S/X locks with atomic lua scripts.
Downloads
271
Maintainers
Readme
locki
A redis-backed lock broker that manages groups of shared and/or exclusive locks with atomic lua scripts.
Due to the fact that lua scripts are blocking, you should use a dedicated redis DB for locki.
Installation:
npm i @lunarade/locki
Usage
import { LockiClient } from '@lunarade/locki';
(async () => {
const client = await LockiClient.create();
await client.connect();
const lockOptions = {
// Shared locks. If we get these, then others can't get exclusivity
shared: ['example', 'path', 'segments', 'to'],
// Exclusive locks. If we get these, no-one can get them
exclusive: ['thing'],
// How often to renew the lock until the async function resolves. Default: 2e3
renewIntervalMs: 2e3,
// Lock TTL to set on every renew. Default: 30e3
ttlMs: 30e3,
endCallback: () => {
// Will be called on end, or when the lock can no longer
// be guaranteed after we get it (e.g. due to network issues)
}
};
await client.withLocks(async (isLockOwned) => {
// We now own our locks and we can perform asynchronous work
// ...
// When it is time to commit our work (e.g. a transaction)
// we can check if we still have the lock (or we can interrupt
// earlier using endCallback)
if (isLockOwned()) {
// session.commitTransaction();
}
}, lockOptions);
})();