@mavenmachines/event-handler-lib 1.1.17 → 1.1.19-rc.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.
- package/dist/src/initializers/lock.service.d.ts +11 -0
- package/dist/src/initializers/lock.service.js +73 -0
- package/dist/src/initializers/lock.service.js.map +1 -0
- package/dist/test/tests/services/lock.service.spec.d.ts +1 -0
- package/dist/test/tests/services/lock.service.spec.js +38 -0
- package/dist/test/tests/services/lock.service.spec.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { RedisClient } from "../clients/redisClient";
|
|
2
|
+
import { DurationLike } from "luxon";
|
|
3
|
+
export declare class LockTimeout extends Error {
|
|
4
|
+
}
|
|
5
|
+
export declare class LockService {
|
|
6
|
+
private readonly redis;
|
|
7
|
+
constructor(redis: RedisClient);
|
|
8
|
+
performLocked<T>(lockName: string, timeout: DurationLike, fn: () => Promise<T> | T): Promise<T>;
|
|
9
|
+
private acquireLock;
|
|
10
|
+
private releaseLock;
|
|
11
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.LockService = exports.LockTimeout = void 0;
|
|
16
|
+
const inversify_1 = require("inversify");
|
|
17
|
+
const redisClient_1 = require("../clients/redisClient");
|
|
18
|
+
const luxon_1 = require("luxon");
|
|
19
|
+
const util_1 = require("util");
|
|
20
|
+
const LOCK_PREFIX = "event-sourcing-locks";
|
|
21
|
+
const DEFAULT_RETRY_DELAY = luxon_1.Duration.fromMillis(10).toMillis();
|
|
22
|
+
class LockTimeout extends Error {
|
|
23
|
+
}
|
|
24
|
+
exports.LockTimeout = LockTimeout;
|
|
25
|
+
const sleep = (0, util_1.promisify)(setTimeout);
|
|
26
|
+
let LockService = class LockService {
|
|
27
|
+
constructor(redis) {
|
|
28
|
+
this.redis = redis.getClient();
|
|
29
|
+
}
|
|
30
|
+
async performLocked(lockName, timeout, fn) {
|
|
31
|
+
const lockTimeoutValue = await this.acquireLock(lockName, timeout);
|
|
32
|
+
try {
|
|
33
|
+
return await fn();
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
if (Date.now() <= lockTimeoutValue) {
|
|
37
|
+
await this.releaseLock(lockName);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async acquireLock(lockName, timeout, retryDelay) {
|
|
42
|
+
const retryDelayMs = retryDelay
|
|
43
|
+
? luxon_1.Duration.fromDurationLike(retryDelay).toMillis()
|
|
44
|
+
: DEFAULT_RETRY_DELAY;
|
|
45
|
+
const timeoutMs = luxon_1.Duration.fromDurationLike(timeout).toMillis();
|
|
46
|
+
const lockTimeoutValue = Date.now() + timeoutMs + 1;
|
|
47
|
+
while (Date.now() < lockTimeoutValue) {
|
|
48
|
+
const result = await this.redis.set(lockKey(lockName), lockTimeoutValue, {
|
|
49
|
+
PX: timeoutMs,
|
|
50
|
+
NX: true,
|
|
51
|
+
});
|
|
52
|
+
console.log(result);
|
|
53
|
+
if (result !== null) {
|
|
54
|
+
return lockTimeoutValue;
|
|
55
|
+
}
|
|
56
|
+
await sleep(retryDelayMs);
|
|
57
|
+
}
|
|
58
|
+
throw new LockTimeout(lockName);
|
|
59
|
+
}
|
|
60
|
+
async releaseLock(lockName) {
|
|
61
|
+
await this.redis.del(lockKey(lockName));
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
exports.LockService = LockService;
|
|
65
|
+
exports.LockService = LockService = __decorate([
|
|
66
|
+
(0, inversify_1.injectable)(),
|
|
67
|
+
__param(0, (0, inversify_1.inject)(redisClient_1.RedisClient)),
|
|
68
|
+
__metadata("design:paramtypes", [redisClient_1.RedisClient])
|
|
69
|
+
], LockService);
|
|
70
|
+
function lockKey(lockName) {
|
|
71
|
+
return `${LOCK_PREFIX}-${lockName}`;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=lock.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lock.service.js","sourceRoot":"","sources":["../../../src/initializers/lock.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,yCAA+C;AAE/C,wDAAqD;AACrD,iCAA+C;AAC/C,+BAAiC;AAEjC,MAAM,WAAW,GAAG,sBAAsB,CAAC;AAC3C,MAAM,mBAAmB,GAAG,gBAAQ,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC;AAE/D,MAAa,WAAY,SAAQ,KAAK;CAAG;AAAzC,kCAAyC;AAEzC,MAAM,KAAK,GAAG,IAAA,gBAAS,EAAC,UAAU,CAAC,CAAC;AAG7B,IAAM,WAAW,GAAjB,MAAM,WAAW;IAGtB,YAAiC,KAAkB;QACjD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,SAAS,EAAE,CAAC;IACjC,CAAC;IAEM,KAAK,CAAC,aAAa,CACxB,QAAgB,EAChB,OAAqB,EACrB,EAAwB;Q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I,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC1C,CAAC;CACF,CAAA;AAjDY,kCAAW;sBAAX,WAAW;IADvB,IAAA,sBAAU,GAAE;IAIE,WAAA,IAAA,kBAAM,EAAC,yBAAW,CAAC,CAAA;qCAAQ,yBAAW;GAHxC,WAAW,CAiDvB;AACD,SAAS,OAAO,CAAC,QAAgB;IAC/B,OAAO,GAAG,WAAW,IAAI,QAAQ,EAAE,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const lock_service_1 = require("../../../src/initializers/lock.service");
|
|
4
|
+
const common_1 = require("../common");
|
|
5
|
+
const util_1 = require("util");
|
|
6
|
+
const chai_1 = require("chai");
|
|
7
|
+
const redisClient_1 = require("../../../src/clients/redisClient");
|
|
8
|
+
const sleep = (0, util_1.promisify)(setTimeout);
|
|
9
|
+
describe.only("LockService", () => {
|
|
10
|
+
let appContainer;
|
|
11
|
+
let lockService;
|
|
12
|
+
let redis;
|
|
13
|
+
before(async () => {
|
|
14
|
+
appContainer = await (0, common_1.getTestAppContainer)();
|
|
15
|
+
lockService = await appContainer.getAsync(lock_service_1.LockService);
|
|
16
|
+
redis = await appContainer.getAsync(redisClient_1.RedisClient);
|
|
17
|
+
});
|
|
18
|
+
it("Should provide mutex across multiple calls", async () => {
|
|
19
|
+
const store = [];
|
|
20
|
+
const lockName = "LOCK";
|
|
21
|
+
await redis.getClient().del(`event-sourcing-locks-${lockName}`);
|
|
22
|
+
async function worker(n) {
|
|
23
|
+
await lockService.performLocked(lockName, { seconds: 10 }, async () => {
|
|
24
|
+
for (let i = 0; i < 10; i++) {
|
|
25
|
+
await sleep(i);
|
|
26
|
+
store.push(n * 10 + i);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
await Promise.all([worker(0), worker(1), worker(2), worker(3)]);
|
|
31
|
+
const expected = [];
|
|
32
|
+
for (let i = 0; i < 40; i++) {
|
|
33
|
+
expected.push(i);
|
|
34
|
+
}
|
|
35
|
+
(0, chai_1.expect)(store).to.deep.equal(expected);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
//# sourceMappingURL=lock.service.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lock.service.spec.js","sourceRoot":"","sources":["../../../../test/tests/services/lock.service.spec.ts"],"names":[],"mappings":";;AACA,yEAAqE;AACrE,sCAAgD;AAChD,+BAAiC;AACjC,+BAA8B;AAC9B,kEAA+D;AAE/D,MAAM,KAAK,GAAG,IAAA,gBAAS,EAAC,UAAU,CAAC,CAAC;AAEpC,QAAQ,CAAC,IAAI,CAAC,aAAa,EAAE,GAAG,EAAE;IAChC,IAAI,YAAuB,CAAC;IAC5B,IAAI,WAAwB,CAAC;IAC7B,IAAI,KAAkB,CAAC;IAEvB,MAAM,CAAC,KAAK,IAAI,EAAE;QAChB,YAAY,GAAG,MAAM,IAAA,4BAAmB,GAAE,CAAC;QAC3C,WAAW,GAAG,MAAM,YAAY,CAAC,QAAQ,CAAC,0BAAW,CAAC,CAAC;QACvD,KAAK,GAAG,MAAM,YAAY,CAAC,QAAQ,CAAC,yBAAW,CAAC,CAAC;IACnD,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,4CAA4C,EAAE,KAAK,IAAI,EAAE;QAC1D,MAAM,KAAK,GAAa,EAAE,CAAC;QAC3B,MAAM,QAAQ,GAAG,MAAM,CAAC;QAExB,MAAM,KAAK,CAAC,SAAS,EAAE,CAAC,GAAG,CAAC,wBAAwB,QAAQ,EAAE,CAAC,CAAC;QAEhE,KAAK,UAAU,MAAM,CAAC,CAAS;YAC7B,MAAM,WAAW,CAAC,aAAa,CAAC,QAAQ,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,EAA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