@nathapp/nestjs-redis 3.2.1 → 3.3.0

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.
@@ -1,15 +1,15 @@
1
1
  import { Logger, OnApplicationShutdown } from "@nestjs/common";
2
- import Redlock from "redlock";
2
+ import Redlock, { Lock } from "redlock";
3
3
  import { RedisService } from "./redis.service";
4
4
  import { LockSettings } from "./lock.interface";
5
5
  export declare class RedisLockService implements OnApplicationShutdown {
6
6
  private readonly redisService;
7
7
  protected readonly logger: Logger;
8
8
  protected readonly redLock: Redlock;
9
- protected readonly locks: {};
9
+ protected readonly locks: Map<string, Lock>;
10
10
  constructor(redisService: RedisService, lockSettings?: LockSettings);
11
11
  onApplicationShutdown(signal?: string): Promise<void>;
12
- acquireLock(name: string, expiryMs?: number): Promise<void>;
13
- releaseLock(name: string): Promise<void>;
12
+ acquireLock(name: string, expiryMs?: number): Promise<string>;
13
+ releaseLock(token: string): Promise<void>;
14
14
  withLock<T = any>(name: string, runWithLock: () => Promise<T>, expiryMs?: number): Promise<T>;
15
15
  }
@@ -11,6 +11,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
11
11
  var RedisLockService_1;
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  exports.RedisLockService = void 0;
14
+ const crypto_1 = require("crypto");
14
15
  const common_1 = require("@nestjs/common");
15
16
  const redlock_1 = require("redlock");
16
17
  const redis_service_1 = require("./redis.service");
@@ -18,7 +19,7 @@ let RedisLockService = RedisLockService_1 = class RedisLockService {
18
19
  constructor(redisService, lockSettings) {
19
20
  this.redisService = redisService;
20
21
  this.logger = new common_1.Logger(RedisLockService_1.name);
21
- this.locks = {};
22
+ this.locks = new Map();
22
23
  const defaultSettings = {
23
24
  driftFactor: 0.01,
24
25
  retryCount: 0,
@@ -37,25 +38,30 @@ let RedisLockService = RedisLockService_1 = class RedisLockService {
37
38
  async acquireLock(name, expiryMs = 60000) {
38
39
  this.logger.log(`acquireLock redis Lock for ${name}`);
39
40
  const lock = await this.redLock.acquire([name], expiryMs);
40
- if (lock) {
41
- this.locks[name] = lock;
41
+ if (!lock) {
42
+ throw new Error(`Failed to acquire redis lock for ${name}`);
42
43
  }
44
+ const token = (0, crypto_1.randomUUID)();
45
+ this.locks.set(token, lock);
46
+ return token;
43
47
  }
44
- async releaseLock(name) {
45
- this.logger.log(`releaseLock redis Lock for ${name}`);
46
- if (this.locks.hasOwnProperty(name)) {
48
+ async releaseLock(token) {
49
+ this.logger.log(`releaseLock redis Lock for token ${token}`);
50
+ if (this.locks.has(token)) {
47
51
  try {
48
- const lock = this.locks[name];
52
+ const lock = this.locks.get(token);
49
53
  await lock.release();
50
- this.locks[name] = undefined;
51
54
  }
52
55
  catch (error) {
53
- this.logger.log(`releaseLock redis Lock for ${name} error:`, error);
56
+ this.logger.log(`releaseLock redis Lock for token ${token} error:`, error);
57
+ }
58
+ finally {
59
+ this.locks.delete(token);
54
60
  }
55
61
  }
56
62
  }
57
63
  async withLock(name, runWithLock, expiryMs = 60000) {
58
- await this.acquireLock(name, expiryMs);
64
+ const token = await this.acquireLock(name, expiryMs);
59
65
  try {
60
66
  return await runWithLock();
61
67
  }
@@ -63,7 +69,7 @@ let RedisLockService = RedisLockService_1 = class RedisLockService {
63
69
  throw error;
64
70
  }
65
71
  finally {
66
- await this.releaseLock(name);
72
+ await this.releaseLock(token);
67
73
  }
68
74
  }
69
75
  };
@@ -1 +1 @@
1
- {"version":3,"file":"lock.service.js","sourceRoot":"","sources":["../lib/lock.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAA2E;AAC3E,qCAAkD;AAClD,mDAA+C;AAIxC,IAAM,gBAAgB,wBAAtB,MAAM,gBAAgB;IAO3B,YAA6B,YAA0B,EAAE,YAA2B;QAAvD,iBAAY,GAAZ,YAAY,CAAc;QANpC,WAAM,GAAG,IAAI,eAAM,CAAC,kBAAgB,CAAC,IAAI,CAAC,CAAC;QAI3C,UAAK,GAAG,EA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BAC3C,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;YACvB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,oCAAoC,KAAK,SAAS,EAAE,KAAK,CAAC,CAAC;YAC7E,CAAC;oBAAS,CAAC;gBACT,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC3B,CAAC;QACH,CAAC;IACH,CAAC;IAEM,KAAK,CAAC,QAAQ,CAAU,IAAY,EAAE,WAA6B,EAAE,QAAQ,GAAG,KAAK;QAC1F,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QACrD,IAAI,CAAC;YACH,OAAO,MAAM,WAAW,EAAE,CAAC;QAC7B,CAAC;QACD,OAAM,KAAK,EAAE,CAAC;YACZ,MAAM,KAAK,CAAC;QACd,CAAC;gBACO,CAAC;YACP,MAAM,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;CAEF,CAAA;AA5EY,4CAAgB;2BAAhB,gBAAgB;IAD5B,IAAA,mBAAU,GAAE;qCAQgC,4BAAY;GAP5C,gBAAgB,CA4E5B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nathapp/nestjs-redis",
3
- "version": "3.2.1",
3
+ "version": "3.3.0",
4
4
  "description": "NestJS ioredis module with multi-client support and Redlock-based distributed locking",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -10,7 +10,9 @@
10
10
  "test": "jest --config ./test/jest-test.json",
11
11
  "test:cov": "jest --config ./test/jest-test.json --coverage",
12
12
  "test:watch": "jest --config ./test/jest-test.json --watch",
13
- "lint": "eslint -c .eslintrc.js"
13
+ "lint": "eslint -c .eslintrc.js",
14
+ "type-check": "tsc --noEmit -p tsconfig.json",
15
+ "lint:fix": "eslint -c .eslintrc.js --fix"
14
16
  },
15
17
  "author": "William Khoo",
16
18
  "license": "MIT",