Skip to content

Class: FileMutexDriver ​

The file driver ({type: 'file'}): each lock is a file of a directory, created exclusively, shared by the processes of one host using the same directory and prefix (several workers or applications of a machine). It needs no package. The processes must share the directory: the default one (lakutata-mutex in the operating system's temporary directory) can differ between users. A directory shared by several hosts (a network file system) works only as far as its exclusive creation and renames are atomic, and only the expiration then frees the lock of a crashed host.

The locks expire after their ttl unless renewed; a lock whose process is gone on this host is taken over at once, without waiting for its expiration. The driver gives no fencing token (fence is undefined).

The file of a lock is created only where none exists, and it is missing only once its holder released it: a renewal and a takeover replace it at once (a new file renamed over it). The renewals, the takeovers and the releases of a lock run one at a time, each in the guard of the lock: a file created exclusively next to it for the few operations they take.

Example ​

typescript
import {FileMutexDriver, type DriverLock} from 'lakutata/com/mutex'

//The workers of this machine run the migration one at a time
const driver: FileMutexDriver = new FileMutexDriver('/var/lock/my-app', 'my-app:')
const lock: DriverLock | undefined = await driver.acquire('migrate', 60000)
if (lock) {
    try {
        console.log('migrating')
    } finally {
        await driver.release('migrate', lock)
    }
}

Extends ​

Constructors ​

Constructor ​

new FileMutexDriver(directory, prefix?): FileMutexDriver

Create a driver on a directory, created at the first acquisition when it does not exist.

Parameters ​

directory ​

string

The directory of the lock files.

prefix? ​

string

The prefix added to the lock names; '' by default.

Returns ​

FileMutexDriver

Overrides ​

MutexDriver.constructor

Properties ​

expiring ​

readonly expiring: boolean

true: the locks expire after their ttl unless renewed.

Overrides ​

MutexDriver.expiring


onLost ​

onLost: (names) => void

Called with the names of the locks the driver lost at once, when the connection holding advisory locks closes or fails. A Mutex sets it; it does nothing by default.

Parameters ​

names ​

string[]

Returns ​

void

Inherited from ​

MutexDriver.onLost

Methods ​

acquire() ​

acquire(name, ttl): Promise<DriverLock | undefined>

Try to acquire a lock once: create its file exclusively, or take over a file expired, abandoned, or whose process of this host is gone. A takeover happens in the guard of the lock (another process taking it over at the same time gives up), and replaces the file at once while it is still the one read: another process never finds it missing.

Parameters ​

name ​

string

The lock name.

ttl ​

number

How long the lock stays held without a renewal, in milliseconds.

Returns ​

Promise<DriverLock | undefined>

The lock (without fencing token), or undefined when another holder has it.

Throws ​

When the directory cannot be created or the file written (permissions, disk full), or when a file taken over cannot be replaced (busy on Windows for a third of ttl, 2.5 seconds at most).

Overrides ​

MutexDriver.acquire


close() ​

close(): Promise<void>

Do nothing: the driver keeps no file open; the lock files stay until they are released or taken over.

Returns ​

Promise<void>

Overrides ​

MutexDriver.close


extend() ​

extend(name, lock, ttl): Promise<boolean>

Renew a lock whose file still holds this holder's token, for ttl milliseconds from now: the new content is written to a file of its own, then renamed over the lock file, which is replaced at once and never missing (another process cannot create it meanwhile). The renewal waits for the guard of the lock: a lock released meanwhile is not written again, and a lock is not taken over between its check and its replacement. A lock expired is not renewed (lost), even when no other process took it over yet. On Windows, a rename over a file another process is reading is tried again, for a third of ttl at most (the pace of the component's renewals) and 2.5 seconds (the guard is broken after 5).

Parameters ​

name ​

string

The lock name.

lock ​

DriverLock

The lock acquired.

ttl ​

number

How long the lock stays held without another renewal, in milliseconds.

Returns ​

Promise<boolean>

Whether it was renewed.

Throws ​

When the new content cannot be written or renamed over the lock file (the component tries again until the lock expires).

Overrides ​

MutexDriver.extend


release() ​

release(name, lock): Promise<boolean>

Release a lock by removing its file, when the file still holds this holder's token. The release waits for the guard of the lock, so that the file it removes is not replaced meanwhile.

Parameters ​

name ​

string

The lock name.

lock ​

DriverLock

The lock acquired.

Returns ​

Promise<boolean>

Whether it was released.

Overrides ​

MutexDriver.release

Released under the MIT License.