0.2.48 • Published 9 months ago

@anchan828/nest-redlock v0.2.48

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months 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.48

9 months ago

0.2.47

9 months ago

0.2.46

9 months ago

0.2.45

9 months ago

0.2.44

10 months ago

0.2.41

10 months ago

0.2.40

10 months ago

0.2.43

10 months ago

0.2.42

10 months ago

0.2.27

1 year ago

0.2.26

1 year ago

0.2.25

1 year ago

0.2.39

11 months ago

0.2.30

1 year ago

0.2.38

11 months ago

0.2.37

11 months ago

0.2.36

11 months ago

0.2.35

11 months ago

0.2.34

12 months ago

0.2.33

12 months ago

0.2.32

12 months ago

0.2.31

1 year ago

0.2.29

1 year ago

0.2.28

1 year ago

0.2.24

1 year ago

0.2.23

1 year ago

0.2.22

1 year ago

0.2.21

1 year ago

0.2.20

1 year ago

0.2.19

1 year ago

0.2.18

1 year ago

0.2.17

1 year ago

0.2.16

1 year ago

0.2.15

1 year ago

0.2.14

1 year ago

0.2.13

1 year ago

0.2.12

1 year ago

0.2.11

1 year ago

0.2.10

1 year ago

0.2.9

1 year ago

0.2.8

1 year ago

0.2.7

1 year ago

0.2.6

1 year ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.2.2

2 years ago

0.1.63

2 years ago

0.1.64

2 years ago

0.1.65

2 years ago

0.1.62

2 years ago

0.1.52

2 years ago

0.1.53

2 years ago

0.1.54

2 years ago

0.1.55

2 years ago

0.1.56

2 years ago

0.1.57

2 years ago

0.1.58

2 years ago

0.1.59

2 years ago

0.1.50

2 years ago

0.1.51

2 years ago

0.1.49

2 years ago

0.1.45

2 years ago

0.1.46

2 years ago

0.1.47

2 years ago

0.1.48

2 years ago

0.1.60

2 years ago

0.1.61

2 years ago

0.1.41

2 years ago

0.1.42

2 years ago

0.1.43

2 years ago

0.1.44

2 years ago

0.1.40

2 years ago

0.1.39

2 years ago

0.1.38

2 years ago

0.1.30

2 years ago

0.1.31

2 years ago

0.1.32

2 years ago

0.1.33

2 years ago

0.1.34

2 years ago

0.1.35

2 years ago

0.1.36

2 years ago

0.1.37

2 years ago

0.1.27

2 years ago

0.1.28

2 years ago

0.1.29

2 years ago

0.1.20

3 years ago

0.1.21

3 years ago

0.1.22

2 years ago

0.1.23

2 years ago

0.1.24

2 years ago

0.1.25

2 years ago

0.1.26

2 years ago

0.1.10

3 years ago

0.1.11

3 years ago

0.1.12

3 years ago

0.1.13

3 years ago

0.1.14

3 years ago

0.1.15

3 years ago

0.1.16

3 years ago

0.1.8

3 years ago

0.1.17

3 years ago

0.1.7

3 years ago

0.1.18

3 years ago

0.1.19

3 years ago

0.1.9

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago