Repository navigation
Expand file tree
/
Copy pathrequest-execution-locks.ts
More file actions
100 lines (92 loc) · 2.97 KB
/
Copy pathrequest-execution-locks.ts
File metadata and controls
100 lines (92 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { withKeyedLock } from '@agent-device/kernel/keyed-lock';
import type { RequestExecutionLockKey } from './request-binding.ts';
type RetainedExecutionLock = {
acquired: Promise<void>;
completion: Promise<void>;
release(): void;
};
export type RequestExecutionLocks = {
run<T>(task: () => Promise<T>): Promise<T>;
retainDevice(deviceId: string): Promise<void>;
};
/**
* Owns the stable execution locks for one request and any device lock discovered
* after admission. Dynamic device retention is used by a fresh replay whose
* first open cannot select its device during advisory preflight.
*/
export function createRequestExecutionLocks(params: {
locks: Map<string, Promise<unknown>>;
initialKeys: RequestExecutionLockKey[];
}): RequestExecutionLocks {
const { locks, initialKeys } = params;
const initialKeySet = new Set(initialKeys);
const retainedLocks = new Map<RequestExecutionLockKey, RetainedExecutionLock>();
let running = false;
const releaseRetainedLocks = async (): Promise<void> => {
const held = [...retainedLocks.values()];
for (const lock of held) lock.release();
await Promise.all(held.map(async (lock) => await lock.completion));
retainedLocks.clear();
};
return {
run: async (task) => {
if (running) {
throw new Error('Request execution locks cannot run the same scope concurrently.');
}
running = true;
try {
return await withRequestExecutionLockKeys(locks, initialKeys, task);
} finally {
try {
await releaseRetainedLocks();
} finally {
running = false;
}
}
},
retainDevice: async (deviceId) => {
if (!running) {
throw new Error('A device execution lock can only be retained by a running request.');
}
const key = `device:${deviceId}` as const;
if (initialKeySet.has(key)) return;
let retained = retainedLocks.get(key);
if (!retained) {
retained = retainExecutionLock(locks, key);
retainedLocks.set(key, retained);
}
await retained.acquired;
},
};
}
export async function withRequestExecutionLockKeys<T>(
locks: Map<string, Promise<unknown>>,
keys: readonly RequestExecutionLockKey[],
task: () => Promise<T>,
): Promise<T> {
const [key, ...remainingKeys] = keys;
if (!key) return await task();
return await withKeyedLock(
locks,
key,
async () => await withRequestExecutionLockKeys(locks, remainingKeys, task),
);
}
function retainExecutionLock(
locks: Map<string, Promise<unknown>>,
key: RequestExecutionLockKey,
): RetainedExecutionLock {
let markAcquired: () => void = () => {};
let release: () => void = () => {};
const acquired = new Promise<void>((resolve) => {
markAcquired = resolve;
});
const released = new Promise<void>((resolve) => {
release = resolve;
});
const completion = withKeyedLock(locks, key, async () => {
markAcquired();
await released;
});
return { acquired, completion, release };
}