@zukhruf/mutex 0.8.25 → 0.8.27
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.
|
@@ -4,10 +4,11 @@ import type { AcquireOptions, LockStore } from '../../mutex/lock-store.ts';
|
|
|
4
4
|
export interface MemoryStoreOptions {
|
|
5
5
|
tokens?: TokenSource;
|
|
6
6
|
}
|
|
7
|
-
/** Per-key FIFO line
|
|
7
|
+
/** Per-key FIFO line on a `Lock`; locks are shared only through this instance. */
|
|
8
8
|
export declare class MemoryStore implements LockStore {
|
|
9
9
|
#private;
|
|
10
10
|
constructor({ tokens }?: MemoryStoreOptions);
|
|
11
|
+
/** A cancel of `signal` takes the caller out of the line, so the caller behind it moves up. */
|
|
11
12
|
acquire(key: string, { signal }?: AcquireOptions): Promise<LockHandle>;
|
|
12
13
|
tryAcquire(key: string): Promise<LockHandle | undefined>;
|
|
13
14
|
/** The first caller in a key's line holds it, so a key with a line is held. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory-store.d.ts","sourceRoot":"","sources":["../../../src/lock-stores/memory/memory-store.ts"],"names":[],"mappings":"AACA,OAAO,EAAsB,KAAK,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAExE,OAAO,EAAE,KAAK,UAAU,EAAY,MAAM,sBAAsB,CAAC;AACjE,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAE3E,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;
|
|
1
|
+
{"version":3,"file":"memory-store.d.ts","sourceRoot":"","sources":["../../../src/lock-stores/memory/memory-store.ts"],"names":[],"mappings":"AACA,OAAO,EAAsB,KAAK,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAExE,OAAO,EAAE,KAAK,UAAU,EAAY,MAAM,sBAAsB,CAAC;AACjE,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAE3E,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAQD,kFAAkF;AAClF,qBAAa,WAAY,YAAW,SAAS;;gBAK/B,EAAE,MAAiC,EAAE,GAAE,kBAAuB;IAI1E,+FAA+F;IACzF,OAAO,CACX,GAAG,EAAE,MAAM,EACX,EAAE,MAAM,EAAE,GAAE,cAAmB,GAC9B,OAAO,CAAC,UAAU,CAAC;IAOhB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAS9D,+EAA+E;IACzE,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAI3C,qHAAqH;IACrH,IAAI,WAAW,IAAI,OAAO,CAEzB;CAwBF"}
|
|
@@ -1,39 +1,29 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Lock } from '@zukhruf/async';
|
|
2
2
|
import { CounterTokenSource } from '@zukhruf/fencing';
|
|
3
3
|
import { leaseFor } from "../../mutex/lease.js";
|
|
4
|
-
/** Per-key FIFO line
|
|
4
|
+
/** Per-key FIFO line on a `Lock`; locks are shared only through this instance. */
|
|
5
5
|
export class MemoryStore {
|
|
6
|
-
/**
|
|
6
|
+
/** The line of each key that a caller holds or waits for. */
|
|
7
7
|
#lines = new Map();
|
|
8
8
|
#tokens;
|
|
9
9
|
constructor({ tokens = new CounterTokenSource() } = {}) {
|
|
10
10
|
this.#tokens = tokens;
|
|
11
11
|
}
|
|
12
|
+
/** A cancel of `signal` takes the caller out of the line, so the caller behind it moves up. */
|
|
12
13
|
async acquire(key, { signal } = {}) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this.#
|
|
17
|
-
const release = async () => {
|
|
18
|
-
released.open();
|
|
19
|
-
if (this.#lines.get(key) === released)
|
|
20
|
-
this.#lines.delete(key);
|
|
21
|
-
};
|
|
22
|
-
const turn = previous?.wait() ?? Promise.resolve();
|
|
23
|
-
try {
|
|
24
|
-
await untilAborted(turn, signal);
|
|
25
|
-
}
|
|
26
|
-
catch (error) {
|
|
27
|
-
// A waiter cannot leave the middle of the line, so its place passes the key on when its turn comes.
|
|
28
|
-
void turn.then(release);
|
|
29
|
-
throw error;
|
|
30
|
-
}
|
|
31
|
-
return leaseFor(key, { [Symbol.asyncDispose]: release }, this.#tokens);
|
|
14
|
+
const { lock, place } = this.#join(key);
|
|
15
|
+
using joined = place;
|
|
16
|
+
joined.use(await lock.acquire({ signal }));
|
|
17
|
+
return await this.#lease(key, joined.move());
|
|
32
18
|
}
|
|
33
19
|
async tryAcquire(key) {
|
|
34
|
-
|
|
20
|
+
const { lock, place } = this.#join(key);
|
|
21
|
+
using joined = place;
|
|
22
|
+
const held = lock.tryAcquire();
|
|
23
|
+
if (!held)
|
|
35
24
|
return undefined;
|
|
36
|
-
|
|
25
|
+
joined.use(held);
|
|
26
|
+
return await this.#lease(key, joined.move());
|
|
37
27
|
}
|
|
38
28
|
/** The first caller in a key's line holds it, so a key with a line is held. */
|
|
39
29
|
async isHeld(key) {
|
|
@@ -43,5 +33,23 @@ export class MemoryStore {
|
|
|
43
33
|
get holdsAnyKey() {
|
|
44
34
|
return this.#lines.size > 0;
|
|
45
35
|
}
|
|
36
|
+
/** Joining is synchronous, so the order of the line is the order of the calls. Dispose the place to leave the line. */
|
|
37
|
+
#join(key) {
|
|
38
|
+
const line = this.#lines.getOrInsertComputed(key, () => ({
|
|
39
|
+
lock: new Lock(),
|
|
40
|
+
callers: 0,
|
|
41
|
+
}));
|
|
42
|
+
line.callers++;
|
|
43
|
+
const place = new DisposableStack();
|
|
44
|
+
place.defer(() => {
|
|
45
|
+
if (--line.callers === 0)
|
|
46
|
+
this.#lines.delete(key);
|
|
47
|
+
});
|
|
48
|
+
return { lock: line.lock, place };
|
|
49
|
+
}
|
|
50
|
+
/** The release frees the lock and leaves the line in the same turn, so a look right after it sees the key free. */
|
|
51
|
+
#lease(key, held) {
|
|
52
|
+
return leaseFor(key, { [Symbol.asyncDispose]: async () => held.dispose() }, this.#tokens);
|
|
53
|
+
}
|
|
46
54
|
}
|
|
47
55
|
//# sourceMappingURL=memory-store.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory-store.js","sourceRoot":"","sources":["../../../src/lock-stores/memory/memory-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"memory-store.js","sourceRoot":"","sources":["../../../src/lock-stores/memory/memory-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,kBAAkB,EAAoB,MAAM,kBAAkB,CAAC;AAExE,OAAO,EAAmB,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAajE,kFAAkF;AAClF,MAAM,OAAO,WAAW;IACtB,6DAA6D;IACpD,MAAM,GAAG,IAAI,GAAG,EAAgB,CAAC;IACjC,OAAO,CAAc;IAE9B,YAAY,EAAE,MAAM,GAAG,IAAI,kBAAkB,EAAE,KAAyB,EAAE;QACxE,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED,+FAA+F;IAC/F,KAAK,CAAC,OAAO,CACX,GAAW,EACX,EAAE,MAAM,KAAqB,EAAE;QAE/B,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACxC,MAAM,MAAM,GAAG,KAAK,CAAC;QACrB,MAAM,CAAC,GAAG,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;QAC3C,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;IAC/C,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,GAAW;QAC1B,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GA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,CAAC,YAAY,CAAC,EAAE,KAAK,IAAI,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,EACrD,IAAI,CAAC,OAAO,CACb,CAAC;IACJ,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/mutex",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.27",
|
|
4
4
|
"description": "A mutex with interchangeable lock stores, from one object to every process on a host, with fencing tokens on every lease.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -46,15 +46,15 @@
|
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@zukhruf/async": "^0.8.
|
|
50
|
-
"@zukhruf/election": "^0.8.
|
|
51
|
-
"@zukhruf/fencing": "^0.8.
|
|
52
|
-
"@zukhruf/fs": "^0.8.
|
|
53
|
-
"@zukhruf/lease": "^0.8.
|
|
54
|
-
"@zukhruf/pipes": "^0.8.
|
|
49
|
+
"@zukhruf/async": "^0.8.27",
|
|
50
|
+
"@zukhruf/election": "^0.8.27",
|
|
51
|
+
"@zukhruf/fencing": "^0.8.27",
|
|
52
|
+
"@zukhruf/fs": "^0.8.27",
|
|
53
|
+
"@zukhruf/lease": "^0.8.27",
|
|
54
|
+
"@zukhruf/pipes": "^0.8.27"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"@zukhruf/docker": "^0.8.
|
|
57
|
+
"@zukhruf/docker": "^0.8.27"
|
|
58
58
|
},
|
|
59
59
|
"peerDependenciesMeta": {
|
|
60
60
|
"@zukhruf/docker": {
|
|
@@ -64,9 +64,9 @@
|
|
|
64
64
|
"devDependencies": {
|
|
65
65
|
"@sinonjs/fake-timers": "15.4.0",
|
|
66
66
|
"@types/pg": "^8.23.1",
|
|
67
|
-
"@zukhruf/docker": "^0.8.
|
|
68
|
-
"@zukhruf/sqlite-open": "^0.8.
|
|
69
|
-
"@zukhruf/testing": "^0.8.
|
|
67
|
+
"@zukhruf/docker": "^0.8.27",
|
|
68
|
+
"@zukhruf/sqlite-open": "^0.8.27",
|
|
69
|
+
"@zukhruf/testing": "^0.8.27",
|
|
70
70
|
"hono": "^4.13.13",
|
|
71
71
|
"mariadb": "^3.5.4",
|
|
72
72
|
"mysql2": "^3.24.5",
|