1.1.5 • Published 11 months ago

@trellisorg/distributed-lock v1.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

@trellisorg/distributed-lock

This library provides a simple way to implement distributed locks using Redis. It is designed to be easy to use and integrate with your existing Node.js applications.

Installation

npm install @trellisorg/distributed-lock ioredis

Setup

Standalone TypeScript

import { RedisMutex } from '@trellisorg/distributed-lock/redis-mutex';
import Redis from 'ioredis';

const redisClient = new Redis({
    host: 'localhost',
    port: 6379,
});

const redisMutex = new RedisMutex({
    client: redisClient,
    lockPrefix: 'my-app',
    lockTimeout: 10_000,
    fifo: false,
});

// Acquire a lock on the resource 'my-resource'
const lock = await redisMutex.lock('my-resource');

// Do some work that requires the lock
console.log('Working with the resource...');

// Release the lock
await lock.unlock();

NestJS

import { Module } from '@nestjs/common';
import { DistributedLockModule } from '@trellisorg/distributed-lock/nest';
import { redisMutexLockAdapter } from '@trellisorg/distributed-lock/redis-mutex';

@Module({
    imports: [
        DistributedLockModule.register([
            {
                config: {
                    lockPrefix: 'my-app',
                    client: {
                        host: 'localhost',
                        port: 6379,
                    },
                    retryOptions: {},
                    lockTimeout: 10_000,
                    fifo: false,
                },
                adapter: redisMutexLockAdapter,
                name: 'redis',
            },
        ]),
    ],
})
export class AppModule {}

Configuration Options

OptionTypeDefaultDescription
clientRedisOptions-The connection options for Redis. See the ioredis documentation for more information.
lockPrefixstring@trellisorg/distributed-lockA prefix for the lock keys. This helps to avoid collisions with other applications using Redis.
lockTimeoutnumber10_000The time it takes for a lock to expire after acquisition. This is in milliseconds.
retryOptionsRetryOptions{}Options for retrying lock acquisition. See the promise-retry documentation for more information.
fifobooleanfalseWhether to use FIFO ordering for lock acquisition. If true, locks will be acquired and released in the order they were requested.

Usage

Standalone TypeScript

import { RedisMutex } from '@trellisorg/distributed-lock/redis-mutex';
import Redis from 'ioredis';

const redisClient = new Redis({
    host: 'localhost',
    port: 6379,
});

const redisMutex = new RedisMutex({
    client: redisClient,
    lockPrefix: 'my-app',
    lockTimeout: 10_000,
    fifo: false,
});

// Acquire a lock on the resource 'my-resource'
const lock = await redisMutex.lock('my-resource');

// Do some work that requires the lock
console.log('Working with the resource...');

// Release the lock
await lock.unlock();

NestJS

import { Injectable } from '@nestjs/common';
import { DistributedLock } from '@trellisorg/distributed-lock';
import { InjectLock } from '@trellisorg/distributed-lock/nest';

@Injectable()
export class MyService {
    constructor(@InjectLock('redis') private readonly redisLock: DistributedLock) {}

    async doSomething() {
        await this.redisLock.withLock('my-resource', async () => {
            // Do some work that requires the lock
            console.log('Working with the resource...');
        });
    }
}

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the MIT License.

1.1.1

11 months ago

1.1.0

11 months ago

1.1.5

11 months ago

1.1.4

11 months ago

1.1.2

11 months ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago