@daiso-tech/core 0.39.0 → 0.40.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/cache/contracts/cache-adapter.contract.d.ts +9 -9
- package/dist/cache/contracts/database-cache-adapter.contract.d.ts +10 -10
- package/dist/cache/implementations/adapters/no-op-cache-adapter/no-op-cache-adapter.d.ts +9 -9
- package/dist/cache/implementations/adapters/no-op-cache-adapter/no-op-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/derivables/cache/cache.js.map +1 -1
- package/dist/event-bus/contracts/event-bus-adapter.contract.d.ts +3 -3
- package/dist/event-bus/implementations/adapters/no-op-event-bus-adapter/no-op-event-bus-adapter.d.ts +3 -3
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.d.ts +1 -0
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js +26 -26
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js.map +1 -1
- package/dist/event-bus/implementations/derivables/event-bus/listener-store.d.ts +11 -4
- package/dist/event-bus/implementations/derivables/event-bus/listener-store.js +17 -12
- package/dist/event-bus/implementations/derivables/event-bus/listener-store.js.map +1 -1
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.js +17 -20
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.js.map +1 -1
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js +159 -752
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js.map +1 -1
- package/dist/lock/contracts/database-lock-adapter.contract.d.ts +52 -14
- package/dist/lock/contracts/lock-adapter.contract.d.ts +29 -8
- package/dist/lock/contracts/lock-adapter.contract.js +10 -1
- package/dist/lock/contracts/lock-adapter.contract.js.map +1 -1
- package/dist/lock/contracts/lock.contract.d.ts +17 -12
- package/dist/lock/contracts/lock.errors.d.ts +10 -0
- package/dist/lock/contracts/lock.errors.js +12 -0
- package/dist/lock/contracts/lock.errors.js.map +1 -1
- package/dist/lock/contracts/lock.events.d.ts +12 -0
- package/dist/lock/contracts/lock.events.js +1 -0
- package/dist/lock/contracts/lock.events.js.map +1 -1
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.d.ts +24 -6
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js +126 -31
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.d.ts +20 -7
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js +59 -35
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.d.ts +9 -9
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js +150 -78
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.d.ts +5 -5
- package/dist/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.js +3 -2
- package/dist/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.d.ts +5 -5
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js +44 -22
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.d.ts +3 -3
- package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.js +42 -10
- package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/is-database-lock-adapter.js +7 -5
- package/dist/lock/implementations/derivables/lock-provider/is-database-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.d.ts +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock.d.ts +3 -2
- package/dist/lock/implementations/derivables/lock-provider/lock.js +60 -41
- package/dist/lock/implementations/derivables/lock-provider/lock.js.map +1 -1
- package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.js +184 -166
- package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.js.map +1 -1
- package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.js +240 -41
- package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.js.map +1 -1
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js +2700 -1510
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js.map +1 -1
- package/dist/utilities/contracts/deinitizable.contract.d.ts +1 -1
- package/dist/utilities/contracts/initizable.contract.d.ts +1 -1
- package/dist/utilities/contracts/prunable.contract.d.ts +1 -1
- package/package.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Lock
|
|
3
3
|
*/
|
|
4
|
+
import { MysqlAdapter } from "kysely";
|
|
4
5
|
import { TimeSpan, } from "../../../../utilities/_module-exports.js";
|
|
5
6
|
/**
|
|
6
7
|
* To utilize the `KyselyLockAdapter`, you must install the [`"kysely"`](https://www.npmjs.com/package/kysely) package and configure a `Kysely` class instance.
|
|
@@ -16,6 +17,9 @@ export class KyselyLockAdapter {
|
|
|
16
17
|
expiredKeysRemovalInterval;
|
|
17
18
|
shouldRemoveExpiredKeys;
|
|
18
19
|
timeoutId = null;
|
|
20
|
+
currentDate;
|
|
21
|
+
setInterval;
|
|
22
|
+
isMysql;
|
|
19
23
|
/**
|
|
20
24
|
* @example
|
|
21
25
|
* ```ts
|
|
@@ -34,10 +38,14 @@ export class KyselyLockAdapter {
|
|
|
34
38
|
* ```
|
|
35
39
|
*/
|
|
36
40
|
constructor(settings) {
|
|
37
|
-
const { kysely, expiredKeysRemovalInterval = TimeSpan.fromMinutes(1), shouldRemoveExpiredKeys = true, } = settings;
|
|
41
|
+
const { kysely, expiredKeysRemovalInterval = TimeSpan.fromMinutes(1), shouldRemoveExpiredKeys = true, currentDate = () => new Date(), setInterval = globalThis.setInterval, } = settings;
|
|
38
42
|
this.expiredKeysRemovalInterval = expiredKeysRemovalInterval;
|
|
39
43
|
this.shouldRemoveExpiredKeys = shouldRemoveExpiredKeys;
|
|
40
44
|
this.kysely = kysely;
|
|
45
|
+
this.isMysql =
|
|
46
|
+
this.kysely.getExecutor().adapter instanceof MysqlAdapter;
|
|
47
|
+
this.currentDate = currentDate;
|
|
48
|
+
this.setInterval = setInterval;
|
|
41
49
|
}
|
|
42
50
|
async deInit() {
|
|
43
51
|
if (this.shouldRemoveExpiredKeys && this.timeoutId !== null) {
|
|
@@ -46,7 +54,7 @@ export class KyselyLockAdapter {
|
|
|
46
54
|
// Should throw if the index does not exists thats why the try catch is used.
|
|
47
55
|
try {
|
|
48
56
|
await this.kysely.schema
|
|
49
|
-
.dropIndex("
|
|
57
|
+
.dropIndex("lock_expiration")
|
|
50
58
|
.on("lock")
|
|
51
59
|
.execute();
|
|
52
60
|
}
|
|
@@ -66,9 +74,9 @@ export class KyselyLockAdapter {
|
|
|
66
74
|
try {
|
|
67
75
|
await this.kysely.schema
|
|
68
76
|
.createTable("lock")
|
|
69
|
-
.addColumn("key", "varchar(255)", (col) => col.primaryKey())
|
|
77
|
+
.addColumn("key", "varchar(255)", (col) => col.notNull().primaryKey())
|
|
70
78
|
.addColumn("owner", "varchar(255)", (col) => col.notNull())
|
|
71
|
-
.addColumn("
|
|
79
|
+
.addColumn("expiration", "bigint")
|
|
72
80
|
.execute();
|
|
73
81
|
}
|
|
74
82
|
catch {
|
|
@@ -77,16 +85,16 @@ export class KyselyLockAdapter {
|
|
|
77
85
|
// Should throw if the index already exists thats why the try catch is used.
|
|
78
86
|
try {
|
|
79
87
|
await this.kysely.schema
|
|
80
|
-
.createIndex("
|
|
88
|
+
.createIndex("lock_expiration")
|
|
81
89
|
.on("lock")
|
|
82
|
-
.columns(["
|
|
90
|
+
.columns(["expiration"])
|
|
83
91
|
.execute();
|
|
84
92
|
}
|
|
85
93
|
catch {
|
|
86
94
|
/* EMPTY */
|
|
87
95
|
}
|
|
88
96
|
if (this.shouldRemoveExpiredKeys) {
|
|
89
|
-
this.timeoutId = setInterval(() => {
|
|
97
|
+
this.timeoutId = this.setInterval(() => {
|
|
90
98
|
void this.removeAllExpired();
|
|
91
99
|
}, this.expiredKeysRemovalInterval.toMilliseconds());
|
|
92
100
|
}
|
|
@@ -94,7 +102,7 @@ export class KyselyLockAdapter {
|
|
|
94
102
|
async removeAllExpired() {
|
|
95
103
|
await this.kysely
|
|
96
104
|
.deleteFrom("lock")
|
|
97
|
-
.where("lock.
|
|
105
|
+
.where("lock.expiration", "<=", this.currentDate().getTime())
|
|
98
106
|
.execute();
|
|
99
107
|
}
|
|
100
108
|
async insert(key, owner, expiration) {
|
|
@@ -103,51 +111,138 @@ export class KyselyLockAdapter {
|
|
|
103
111
|
.values({
|
|
104
112
|
key,
|
|
105
113
|
owner,
|
|
106
|
-
|
|
114
|
+
expiration: expiration?.getTime() ?? null,
|
|
107
115
|
})
|
|
108
116
|
.execute();
|
|
109
117
|
}
|
|
110
|
-
async
|
|
111
|
-
const
|
|
118
|
+
async updateIfExpired(key, owner, expiration) {
|
|
119
|
+
const result = await this.kysely
|
|
112
120
|
.updateTable("lock")
|
|
113
121
|
.where("lock.key", "=", key)
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
eb("lock.
|
|
117
|
-
eb(
|
|
118
|
-
|
|
119
|
-
.set({
|
|
122
|
+
.where((eb) => {
|
|
123
|
+
const hasExpiration = eb("lock.expiration", "is not", null);
|
|
124
|
+
const hasExpired = eb("lock.expiration", "<=", Date.now());
|
|
125
|
+
return eb.and([hasExpiration, hasExpired]);
|
|
126
|
+
})
|
|
127
|
+
.set({
|
|
128
|
+
owner,
|
|
129
|
+
expiration: expiration?.getTime() ?? null,
|
|
130
|
+
})
|
|
120
131
|
.executeTakeFirst();
|
|
121
|
-
return Number(
|
|
132
|
+
return Number(result.numUpdatedRows);
|
|
122
133
|
}
|
|
123
|
-
async remove(key
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
134
|
+
async remove(key) {
|
|
135
|
+
let result;
|
|
136
|
+
if (this.isMysql) {
|
|
137
|
+
result = await this.kysely
|
|
138
|
+
.transaction()
|
|
139
|
+
.setIsolationLevel("serializable")
|
|
140
|
+
.execute(async (trx) => {
|
|
141
|
+
const row = await trx
|
|
142
|
+
.selectFrom("lock")
|
|
143
|
+
.select("lock.expiration")
|
|
144
|
+
.where("lock.key", "=", key)
|
|
145
|
+
.executeTakeFirst();
|
|
146
|
+
await trx
|
|
147
|
+
.deleteFrom("lock")
|
|
148
|
+
.where("lock.key", "=", key)
|
|
149
|
+
.executeTakeFirst();
|
|
150
|
+
return row;
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
result = await this.kysely
|
|
155
|
+
.deleteFrom("lock")
|
|
156
|
+
.where("lock.key", "=", key)
|
|
157
|
+
.returning(["lock.expiration"])
|
|
158
|
+
.executeTakeFirst();
|
|
159
|
+
}
|
|
160
|
+
if (result === undefined) {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
if (result.expiration === null) {
|
|
164
|
+
return {
|
|
165
|
+
expiration: null,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
expiration: new Date(Number(result.expiration)),
|
|
170
|
+
};
|
|
129
171
|
}
|
|
130
|
-
async
|
|
131
|
-
|
|
172
|
+
async removeIfOwner(key, owner) {
|
|
173
|
+
let row;
|
|
174
|
+
if (this.isMysql) {
|
|
175
|
+
row = await this.kysely
|
|
176
|
+
.transaction()
|
|
177
|
+
.setIsolationLevel("serializable")
|
|
178
|
+
.execute(async (trx) => {
|
|
179
|
+
const row = await trx
|
|
180
|
+
.selectFrom("lock")
|
|
181
|
+
.where("lock.key", "=", key)
|
|
182
|
+
.where("lock.owner", "=", owner)
|
|
183
|
+
.select(["lock.expiration", "lock.owner"])
|
|
184
|
+
.executeTakeFirst();
|
|
185
|
+
await trx
|
|
186
|
+
.deleteFrom("lock")
|
|
187
|
+
.where("lock.key", "=", key)
|
|
188
|
+
.where("lock.owner", "=", owner)
|
|
189
|
+
.execute();
|
|
190
|
+
return row;
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
row = await this.kysely
|
|
195
|
+
.deleteFrom("lock")
|
|
196
|
+
.where("lock.key", "=", key)
|
|
197
|
+
.where("lock.owner", "=", owner)
|
|
198
|
+
.returning(["lock.expiration", "lock.owner"])
|
|
199
|
+
.executeTakeFirst();
|
|
200
|
+
}
|
|
201
|
+
if (row === undefined) {
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
const { expiration } = row;
|
|
205
|
+
if (expiration === null) {
|
|
206
|
+
return {
|
|
207
|
+
owner: row.owner,
|
|
208
|
+
expiration: null,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
owner: row.owner,
|
|
213
|
+
expiration: new Date(Number(expiration)),
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
async updateExpirationIfOwner(key, owner, expiration) {
|
|
217
|
+
const result = await this.kysely
|
|
132
218
|
.updateTable("lock")
|
|
133
219
|
.where("lock.key", "=", key)
|
|
134
220
|
.where("lock.owner", "=", owner)
|
|
135
|
-
.set({
|
|
221
|
+
.set({
|
|
222
|
+
expiration: expiration.getTime(),
|
|
223
|
+
})
|
|
136
224
|
.executeTakeFirst();
|
|
137
|
-
return Number(
|
|
225
|
+
return Number(result.numUpdatedRows);
|
|
138
226
|
}
|
|
139
227
|
async find(key) {
|
|
140
228
|
const row = await this.kysely
|
|
141
229
|
.selectFrom("lock")
|
|
230
|
+
.select(["lock.owner", "lock.expiration"])
|
|
142
231
|
.where("lock.key", "=", key)
|
|
143
|
-
.select(["lock.owner", "lock.expiresAt"])
|
|
144
232
|
.executeTakeFirst();
|
|
145
233
|
if (row === undefined) {
|
|
146
234
|
return null;
|
|
147
235
|
}
|
|
236
|
+
const { owner, expiration } = row;
|
|
237
|
+
if (expiration === null) {
|
|
238
|
+
return {
|
|
239
|
+
owner,
|
|
240
|
+
expiration: null,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
148
243
|
return {
|
|
149
|
-
|
|
150
|
-
|
|
244
|
+
owner,
|
|
245
|
+
expiration: new Date(Number(expiration)),
|
|
151
246
|
};
|
|
152
247
|
}
|
|
153
248
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kysely-lock-adapter.js","sourceRoot":"","sources":["../../../../../src/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAOH,OAAO,EAIH,QAAQ,GACX,MAAM,gCAAgC,CAAC;
|
|
1
|
+
{"version":3,"file":"kysely-lock-adapter.js","sourceRoot":"","sources":["../../../../../src/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAOH,OAAO,EAAE,YAAY,EAAe,MAAM,QAAQ,CAAC;AACnD,OAAO,EAIH,QAAQ,GACX,MAAM,gCAAgC,CAAC;AA+DxC;;;;;;;;GAQG;AACH,MAAM,OAAO,iBAAiB;IAGT,MAAM,CAAkC;IACxC,0BAA0B,CAAW;IACrC,uBAAuB,CAAU;IAC1C,SAAS,GACb,IAAI,CAAC;IACQ,WAAW,CAAa;IACxB,WAAW,CAAqB;IAChC,OAAO,CAAU;IAElC;;;;;;;;;;;;;;;;OAgBG;IACH,YAAY,QAAmC;QAC3C,MAAM,EACF,MAAM,EACN,0BAA0B,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC,EACpD,uBAAuB,GAAG,IAAI,EAC9B,WAAW,GAAG,GAAG,EAAE,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,CAAC;IACzC,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,GAAW;QAClB,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,MAAM;aACxB,UAAU,CAAC,MAAM,CAAC;aAClB,MAAM,CAAC,CAAC,YAAY,EAAE,iBAAiB,CAAC,CAAC;aACzC,KAAK,CAAC,UAAU,EAAE,GAAG,EAAE,GAAG,CAAC;aAC3B,gBAAgB,EAAE,CAAC;QAExB,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACpB,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,GAAG,GAAG,CAAC;QAClC,IAAI,UAAU,KAAK,IAAI,EAAE,CAAC;YACtB,OAAO;gBACH,KAAK;gBACL,UAAU,EAAE,IAAI;aACnB,CAAC;QACN,CAAC;QAED,OAAO;YACH,KAAK;YACL,UAAU,EAAE,IAAI,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;SAC3C,CAAC;IACN,CAAC;CACJ"}
|
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Lock
|
|
3
3
|
*/
|
|
4
|
-
import type { TimeSpan } from "../../../../utilities/_module-exports.js";
|
|
5
|
-
import type
|
|
4
|
+
import type { IDeinitizable, TimeSpan } from "../../../../utilities/_module-exports.js";
|
|
5
|
+
import { type ILockAdapter, type LockRefreshResult } from "../../../../lock/contracts/_module-exports.js";
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* IMPORT_PATH: `"@daiso-tech/core/lock/adapters"`
|
|
9
|
+
* @group Adapters
|
|
10
|
+
*/
|
|
11
|
+
export type MemoryLockData = {
|
|
12
|
+
owner: string;
|
|
13
|
+
hasExpiration: true;
|
|
14
|
+
timeoutId: string | number | NodeJS.Timeout;
|
|
15
|
+
} | {
|
|
16
|
+
owner: string;
|
|
17
|
+
hasExpiration: false;
|
|
18
|
+
};
|
|
6
19
|
/**
|
|
7
20
|
* Note the `MemoryLockAdapter` is limited to single process usage and cannot be shared across multiple servers or different processes.
|
|
8
21
|
* This adapter is meant to be used for testing.
|
|
@@ -10,9 +23,8 @@ import type { ILockAdapter, ILockData } from "../../../../lock/contracts/_module
|
|
|
10
23
|
* IMPORT_PATH: `"@daiso-tech/core/lock/adapters"`
|
|
11
24
|
* @group Adapters
|
|
12
25
|
*/
|
|
13
|
-
export declare class MemoryLockAdapter implements ILockAdapter {
|
|
26
|
+
export declare class MemoryLockAdapter implements ILockAdapter, IDeinitizable {
|
|
14
27
|
private readonly map;
|
|
15
|
-
private readonly timeoutMap;
|
|
16
28
|
/**
|
|
17
29
|
* @example
|
|
18
30
|
* ```ts
|
|
@@ -29,9 +41,10 @@ export declare class MemoryLockAdapter implements ILockAdapter {
|
|
|
29
41
|
* const lockAdapter = new MemoryLockAdapter(map);
|
|
30
42
|
* ```
|
|
31
43
|
*/
|
|
32
|
-
constructor(map?: Map<string,
|
|
44
|
+
constructor(map?: Map<string, MemoryLockData>);
|
|
45
|
+
deInit(): Promise<void>;
|
|
33
46
|
acquire(key: string, owner: string, ttl: TimeSpan | null): Promise<boolean>;
|
|
34
47
|
release(key: string, owner: string): Promise<boolean>;
|
|
35
|
-
forceRelease(key: string): Promise<
|
|
36
|
-
refresh(key: string, owner: string,
|
|
48
|
+
forceRelease(key: string): Promise<boolean>;
|
|
49
|
+
refresh(key: string, owner: string, ttl: TimeSpan): Promise<LockRefreshResult>;
|
|
37
50
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Lock
|
|
3
3
|
*/
|
|
4
|
+
import { LOCK_REFRESH_RESULT, } from "../../../../lock/contracts/_module-exports.js";
|
|
4
5
|
/**
|
|
5
6
|
* Note the `MemoryLockAdapter` is limited to single process usage and cannot be shared across multiple servers or different processes.
|
|
6
7
|
* This adapter is meant to be used for testing.
|
|
@@ -10,7 +11,6 @@
|
|
|
10
11
|
*/
|
|
11
12
|
export class MemoryLockAdapter {
|
|
12
13
|
map;
|
|
13
|
-
timeoutMap = new Map();
|
|
14
14
|
/**
|
|
15
15
|
* @example
|
|
16
16
|
* ```ts
|
|
@@ -31,64 +31,88 @@ export class MemoryLockAdapter {
|
|
|
31
31
|
this.map = map;
|
|
32
32
|
}
|
|
33
33
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
34
|
+
async deInit() {
|
|
35
|
+
for (const [key, lockData] of this.map) {
|
|
36
|
+
if (lockData.hasExpiration) {
|
|
37
|
+
clearTimeout(lockData.timeoutId);
|
|
38
|
+
}
|
|
39
|
+
this.map.delete(key);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
34
43
|
async acquire(key, owner, ttl) {
|
|
35
|
-
|
|
36
|
-
if (
|
|
37
|
-
|
|
44
|
+
let lock = this.map.get(key);
|
|
45
|
+
if (lock !== undefined) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
if (ttl === null) {
|
|
49
|
+
lock = {
|
|
38
50
|
owner,
|
|
39
|
-
|
|
40
|
-
}
|
|
51
|
+
hasExpiration: false,
|
|
52
|
+
};
|
|
53
|
+
this.map.set(key, lock);
|
|
41
54
|
}
|
|
42
|
-
|
|
43
|
-
|
|
55
|
+
else {
|
|
56
|
+
const timeoutId = setTimeout(() => {
|
|
44
57
|
this.map.delete(key);
|
|
45
|
-
|
|
46
|
-
|
|
58
|
+
}, ttl.toMilliseconds());
|
|
59
|
+
lock = {
|
|
60
|
+
owner,
|
|
61
|
+
hasExpiration: true,
|
|
62
|
+
timeoutId,
|
|
63
|
+
};
|
|
64
|
+
this.map.set(key, lock);
|
|
47
65
|
}
|
|
48
|
-
return
|
|
66
|
+
return true;
|
|
49
67
|
}
|
|
50
68
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
51
69
|
async release(key, owner) {
|
|
52
|
-
const
|
|
53
|
-
if (
|
|
54
|
-
return
|
|
70
|
+
const lock = this.map.get(key);
|
|
71
|
+
if (lock === undefined) {
|
|
72
|
+
return false;
|
|
55
73
|
}
|
|
56
|
-
if (
|
|
74
|
+
if (lock.owner !== owner) {
|
|
57
75
|
return false;
|
|
58
76
|
}
|
|
59
|
-
|
|
60
|
-
|
|
77
|
+
if (lock.hasExpiration) {
|
|
78
|
+
clearTimeout(lock.timeoutId);
|
|
79
|
+
}
|
|
61
80
|
this.map.delete(key);
|
|
62
81
|
return true;
|
|
63
82
|
}
|
|
64
83
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
65
84
|
async forceRelease(key) {
|
|
66
|
-
|
|
67
|
-
|
|
85
|
+
const lock = this.map.get(key);
|
|
86
|
+
if (lock === undefined) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
if (lock.hasExpiration) {
|
|
90
|
+
clearTimeout(lock.timeoutId);
|
|
91
|
+
}
|
|
68
92
|
this.map.delete(key);
|
|
93
|
+
return true;
|
|
69
94
|
}
|
|
70
95
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
71
|
-
async refresh(key, owner,
|
|
72
|
-
const
|
|
73
|
-
if (
|
|
74
|
-
return
|
|
96
|
+
async refresh(key, owner, ttl) {
|
|
97
|
+
const lock = this.map.get(key);
|
|
98
|
+
if (lock === undefined) {
|
|
99
|
+
return LOCK_REFRESH_RESULT.UNOWNED_REFRESH;
|
|
75
100
|
}
|
|
76
|
-
if (
|
|
77
|
-
return
|
|
101
|
+
if (lock.owner !== owner) {
|
|
102
|
+
return LOCK_REFRESH_RESULT.UNOWNED_REFRESH;
|
|
78
103
|
}
|
|
79
|
-
if (
|
|
80
|
-
return
|
|
104
|
+
if (!lock.hasExpiration) {
|
|
105
|
+
return LOCK_REFRESH_RESULT.UNEXPIRABLE_KEY;
|
|
81
106
|
}
|
|
107
|
+
clearTimeout(lock.timeoutId);
|
|
108
|
+
const timeoutId = setTimeout(() => {
|
|
109
|
+
this.map.delete(key);
|
|
110
|
+
}, ttl.toMilliseconds());
|
|
82
111
|
this.map.set(key, {
|
|
83
|
-
...
|
|
84
|
-
|
|
112
|
+
...lock,
|
|
113
|
+
timeoutId,
|
|
85
114
|
});
|
|
86
|
-
|
|
87
|
-
this.timeoutMap.set(key, setTimeout(() => {
|
|
88
|
-
this.timeoutMap.delete(key);
|
|
89
|
-
this.map.delete(key);
|
|
90
|
-
}, time.toMilliseconds()));
|
|
91
|
-
return true;
|
|
115
|
+
return LOCK_REFRESH_RESULT.REFRESHED;
|
|
92
116
|
}
|
|
93
117
|
}
|
|
94
118
|
//# sourceMappingURL=memory-lock-adapter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory-lock-adapter.js","sourceRoot":"","sources":["../../../../../src/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"memory-lock-adapter.js","sourceRoot":"","sources":["../../../../../src/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EACH,mBAAmB,GAGtB,MAAM,qCAAqC,CAAC;AAkB7C;;;;;;GAMG;AACH,MAAM,OAAO,iBAAiB;IAiBG;IAhB7B;;;;;;;;;;;;;;;OAeG;IACH,YAA6B,MAAM,IAAI,GAAG,EAA0B;QAAvC,QAAG,GAAH,GAAG,CAAoC;IAAG,CAAC;IAExE,4DAA4D;IAC5D,KAAK,CAAC,MAAM;QACR,KAAK,MAAM,CAAC,GAAG,EAAE,QAAQ,CAAC,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC;YACrC,IAAI,QAAQ,CAAC,aAAa,EAAE,CAAC;gBACzB,YAAY,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;YACrC,CAAC;YACD,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACzB,CAAC;IACL,CAAC;IAED,4DAA4D;IAC5D,KAAK,CAAC,OAAO,CACT,GAAW,EACX,KAAa,EACb,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
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Lock
|
|
3
3
|
*/
|
|
4
|
+
import type { TimeSpan } from "../../../../utilities/_module-exports.js";
|
|
4
5
|
import { type IDeinitizable, type IInitizable } from "../../../../utilities/_module-exports.js";
|
|
5
|
-
import type
|
|
6
|
+
import { type ILockAdapter, type LockRefreshResult } from "../../../../lock/contracts/_module-exports.js";
|
|
6
7
|
import type { CollectionOptions, Db } from "mongodb";
|
|
7
|
-
import { ObjectId } from "mongodb";
|
|
8
|
+
import type { ObjectId } from "mongodb";
|
|
8
9
|
/**
|
|
9
10
|
*
|
|
10
11
|
* IMPORT_PATH: `"@daiso-tech/core/lock/adapters"`
|
|
@@ -27,7 +28,7 @@ export type MongodbLockDocument = {
|
|
|
27
28
|
_id: ObjectId;
|
|
28
29
|
key: string;
|
|
29
30
|
owner: string;
|
|
30
|
-
|
|
31
|
+
expiration: Date | null;
|
|
31
32
|
};
|
|
32
33
|
/**
|
|
33
34
|
* To utilize the `MongodbLockAdapter`, you must install the [`"mongodb"`](https://www.npmjs.com/package/mongodb) package.
|
|
@@ -37,7 +38,7 @@ export type MongodbLockDocument = {
|
|
|
37
38
|
* IMPORT_PATH: `"@daiso-tech/core/lock/adapters"`
|
|
38
39
|
* @group Adapters
|
|
39
40
|
*/
|
|
40
|
-
export declare class MongodbLockAdapter implements
|
|
41
|
+
export declare class MongodbLockAdapter implements ILockAdapter, IDeinitizable, IInitizable {
|
|
41
42
|
private readonly database;
|
|
42
43
|
private readonly collection;
|
|
43
44
|
private readonly collectionName;
|
|
@@ -67,9 +68,8 @@ export declare class MongodbLockAdapter implements IDatabaseLockAdapter, IDeinit
|
|
|
67
68
|
* Note all lock data will be removed.
|
|
68
69
|
*/
|
|
69
70
|
deInit(): Promise<void>;
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
refresh(key: string, owner: string,
|
|
74
|
-
find(key: string): Promise<ILockData | null>;
|
|
71
|
+
acquire(key: string, owner: string, ttl: TimeSpan | null): Promise<boolean>;
|
|
72
|
+
release(key: string, owner: string): Promise<boolean>;
|
|
73
|
+
forceRelease(key: string): Promise<boolean>;
|
|
74
|
+
refresh(key: string, owner: string, ttl: TimeSpan): Promise<LockRefreshResult>;
|
|
75
75
|
}
|