@anchan828/nest-redlock 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +123 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018-2022 anchan828
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# @anchan828/nest-redlock
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
This is a [Nest](https://github.com/nestjs/nest) implementation of the redlock algorithm for distributed redis.
|
|
7
|
+
|
|
8
|
+
This package uses [node-redlock](https://github.com/mike-marcacci/node-redlock).
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
$ npm i --save @anchan828/nest-redlock ioredis
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Quick Start
|
|
17
|
+
|
|
18
|
+
### 1. Import module
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { RedisRedlockModule } from "@anchan828/nest-redlock";
|
|
22
|
+
import Redis from "ioredis";
|
|
23
|
+
|
|
24
|
+
@Module({
|
|
25
|
+
imports: [
|
|
26
|
+
RedisRedlockModule.register({
|
|
27
|
+
clients: [new Redis({ host: "localhost" })],
|
|
28
|
+
}),
|
|
29
|
+
],
|
|
30
|
+
})
|
|
31
|
+
export class AppModule {}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### 2. Add `RedisRedlock` decorator
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { RedisRedlock } from "@anchan828/nest-redlock";
|
|
38
|
+
|
|
39
|
+
@Injectable()
|
|
40
|
+
export class ExampleService {
|
|
41
|
+
@RedisRedlock("lock-key")
|
|
42
|
+
public async addComment(projectId: number, comment: string): Promise<void> {}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
This is complete. redlock is working correctly!
|
|
47
|
+
See [node-redlock](https://github.com/mike-marcacci/node-redlock) for more information on redlock.
|
|
48
|
+
|
|
49
|
+
## Define complex resources (lock keys)
|
|
50
|
+
|
|
51
|
+
If you use a constant, it will be locked on all calls. Let's make the range a little smaller.
|
|
52
|
+
|
|
53
|
+
In this example, only certain projects are now locked.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { RedisRedlock } from "@anchan828/nest-redlock";
|
|
57
|
+
|
|
58
|
+
@Injectable()
|
|
59
|
+
export class ExampleService {
|
|
60
|
+
// The arguments define the class object to which the decorator is being added and the method arguments in order.
|
|
61
|
+
@RedisRedlock((target: ExampleService, projectId: number, comment: string) => `projects/${projectId}/comments`)
|
|
62
|
+
public async addComment(projectId: number, comment: string): Promise<void> {}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Of course, you can lock multiple keys.
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
@Injectable()
|
|
70
|
+
export class ExampleService {
|
|
71
|
+
@RedisRedlock((target: ExampleService, projectId: number, args: Array<{ commentId: number; comment: string }>) =>
|
|
72
|
+
args.map((arg) => `projects/${projectId}/comments/${arg.commentId}`),
|
|
73
|
+
)
|
|
74
|
+
public async updateComments(projectId: number, args: Array<{ commentId: number; comment: string }>): Promise<void> {}
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Using Redlock service
|
|
79
|
+
|
|
80
|
+
If you want to use node-redlock as is, use RedisRedlockService.
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { RedisRedlockService } from "@anchan828/nest-redlock";
|
|
84
|
+
|
|
85
|
+
@Injectable()
|
|
86
|
+
export class ExampleService {
|
|
87
|
+
constructor(private readonly redlock: RedisRedlockService) {}
|
|
88
|
+
|
|
89
|
+
public async addComment(projectId: number, comment: string): Promise<void> {
|
|
90
|
+
await this.redlock.using([`projects/${projectId}/comments`], 5000, (signal) => {
|
|
91
|
+
// Do something...
|
|
92
|
+
|
|
93
|
+
if (signal.aborted) {
|
|
94
|
+
throw signal.error;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Troubleshooting
|
|
102
|
+
|
|
103
|
+
### Nest can't resolve dependencies of the XXX. Please make sure that the "@redisRedlockService" property is available in the current context.
|
|
104
|
+
|
|
105
|
+
This is the error output when using the RedisRedlock decorator without importing the RedisRedlockModule.
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { RedisRedlockModule } from "@anchan828/nest-redlock";
|
|
109
|
+
import Redis from "ioredis";
|
|
110
|
+
|
|
111
|
+
@Module({
|
|
112
|
+
imports: [
|
|
113
|
+
RedisRedlockModule.register({
|
|
114
|
+
clients: [new Redis({ host: "localhost" })],
|
|
115
|
+
}),
|
|
116
|
+
],
|
|
117
|
+
})
|
|
118
|
+
export class AppModule {}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
[MIT](LICENSE)
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@anchan828/nest-redlock",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "This is a [Nest](https://github.com/nestjs/nest) implementation of the redlock algorithm for distributed redis locks.",
|
|
5
|
+
"homepage": "https://github.com/anchan828/nest-redlock#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/anchan828/nest-redlock/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/anchan828/nest-redlock.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "anchan828 <anchan828@gmail.com>",
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc -p tsconfig.build.json",
|
|
19
|
+
"format": "prettier --write '**/*.{js,json,yml,yaml,md}'",
|
|
20
|
+
"postinstall": "husky install",
|
|
21
|
+
"lint": "TIMING=1 eslint --ignore-path .eslintignore '**/*.ts'",
|
|
22
|
+
"lint:fix": "npm run lint -- --fix",
|
|
23
|
+
"test": "jest --coverage --runInBand"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@commitlint/cli": "17.0.3",
|
|
27
|
+
"@commitlint/config-conventional": "17.0.3",
|
|
28
|
+
"@nestjs/common": "9.0.11",
|
|
29
|
+
"@nestjs/core": "9.0.11",
|
|
30
|
+
"@nestjs/platform-express": "9.0.11",
|
|
31
|
+
"@nestjs/testing": "9.0.11",
|
|
32
|
+
"@types/jest": "28.1.7",
|
|
33
|
+
"@types/node": "17.0.42",
|
|
34
|
+
"@types/supertest": "2.0.12",
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "5.34.0",
|
|
36
|
+
"@typescript-eslint/parser": "5.34.0",
|
|
37
|
+
"eslint": "8.22.0",
|
|
38
|
+
"eslint-config-prettier": "8.5.0",
|
|
39
|
+
"eslint-plugin-prettier": "4.2.1",
|
|
40
|
+
"husky": "8.0.1",
|
|
41
|
+
"ioredis": "5.2.2",
|
|
42
|
+
"jest": "28.1.3",
|
|
43
|
+
"lint-staged": "13.0.3",
|
|
44
|
+
"prettier": "2.7.1",
|
|
45
|
+
"redlock": "5.0.0-beta.2",
|
|
46
|
+
"reflect-metadata": "0.1.13",
|
|
47
|
+
"rxjs": "7.5.6",
|
|
48
|
+
"supertest": "6.2.4",
|
|
49
|
+
"ts-jest": "28.0.8",
|
|
50
|
+
"ts-node": "10.9.1",
|
|
51
|
+
"typescript": "4.7.4"
|
|
52
|
+
},
|
|
53
|
+
"volta": {
|
|
54
|
+
"node": "18.4.0"
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
58
|
+
}
|
|
59
|
+
}
|