@diia-inhouse/redis 3.1.76 → 3.2.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.
@@ -11,6 +11,11 @@ type LockOptions = {
11
11
  * @defaultValue 500
12
12
  */
13
13
  retryInterval?: number;
14
+ /**
15
+ * Maximum time in milliseconds to keep trying to acquire a locked resource before giving up
16
+ * @defaultValue ttl * 2
17
+ */
18
+ acquireTimeout?: number;
14
19
  };
15
20
  //#endregion
16
21
  export { Lock, LockOptions, MutexStatusResult, RedlockMutex$1 as RedlockMutex };
@@ -11,9 +11,12 @@ declare class RedlockService implements OnHealthCheck, OnDestroy {
11
11
  constructor(storeConfig: RedisConfig, logger: Logger);
12
12
  onHealthCheck(): Promise<HealthCheckResult<MutexStatusResult>>;
13
13
  onDestroy(): Promise<void>;
14
- lock(resource: string, ttl?: number, {
15
- retryInterval
16
- }?: LockOptions): Promise<RedlockMutex>;
14
+ lock(resource: string, ttl?: number, options?: LockOptions): Promise<RedlockMutex>;
15
+ /**
16
+ * Same as `lock`, but resolves with `null` instead of throwing when the resource is not acquired within `acquireTimeout`
17
+ */
18
+ tryLock(resource: string, ttl?: number, options?: LockOptions): Promise<RedlockMutex | null>;
19
+ private createMutex;
17
20
  }
18
21
  //#endregion
19
22
  export { RedlockService };
@@ -37,15 +37,25 @@ var RedlockService = class {
37
37
  async onDestroy() {
38
38
  await this.clientRW.quit();
39
39
  }
40
- async lock(resource, ttl = 6e4, { retryInterval = 500 } = {}) {
40
+ async lock(resource, ttl = 6e4, options = {}) {
41
+ const mutex = this.createMutex(resource, ttl, options);
42
+ await mutex.acquire();
43
+ return mutex;
44
+ }
45
+ /**
46
+ * Same as `lock`, but resolves with `null` instead of throwing when the resource is not acquired within `acquireTimeout`
47
+ */
48
+ async tryLock(resource, ttl = 6e4, options = {}) {
49
+ const mutex = this.createMutex(resource, ttl, options);
50
+ return await mutex.tryAcquire() ? mutex : null;
51
+ }
52
+ createMutex(resource, ttl, { retryInterval = 500, acquireTimeout = ttl * 2 }) {
41
53
  this.logger.info(`Start LOCK resource [${resource}] for ttl [${ttl}]ms`);
42
- const mutex = new RedlockMutex([this.clientRW], resource, {
54
+ return new RedlockMutex([this.clientRW], resource, {
43
55
  lockTimeout: ttl,
44
- acquireTimeout: ttl * 2,
56
+ acquireTimeout,
45
57
  retryInterval
46
58
  });
47
- await mutex.acquire();
48
- return mutex;
49
59
  }
50
60
  };
51
61
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diia-inhouse/redis",
3
- "version": "3.1.76",
3
+ "version": "3.2.0",
4
4
  "type": "module",
5
5
  "description": "Redis services - redlock, pubsub, cache and store",
6
6
  "main": "dist/index.js",
@@ -49,7 +49,7 @@
49
49
  "devDependencies": {
50
50
  "@diia-inhouse/configs": "7.2.1",
51
51
  "@diia-inhouse/diia-logger": "4.4.71",
52
- "@diia-inhouse/env": "3.3.62",
52
+ "@diia-inhouse/env": "3.3.63",
53
53
  "@diia-inhouse/errors": "2.2.31",
54
54
  "@diia-inhouse/oxc-config": "2.1.6",
55
55
  "@diia-inhouse/test": "8.3.18",