datastore-lock
A lightweight implementation of the distributed lock which is based on Google Datastore.
Where to use?
Mainly useful for the application level distributed locks.
Example Scenario: you have a an application which sends an email based on an external event. If your application is running multiple instances of it and external event triggers an action in all the instances, you might be required to use distributed locks to ensure that NOT all instances end up sending the email and only one mail is being sent.
What does it offer?
It allows an application to acquire lock for a leased period of time (by default 1 minute, but can be configured using options
) which will prevent other application instance who complete for it from acquiring the lock, till the first application either releases the lock or leased time gets expired.
How to use?
- Download via npm
npm install datastore-lock
. - Instantiate
const dslock = options;
- acquire lock
dslock;
- release the lock once done
dslock;
Options
while instantiation you can pass options to configure the datastore-lock
const dslock = options
options
object can have following values:
projectId
- (required ifdsClient
is not set) google project id which will be used to connect to Google DatastoreleaseFor
- (optional) max number of seconds the lock would be available for, after this time runs out and lock is not released, it will automatically get expired. Default value is 60.dsClient
- (required ifprojectId
is not set) an instance of Datastore client as described here.keyFilename
- (optional) file path of the Google service account credentials, alternatively you can setGOOGLE_APPLICATION_CREDENTIALS
env variable.lockKeyName
- (optional) name of the key which will be used while creating the lock in the Datastore. Useful in cases where you want more than one locks in the same application for different functionalities. Default value if lockKeyName.namespace
- (optional) Google Datastore namespace to create and store the lock. Default value is dslock.