Core
Write Plugins v1.x
1

You are currently looking at the documentation of a previous version of Kuzzle. We strongly recommend that you use the latest version. You can also use the version selector in the top menu.

Mutex #

Available since 1.12.0

Instantiates a new mutex, that can be used to lock a resource.

Allows to have a process played on only 1 node on a Kuzzle cluster, with the other ones waiting until the lock is freed.

Also works within a single node: in that case, it's better if lock attempts from different parts of the code use different Mutex instances (with the same lock ID).


Constructor #

new context.constructors.Mutex(lockId, [options]);

ArgumentsTypeDescription
lockId
string
Lock unique id (must be identical across all nodes)
options
object
(optional) Mutex options (see below)

options #

The options object is used to configure a mutex behavior. The following properties can be provided:

OptionTypeDescription
attemptDelayNumber(default: 200) Delay in milliseconds between lock attempts
timeoutNumber(default: -1) Mutex lock acquisition timeout, in milliseconds. If set to 0, locking will return immediately, whether it can or cannot lock within the 1st attempt. If set to -1, the mutex will try to acquire the resource indefinitely
ttlNumber(default: 5000) Lock time to live, in milliseconds. Locks will always be freed after that delay has expired.

lock #

Locks the resource.

Returns a promise, resolving to a boolean value.

If a timeout (see constructor options) has been set with a number greather than or equal to 0, then you MUST check the boolean result to verify if the lock has been acquired or not.

If the lock timeout was set to -1, the promise will only be resolved once the lock was successfully acquired, and the boolean result is always true.

Arguments #

This function takes no argument.

lock();

Return #

The lock function resolves to a boolean telling whether the lock was acquired or not.


unlock #

Frees the locked resource, making it available.

Arguments #

This function takes no argument.

unlock();

Return #

Returns a promise, resolved once the mutex has been unlocked.