4.0.0 • Published 3 years ago

@prismamedia/ts-distributed-lock-mongodb-adapter v4.0.0

Weekly downloads
29
License
MIT
Repository
github
Last release
3 years ago

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';
});
4.0.0

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.1.6

4 years ago

2.1.8

4 years ago

2.1.7

4 years ago

2.1.5

4 years ago

2.1.4

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.0.0-rc.17

4 years ago

1.0.0-rc.16

4 years ago

1.0.0-rc.15

4 years ago

1.0.0-rc.14

4 years ago

1.0.0-rc.13

4 years ago

1.0.0-rc.12

4 years ago

1.0.0-rc.11

4 years ago

1.0.0-rc.10

4 years ago

1.0.0-rc.9

4 years ago

1.0.0-rc.8

4 years ago

1.0.0-rc.7

4 years ago

1.0.0-rc.6

4 years ago

1.0.0-rc.5

4 years ago

1.0.0-rc.4

4 years ago

1.0.0-rc.3

5 years ago

1.0.0-rc.2

5 years ago

1.0.0-rc.1

5 years ago

0.1.0-alpha.0

5 years ago