Skip to content

prismamedia/ts-distributed-lock

Repository files navigation

Typescript distributed lock

npm version github actions status code style: prettier

Provide an easy-to-use "Readers-writers lock" https://en.wikipedia.org/wiki/Readers–writer_lock

An RW lock allows concurrent access for read-only operations, while write operations require exclusive access. This means that multiple threads can read the data in parallel but an exclusive lock is needed for writing or modifying data. When a writer is writing the data, all other writers or readers will be blocked until the writer is finished writing.

Configuration

// ./locker.ts
import { Locker } from '@prismamedia/ts-distributed-lock';
import { MongoDBAdapter } from '@prismamedia/ts-distributed-lock-mongodb-adapter';

const adapter = new MongoDBAdapter('mongodb://localhost:27017/my-database');

export const locker = new Locker(adapter);

Setup

The adapter may needs some setup before use

// ./setup.ts
import { locker } from './locker';

await locker.setup();

Usage

// ./usage.ts
import { locker } from './locker';

const firstLock = await locker.lockAsReader('my-lock-name');
try {
  // Everything I have to do ...
} finally {
  await locker.release(firstLock);
}

const secondLock = await locker.lockAsWriter('my-lock-name');
try {
  // Everything I have to do ...
} finally {
  await locker.release(secondLock);
}

Or with some helpers that ensure the lock is released

// ./usage.ts
import { locker } from './locker';

const firstTaskResult = await locker.ensureReadingTaskConcurrency(
  'my-lock-name',
  async () => {
    // Everything I have to do ...

    return 'myFirstTaskResult';
  },
);

const secondTaskResult = await locker.ensureWritingTaskConcurrency(
  'my-lock-name',
  async () => {
    // Everything I have to do ...

    return 'mySecondTaskResult';
  },
);