Client library for nodejs-grpc-mutex-api
How to Install
npm i grpc-mutex-client
How to use
First, you need to have a single instance (with no horizontal scaling) of grpc-mutex-api running. Then, obtain an instance of Mutex:
const client = new MutexApiClient('example.com');
Then, just acquire and release an id as you need:
const release = await client.acquire({ id: 'my-mutex-id' });
// Here you do your stuff
await release();
Be aware that the acquire may fail. There is a default 15000 ms timeout to try to acquire a mutex, with 1 attempt. Also, an acquired mutex lasts, by default, for 5 minutes. Which you can also change, like this:
const release = await client.acquire({
id: 'my-mutex-id',
waitTimeout: 30000, // will wait 30 seconds at each attempt of acquiring the mutex,
attempts: 5 // will attempt up to 5 times to acquire the mutex,
mutexTimeout: 60*60*1000 // the mutex will last for an hour if not released
});
// Here you do your stuff
await release();
License
Licensed under MIT.