0.2.22 • Published 3 days ago

@anchan828/nest-redlock v0.2.22

Weekly downloads
-
License
MIT
Repository
github
Last release
3 days ago

@anchan828/nest-redlock

npm NPM

This is a Nest implementation of the redlock algorithm for distributed redis locks.

This package uses node-redlock.

Installation

$ npm i --save @anchan828/nest-redlock ioredis

Quick Start

1. Import module

import { RedlockModule } from "@anchan828/nest-redlock";
import Redis from "ioredis";

@Module({
  imports: [
    RedlockModule.register({
      // See https://github.com/mike-marcacci/node-redlock#configuration
      clients: [new Redis({ host: "localhost" })],
      settings: {
        driftFactor: 0.01,
        retryCount: 10,
        retryDelay: 200,
        retryJitter: 200,
        automaticExtensionThreshold: 500,
      },
      // Default duratiuon to use with Redlock decorator
      duration: 1000,
    }),
  ],
})
export class AppModule {}

2. Add Redlock decorator

import { Redlock } from "@anchan828/nest-redlock";

@Injectable()
export class ExampleService {
  @Redlock("lock-key")
  public async addComment(projectId: number, comment: string): Promise<void> {}
}

This is complete. redlock is working correctly! See node-redlock for more information on redlock.

Define complex resources (lock keys)

Using constants causes the same lock key to be used for all calls. Let's reduce the scope a bit more.

In this example, only certain projects are now locked.

import { Redlock } from "@anchan828/nest-redlock";

@Injectable()
export class ExampleService {
  // The arguments define the class object to which the decorator is being added and the method arguments in order.
  @Redlock<ExampleService["addComment"]>(
    (target: ExampleService, projectId: number, comment: string) => `projects/${projectId}/comments`,
  )
  public async addComment(projectId: number, comment: string): Promise<void> {}
}

Of course, you can lock multiple keys.

@Injectable()
export class ExampleService {
  @Redlock<ExampleService["updateComments"]>(
    (target: ExampleService, projectId: number, args: Array<{ commentId: number; comment: string }>) =>
      args.map((arg) => `projects/${projectId}/comments/${arg.commentId}`),
  )
  public async updateComments(projectId: number, args: Array<{ commentId: number; comment: string }>): Promise<void> {}
}

Using Redlock service

If you want to use node-redlock as is, use RedlockService.

import { RedlockService } from "@anchan828/nest-redlock";

@Injectable()
export class ExampleService {
  constructor(private readonly redlock: RedlockService) {}

  public async addComment(projectId: number, comment: string): Promise<void> {
    await this.redlock.using([`projects/${projectId}/comments`], 5000, (signal) => {
      // Do something...

      if (signal.aborted) {
        throw signal.error;
      }
    });
  }
}

Using fake RedlockService

If you do not want to use Redis in your Unit tests, define the fake class as RedlockService.

const app = await Test.createTestingModule({
  providers: [TestService, { provide: RedlockService, useClass: FakeRedlockService }],
}).compile();

Troubleshooting

Nest can't resolve dependencies of the XXX. Please make sure that the "@redlockService" property is available in the current context.

This is the error output when using the Redlock decorator without importing the RedlockModule.

import { RedlockModule } from "@anchan828/nest-redlock";
import Redis from "ioredis";

@Module({
  imports: [
    RedlockModule.register({
      clients: [new Redis({ host: "localhost" })],
    }),
  ],
})
export class AppModule {}

What should I do with Unit tests, I don't want to use Redis.

Use FakeRedlockService class. Register FakeRedlockService with the provider as RedlockService.

const app = await Test.createTestingModule({
  providers: [TestService, { provide: RedlockService, useClass: FakeRedlockService }],
}).compile();

License

MIT

0.2.22

3 days ago

0.2.21

10 days ago

0.2.20

17 days ago

0.2.19

24 days ago

0.2.18

1 month ago

0.2.17

1 month ago

0.2.16

2 months ago

0.2.15

2 months ago

0.2.14

2 months ago

0.2.13

2 months ago

0.2.12

2 months ago

0.2.11

3 months ago

0.2.10

3 months ago

0.2.9

3 months ago

0.2.8

3 months ago

0.2.7

4 months ago

0.2.6

4 months ago

0.2.5

5 months ago

0.2.4

5 months ago

0.2.3

5 months ago

0.2.1

5 months ago

0.2.0

6 months ago

0.2.2

5 months ago

0.1.63

6 months ago

0.1.64

6 months ago

0.1.65

6 months ago

0.1.62

6 months ago

0.1.52

9 months ago

0.1.53

9 months ago

0.1.54

8 months ago

0.1.55

8 months ago

0.1.56

8 months ago

0.1.57

8 months ago

0.1.58

7 months ago

0.1.59

7 months ago

0.1.50

9 months ago

0.1.51

9 months ago

0.1.49

9 months ago

0.1.45

10 months ago

0.1.46

10 months ago

0.1.47

10 months ago

0.1.48

10 months ago

0.1.60

7 months ago

0.1.61

7 months ago

0.1.41

12 months ago

0.1.42

11 months ago

0.1.43

11 months ago

0.1.44

11 months ago

0.1.40

12 months ago

0.1.39

1 year ago

0.1.38

1 year ago

0.1.30

1 year ago

0.1.31

1 year ago

0.1.32

1 year ago

0.1.33

1 year ago

0.1.34

1 year ago

0.1.35

1 year ago

0.1.36

1 year ago

0.1.37

1 year ago

0.1.27

1 year ago

0.1.28

1 year ago

0.1.29

1 year ago

0.1.20

1 year ago

0.1.21

1 year ago

0.1.22

1 year ago

0.1.23

1 year ago

0.1.24

1 year ago

0.1.25

1 year ago

0.1.26

1 year ago

0.1.10

2 years ago

0.1.11

1 year ago

0.1.12

1 year ago

0.1.13

1 year ago

0.1.14

1 year ago

0.1.15

1 year ago

0.1.16

1 year ago

0.1.8

2 years ago

0.1.17

1 year ago

0.1.7

2 years ago

0.1.18

1 year ago

0.1.19

1 year ago

0.1.9

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago