durability 4.1.1 → 4.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.
@@ -0,0 +1,797 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _DurabilitySemaphoreQueue_instances, _DurabilitySemaphoreQueue_engine, _DurabilitySemaphoreQueue_store, _DurabilitySemaphoreQueue_server, _DurabilitySemaphoreQueue_clientId, _DurabilitySemaphoreQueue_admit, _DurabilitySemaphoreQueue_finished, _DurabilitySemaphoreQueue_finishedCheckMs, _DurabilitySemaphoreQueue_maxAdmitBatch, _DurabilitySemaphoreQueue_attemptTimeoutMs, _DurabilitySemaphoreQueue_delay, _DurabilitySemaphoreQueue_emit, _DurabilitySemaphoreQueue_outbox, _DurabilitySemaphoreQueue_requesting, _DurabilitySemaphoreQueue_blocked, _DurabilitySemaphoreQueue_notifying, _DurabilitySemaphoreQueue_checking, _DurabilitySemaphoreQueue_runDue, _DurabilitySemaphoreQueue_request, _DurabilitySemaphoreQueue_claimLocally, _DurabilitySemaphoreQueue_drawLeases, _DurabilitySemaphoreQueue_returnIdleLeases, _DurabilitySemaphoreQueue_applyLeaseGrants, _DurabilitySemaphoreQueue_applyResults, _DurabilitySemaphoreQueue_refreshKey, _DurabilitySemaphoreQueue_stageReport, _DurabilitySemaphoreQueue_stageLeaseReport, _DurabilitySemaphoreQueue_publishReports, _DurabilitySemaphoreQueue_releaseFinished, _DurabilitySemaphoreQueue_checkFinished, _DurabilitySemaphoreQueue_notify, _DurabilitySemaphoreQueue_notifyBatch, _DurabilitySemaphoreQueue_retryDelay, _DurabilitySemaphoreQueue_deadline;
13
+ import { reportFailure, serializeError } from './errors.js';
14
+ import { durabilitySemaphoreQueueMigrations, migrate, } from './migrations.js';
15
+ import { DurabilityOutbox, stageWithin } from './outbox.js';
16
+ import { assertPositiveInteger, } from './policy.js';
17
+ import { DurabilityScheduler, engineFor, } from './scheduler.js';
18
+ import { deserialize, serialize } from './serialization.js';
19
+ import { QueueStore, } from './semaphore-queue-store.js';
20
+ import { exponential, jitter } from './utils.js';
21
+ const queueItemsTable = {
22
+ name: 'durability_semaphore_queue_items',
23
+ key: 'id',
24
+ };
25
+ const outboxChannel = 'durability.semaphore-queue';
26
+ /** Queued items read per key when building a request's demand. */
27
+ const demandScanLimit = 200;
28
+ const maxDelayMs = 2 ** 31 - 1;
29
+ const pairKey = (key, second) => JSON.stringify([key, second]);
30
+ const parsePairKey = (value) => JSON.parse(value);
31
+ const grantReport = (grant) => ({
32
+ grantId: grant.grant_id,
33
+ claimed: grant.claimed,
34
+ released: grant.released,
35
+ });
36
+ // A lease stops drawing by moving its expiry to now, whoever retired it.
37
+ const retiredLocally = (lease, now) => lease.expires_at <= now;
38
+ const leaseReport = (lease) => ({
39
+ leaseId: lease.lease_id,
40
+ drawn: lease.drawn,
41
+ released: lease.released,
42
+ ...(retiredLocally(lease, Date.now()) ? { retired: true } : {}),
43
+ });
44
+ /**
45
+ * The holder sweeps a lease at its expiry by its own clock; stopping local
46
+ * draws this long before it bounds the window a skewed clock could open.
47
+ */
48
+ const leaseDrawMarginMs = 30000;
49
+ const drawable = (lease, now) => lease.drawn < lease.permits && lease.expires_at - leaseDrawMarginMs > now;
50
+ /**
51
+ * A composable DO capability for the client side of {@link DurabilitySemaphore}:
52
+ * queues items per semaphore key in arrival order, asks the holder for
53
+ * permits, and hands admitted items to the application. Claims and releases
54
+ * are published to the holder through an internal outbox channel as
55
+ * cumulative counts, so nothing is lost or double-counted across retries.
56
+ */
57
+ export class DurabilitySemaphoreQueue {
58
+ constructor(config) {
59
+ _DurabilitySemaphoreQueue_instances.add(this);
60
+ _DurabilitySemaphoreQueue_engine.set(this, void 0);
61
+ _DurabilitySemaphoreQueue_store.set(this, void 0);
62
+ _DurabilitySemaphoreQueue_server.set(this, void 0);
63
+ _DurabilitySemaphoreQueue_clientId.set(this, void 0);
64
+ _DurabilitySemaphoreQueue_admit.set(this, void 0);
65
+ _DurabilitySemaphoreQueue_finished.set(this, void 0);
66
+ _DurabilitySemaphoreQueue_finishedCheckMs.set(this, void 0);
67
+ _DurabilitySemaphoreQueue_maxAdmitBatch.set(this, void 0);
68
+ _DurabilitySemaphoreQueue_attemptTimeoutMs.set(this, void 0);
69
+ _DurabilitySemaphoreQueue_delay.set(this, void 0);
70
+ _DurabilitySemaphoreQueue_emit.set(this, void 0);
71
+ _DurabilitySemaphoreQueue_outbox.set(this, void 0);
72
+ _DurabilitySemaphoreQueue_requesting.set(this, new Set());
73
+ _DurabilitySemaphoreQueue_blocked.set(this, new Map());
74
+ _DurabilitySemaphoreQueue_notifying.set(this, void 0);
75
+ _DurabilitySemaphoreQueue_checking.set(this, void 0);
76
+ if (typeof config.clientId !== 'string' || config.clientId.length === 0) {
77
+ throw new TypeError('clientId must be a non-empty string');
78
+ }
79
+ if (typeof config.admit !== 'function') {
80
+ throw new TypeError('Semaphore queue requires an admit function');
81
+ }
82
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_server, config.server, "f");
83
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_clientId, config.clientId, "f");
84
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_admit, config.admit, "f");
85
+ if (config.finished !== undefined &&
86
+ typeof config.finished !== 'function') {
87
+ throw new TypeError('finished must be a function');
88
+ }
89
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_finished, config.finished, "f");
90
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_finishedCheckMs, config.finishedCheckMs ?? 60000, "f");
91
+ assertPositiveInteger('finishedCheckMs', __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_finishedCheckMs, "f"));
92
+ if (__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_finishedCheckMs, "f") > maxDelayMs) {
93
+ throw new RangeError(`finishedCheckMs must be at most ${maxDelayMs}`);
94
+ }
95
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_maxAdmitBatch, config.maxAdmitBatch ?? 100, "f");
96
+ assertPositiveInteger('maxAdmitBatch', __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_maxAdmitBatch, "f"));
97
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, config.attemptTimeoutMs ?? 60000, "f");
98
+ assertPositiveInteger('attemptTimeoutMs', __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f"));
99
+ if (__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f") > maxDelayMs) {
100
+ throw new RangeError(`attemptTimeoutMs must be at most ${maxDelayMs}`);
101
+ }
102
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_delay, config.retries?.delay ?? ((attempt) => jitter(exponential(attempt))), "f");
103
+ // The internal outbox must share this helper's engine, so a private
104
+ // scheduler is created explicitly instead of letting each attach its own.
105
+ const scheduler = config.scheduler ?? new DurabilityScheduler(config);
106
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_engine, engineFor({ scheduler }), "f");
107
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_store, new QueueStore(__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").sql), "f");
108
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_emit, __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").emitter(config.onLifecycleEvent), "f");
109
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").migrateSchema('semaphoreQueue', durabilitySemaphoreQueueMigrations);
110
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_outbox, new DurabilityOutbox({
111
+ scheduler,
112
+ channel: outboxChannel,
113
+ publish: (entries) => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_publishReports).call(this, entries),
114
+ }), "f");
115
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").register({
116
+ table: queueItemsTable,
117
+ runDue: (now) => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_runDue).call(this, now),
118
+ nextPendingAt: () => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").nextPendingAt(__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_finished, "f") !== undefined),
119
+ });
120
+ }
121
+ static migrate(context, target) {
122
+ return migrate(context.storage, 'semaphoreQueue', durabilitySemaphoreQueueMigrations, target);
123
+ }
124
+ alarm(info) {
125
+ return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").alarm(info);
126
+ }
127
+ /**
128
+ * Durably accepts items. Each is admitted once the holder confirms a
129
+ * permit; items whose key already holds unclaimed permits skip the request.
130
+ */
131
+ async enqueue(items) {
132
+ const rows = items.map((item) => {
133
+ const id = item.id ?? crypto.randomUUID();
134
+ if (typeof id !== 'string' ||
135
+ id.length === 0 ||
136
+ typeof item.key !== 'string' ||
137
+ item.key.length === 0 ||
138
+ (item.group !== undefined && typeof item.group !== 'string')) {
139
+ throw new TypeError('Semaphore items need a non-empty id, a semaphore key, and a string group');
140
+ }
141
+ const now = Date.now();
142
+ return {
143
+ id,
144
+ key: item.key,
145
+ grp: item.group ?? '',
146
+ payload: serialize(item.body),
147
+ status: 'queued',
148
+ grant_id: null,
149
+ lease_id: null,
150
+ created_at: now,
151
+ admitted_at: null,
152
+ notified: 0,
153
+ attempt: 0,
154
+ next_attempt_at: now,
155
+ };
156
+ });
157
+ const { queued, duplicates } = await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transaction(() => {
158
+ const store = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f");
159
+ const inserted = [];
160
+ const existing = [];
161
+ for (const row of rows) {
162
+ if (store.insertItem(row)) {
163
+ inserted.push(row);
164
+ }
165
+ else {
166
+ existing.push(row.id);
167
+ }
168
+ }
169
+ for (const key of new Set(inserted.map((row) => row.key))) {
170
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_claimLocally).call(this, key);
171
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_refreshKey).call(this, key);
172
+ }
173
+ return { queued: inserted, duplicates: existing };
174
+ });
175
+ for (const row of queued) {
176
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_emit, "f").call(this, {
177
+ entityKind: 'semaphore_item',
178
+ type: 'queued',
179
+ timestamp: row.created_at,
180
+ id: row.id,
181
+ key: row.key,
182
+ group: row.grp,
183
+ });
184
+ }
185
+ return { queued: queued.map((row) => row.id), duplicates };
186
+ }
187
+ /** The item finished: its permit returns to the holder. Idempotent. */
188
+ async release(id) {
189
+ const released = await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transaction(() => {
190
+ const store = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f");
191
+ const item = store.getItem(id);
192
+ if (!item || item.status !== 'admitted') {
193
+ return undefined;
194
+ }
195
+ store.removeItem(id);
196
+ if (item.lease_id !== null) {
197
+ const lease = store.getLease(item.lease_id);
198
+ if (lease) {
199
+ const returned = lease.released + 1;
200
+ // A spent lease is retired; its row is dropped later, in #runDue.
201
+ const updated = {
202
+ ...lease,
203
+ released: returned,
204
+ expires_at: returned >= lease.permits
205
+ ? Math.min(lease.expires_at, Date.now())
206
+ : lease.expires_at,
207
+ };
208
+ store.updateLease(updated);
209
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_stageLeaseReport).call(this, updated);
210
+ }
211
+ }
212
+ else if (item.grant_id !== null) {
213
+ const grant = store.getGrant(item.grant_id);
214
+ if (grant) {
215
+ const updated = { ...grant, released: grant.released + 1 };
216
+ store.updateGrant(updated);
217
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_stageReport).call(this, updated);
218
+ }
219
+ }
220
+ return item;
221
+ });
222
+ if (!released) {
223
+ return false;
224
+ }
225
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_emit, "f").call(this, {
226
+ entityKind: 'semaphore_item',
227
+ type: 'released',
228
+ timestamp: Date.now(),
229
+ id,
230
+ key: released.key,
231
+ group: released.grp,
232
+ });
233
+ return true;
234
+ }
235
+ /** Removes a still-queued item. False once it counts against a grant. */
236
+ async cancel(id) {
237
+ const cancelled = await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transaction(() => {
238
+ const store = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f");
239
+ const item = store.getItem(id);
240
+ if (!item || item.status !== 'queued') {
241
+ return undefined;
242
+ }
243
+ store.removeItem(id);
244
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_refreshKey).call(this, item.key);
245
+ return item;
246
+ });
247
+ if (!cancelled) {
248
+ return false;
249
+ }
250
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_emit, "f").call(this, {
251
+ entityKind: 'semaphore_item',
252
+ type: 'cancelled',
253
+ timestamp: Date.now(),
254
+ id,
255
+ key: cancelled.key,
256
+ group: cancelled.grp,
257
+ });
258
+ return true;
259
+ }
260
+ async status(id) {
261
+ const item = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").getItem(id);
262
+ if (!item) {
263
+ return undefined;
264
+ }
265
+ if (item.status === 'admitted') {
266
+ return {
267
+ state: 'admitted',
268
+ enqueuedAt: item.created_at,
269
+ admittedAt: item.admitted_at ?? item.created_at,
270
+ };
271
+ }
272
+ if (item.status === 'claiming') {
273
+ return { state: 'claiming', enqueuedAt: item.created_at };
274
+ }
275
+ const blocked = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_blocked, "f").get(pairKey(item.key, item.grp));
276
+ return blocked === undefined
277
+ ? { state: 'queued', enqueuedAt: item.created_at }
278
+ : { state: 'queued', enqueuedAt: item.created_at, blocked };
279
+ }
280
+ async usage() {
281
+ return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").countItems();
282
+ }
283
+ }
284
+ _DurabilitySemaphoreQueue_engine = new WeakMap(), _DurabilitySemaphoreQueue_store = new WeakMap(), _DurabilitySemaphoreQueue_server = new WeakMap(), _DurabilitySemaphoreQueue_clientId = new WeakMap(), _DurabilitySemaphoreQueue_admit = new WeakMap(), _DurabilitySemaphoreQueue_finished = new WeakMap(), _DurabilitySemaphoreQueue_finishedCheckMs = new WeakMap(), _DurabilitySemaphoreQueue_maxAdmitBatch = new WeakMap(), _DurabilitySemaphoreQueue_attemptTimeoutMs = new WeakMap(), _DurabilitySemaphoreQueue_delay = new WeakMap(), _DurabilitySemaphoreQueue_emit = new WeakMap(), _DurabilitySemaphoreQueue_outbox = new WeakMap(), _DurabilitySemaphoreQueue_requesting = new WeakMap(), _DurabilitySemaphoreQueue_blocked = new WeakMap(), _DurabilitySemaphoreQueue_notifying = new WeakMap(), _DurabilitySemaphoreQueue_checking = new WeakMap(), _DurabilitySemaphoreQueue_instances = new WeakSet(), _DurabilitySemaphoreQueue_runDue = async function _DurabilitySemaphoreQueue_runDue(now) {
285
+ // A spent lease row is kept as long as a late reply could still refer to
286
+ // it; deleted sooner, an older echo of the live lease would insert it again.
287
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transactionSync(() => {
288
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").removeSpentLeases(now - __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f"));
289
+ });
290
+ const keys = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").listDueKeys(now);
291
+ await Promise.all([__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_notify).call(this), __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_releaseFinished).call(this, now)]);
292
+ await Promise.all(keys
293
+ .filter(({ key }) => !__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_requesting, "f").has(key))
294
+ .map(({ key }) => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_request).call(this, key)));
295
+ }, _DurabilitySemaphoreQueue_request =
296
+ /** Asks the holder for permits covering this key's queued items. */
297
+ async function _DurabilitySemaphoreQueue_request(key) {
298
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_requesting, "f").add(key);
299
+ try {
300
+ const store = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f");
301
+ const queued = store.listItems(key, 'queued', demandScanLimit);
302
+ const grants = store.listGrants(key);
303
+ const leases = store.listLeases(key);
304
+ if (queued.length === 0) {
305
+ await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transaction(() => {
306
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_refreshKey).call(this, key);
307
+ });
308
+ return;
309
+ }
310
+ const demand = new Map();
311
+ for (const item of queued) {
312
+ const entry = demand.get(item.grp) ?? {
313
+ count: 0,
314
+ oldestAt: item.created_at,
315
+ };
316
+ entry.count += 1;
317
+ entry.oldestAt = Math.min(entry.oldestAt, item.created_at);
318
+ demand.set(item.grp, entry);
319
+ }
320
+ const message = {
321
+ type: 'request',
322
+ v: 1,
323
+ clientId: __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_clientId, "f"),
324
+ demand: [...demand].map(([group, entry]) => ({ group, ...entry })),
325
+ grants: grants.map(grantReport),
326
+ // Always present: it opts this client into leases when the holder offers them.
327
+ leases: leases.map(leaseReport),
328
+ };
329
+ // While the holder holds the request for its round, the key must not
330
+ // look due: the alarm would find it skipped and re-arm at once, spinning
331
+ // for the whole wait. A crash mid-request leaves it due at the deadline.
332
+ await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transaction(() => {
333
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").upsertKey({
334
+ key,
335
+ queued: queued.length,
336
+ next_request_at: Date.now() + __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f"),
337
+ });
338
+ });
339
+ let grant;
340
+ try {
341
+ grant = await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_deadline).call(this, 'request', () => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_server, "f").request(key, message));
342
+ }
343
+ catch (error) {
344
+ reportFailure('durability.semaphore_queue.request_failed', { key }, error);
345
+ await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transaction(() => {
346
+ const current = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").getKey(key);
347
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").upsertKey({
348
+ key,
349
+ queued: current?.queued ?? queued.length,
350
+ next_request_at: Date.now() + __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_retryDelay).call(this, 1),
351
+ });
352
+ });
353
+ return;
354
+ }
355
+ await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transaction(() => {
356
+ const txStore = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f");
357
+ for (const given of grant.grants) {
358
+ txStore.insertGrant({
359
+ grant_id: given.grantId,
360
+ key,
361
+ grp: given.group,
362
+ permits: given.permits,
363
+ claimed: 0,
364
+ confirmed: 0,
365
+ admitted: 0,
366
+ released: 0,
367
+ created_at: Date.now(),
368
+ });
369
+ }
370
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_applyLeaseGrants).call(this, key, grant.leases ?? []);
371
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_claimLocally).call(this, key);
372
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_applyResults).call(this, key, grant.grantResults);
373
+ if (grant.contended) {
374
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_returnIdleLeases).call(this, key);
375
+ }
376
+ const remaining = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_refreshKey).call(this, key, grant.nextRequestAt);
377
+ for (const group of new Set(remaining.map((item) => item.grp))) {
378
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_blocked, "f").delete(pairKey(key, group));
379
+ }
380
+ for (const { group, reason } of grant.blocked) {
381
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_blocked, "f").set(pairKey(key, group), reason);
382
+ }
383
+ });
384
+ const counts = new Map();
385
+ for (const item of store.listItems(key, 'queued', demandScanLimit)) {
386
+ counts.set(item.grp, (counts.get(item.grp) ?? 0) + 1);
387
+ }
388
+ for (const { group, reason } of grant.blocked) {
389
+ const count = counts.get(group) ?? 0;
390
+ if (count > 0) {
391
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_emit, "f").call(this, {
392
+ entityKind: 'semaphore_item',
393
+ type: 'blocked',
394
+ timestamp: Date.now(),
395
+ key,
396
+ group,
397
+ reason,
398
+ count,
399
+ });
400
+ }
401
+ }
402
+ }
403
+ finally {
404
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_requesting, "f").delete(key);
405
+ }
406
+ }, _DurabilitySemaphoreQueue_claimLocally = function _DurabilitySemaphoreQueue_claimLocally(key) {
407
+ const store = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f");
408
+ const grants = store
409
+ .listGrants(key)
410
+ .filter((grant) => grant.claimed < grant.permits);
411
+ if (grants.length === 0) {
412
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_drawLeases).call(this, key);
413
+ return;
414
+ }
415
+ const queued = store.listItems(key, 'queued', demandScanLimit);
416
+ const touched = new Map();
417
+ for (const item of queued) {
418
+ const grant = grants.find((candidate) => candidate.grp === item.grp && candidate.claimed < candidate.permits);
419
+ if (!grant) {
420
+ continue;
421
+ }
422
+ grant.claimed += 1;
423
+ touched.set(grant.grant_id, grant);
424
+ store.updateItem({
425
+ ...item,
426
+ status: 'claiming',
427
+ grant_id: grant.grant_id,
428
+ });
429
+ }
430
+ for (const grant of touched.values()) {
431
+ store.updateGrant(grant);
432
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_stageReport).call(this, grant);
433
+ }
434
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_drawLeases).call(this, key);
435
+ }, _DurabilitySemaphoreQueue_drawLeases = function _DurabilitySemaphoreQueue_drawLeases(key) {
436
+ const store = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f");
437
+ const now = Date.now();
438
+ const leases = store
439
+ .listLeases(key)
440
+ .filter((lease) => drawable(lease, now));
441
+ if (leases.length === 0) {
442
+ return;
443
+ }
444
+ const queued = store.listItems(key, 'queued', demandScanLimit);
445
+ const touched = new Map();
446
+ for (const item of queued) {
447
+ const lease = leases.find((candidate) => candidate.grp === item.grp && drawable(candidate, now));
448
+ if (!lease) {
449
+ continue;
450
+ }
451
+ lease.drawn += 1;
452
+ touched.set(lease.lease_id, lease);
453
+ store.updateItem({
454
+ ...item,
455
+ status: 'admitted',
456
+ lease_id: lease.lease_id,
457
+ admitted_at: now,
458
+ notified: 0,
459
+ attempt: 0,
460
+ next_attempt_at: now,
461
+ });
462
+ }
463
+ for (const lease of touched.values()) {
464
+ store.updateLease(lease);
465
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_stageLeaseReport).call(this, lease);
466
+ }
467
+ }, _DurabilitySemaphoreQueue_returnIdleLeases = function _DurabilitySemaphoreQueue_returnIdleLeases(key) {
468
+ const store = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f");
469
+ const now = Date.now();
470
+ const waiting = new Set(store.listItems(key, 'queued', demandScanLimit).map((item) => item.grp));
471
+ for (const lease of store.listLeases(key)) {
472
+ if (retiredLocally(lease, now) ||
473
+ lease.drawn >= lease.permits ||
474
+ waiting.has(lease.grp)) {
475
+ continue;
476
+ }
477
+ const returned = { ...lease, permits: lease.drawn, expires_at: now };
478
+ store.updateLease(returned);
479
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_stageLeaseReport).call(this, returned);
480
+ }
481
+ }, _DurabilitySemaphoreQueue_applyLeaseGrants = function _DurabilitySemaphoreQueue_applyLeaseGrants(key, grants) {
482
+ const store = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f");
483
+ const now = Date.now();
484
+ for (const grant of grants) {
485
+ const lease = store.getLease(grant.leaseId);
486
+ if (grant.expiresAt <= now) {
487
+ if (!lease || retiredLocally(lease, now)) {
488
+ continue;
489
+ }
490
+ // The holder keeps counting the whole lease until this report says
491
+ // how much was drawn by the time drawing stopped.
492
+ const retired = { ...lease, permits: lease.drawn, expires_at: now };
493
+ store.updateLease(retired);
494
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_stageLeaseReport).call(this, retired);
495
+ continue;
496
+ }
497
+ if (lease) {
498
+ // A refresh sent before this client retired the lease must not revive it.
499
+ if (!retiredLocally(lease, now)) {
500
+ store.updateLease({ ...lease, expires_at: grant.expiresAt });
501
+ }
502
+ continue;
503
+ }
504
+ store.insertLease({
505
+ lease_id: grant.leaseId,
506
+ key,
507
+ grp: grant.group,
508
+ permits: grant.permits,
509
+ drawn: 0,
510
+ released: 0,
511
+ created_at: now,
512
+ expires_at: grant.expiresAt,
513
+ });
514
+ }
515
+ }, _DurabilitySemaphoreQueue_applyResults = function _DurabilitySemaphoreQueue_applyResults(key, results) {
516
+ const store = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f");
517
+ const now = Date.now();
518
+ for (const result of results) {
519
+ const grant = store.getGrant(result.grantId);
520
+ if (!grant || grant.key !== key) {
521
+ continue;
522
+ }
523
+ const claiming = store
524
+ .listItemsByGrant(grant.grant_id)
525
+ .filter((item) => item.status === 'claiming');
526
+ const confirmed = result.status === 'not_found'
527
+ ? grant.admitted
528
+ : Math.max(grant.confirmed, result.claimed);
529
+ const admitNow = claiming.slice(0, Math.max(0, confirmed - grant.admitted));
530
+ for (const item of admitNow) {
531
+ store.updateItem({
532
+ ...item,
533
+ status: 'admitted',
534
+ admitted_at: now,
535
+ notified: 0,
536
+ attempt: 0,
537
+ next_attempt_at: now,
538
+ });
539
+ }
540
+ let updated = {
541
+ ...grant,
542
+ confirmed,
543
+ admitted: grant.admitted + admitNow.length,
544
+ };
545
+ if (result.status !== 'ok') {
546
+ // The holder reclaimed (or forgot) what was not confirmed: those items
547
+ // queue again, and the grant shrinks to what was actually admitted.
548
+ for (const item of claiming.slice(admitNow.length)) {
549
+ store.updateItem({ ...item, status: 'queued', grant_id: null });
550
+ }
551
+ updated = {
552
+ ...updated,
553
+ permits: updated.admitted,
554
+ claimed: updated.admitted,
555
+ confirmed: updated.admitted,
556
+ };
557
+ }
558
+ if (updated.released >= updated.permits &&
559
+ updated.confirmed >= updated.permits) {
560
+ store.removeGrant(grant.grant_id);
561
+ }
562
+ else {
563
+ store.updateGrant(updated);
564
+ }
565
+ }
566
+ }, _DurabilitySemaphoreQueue_refreshKey = function _DurabilitySemaphoreQueue_refreshKey(key, nextRequestAt) {
567
+ const store = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f");
568
+ const queued = store.listItems(key, 'queued', demandScanLimit);
569
+ const current = store.getKey(key);
570
+ if (queued.length === 0 && !current && nextRequestAt === undefined) {
571
+ return queued;
572
+ }
573
+ store.upsertKey({
574
+ key,
575
+ queued: queued.length,
576
+ next_request_at: nextRequestAt ?? current?.next_request_at ?? Date.now(),
577
+ });
578
+ return queued;
579
+ }, _DurabilitySemaphoreQueue_stageReport = function _DurabilitySemaphoreQueue_stageReport(grant) {
580
+ stageWithin(__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_outbox, "f"), __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").sql, pairKey(grant.key, `grant:${grant.grant_id}`), { kind: 'grant', claimed: grant.claimed, released: grant.released });
581
+ }, _DurabilitySemaphoreQueue_stageLeaseReport = function _DurabilitySemaphoreQueue_stageLeaseReport(lease) {
582
+ stageWithin(__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_outbox, "f"), __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").sql, pairKey(lease.key, `lease:${lease.lease_id}`), {
583
+ kind: 'lease',
584
+ drawn: lease.drawn,
585
+ released: lease.released,
586
+ ...(retiredLocally(lease, Date.now()) ? { retired: true } : {}),
587
+ });
588
+ }, _DurabilitySemaphoreQueue_publishReports =
589
+ /** Outbox sink: one `sync` per key; a failed key keeps its entries pending. */
590
+ async function _DurabilitySemaphoreQueue_publishReports(entries) {
591
+ const byKey = new Map();
592
+ for (const entry of entries) {
593
+ const [key] = parsePairKey(entry.key);
594
+ byKey.set(key, [...(byKey.get(key) ?? []), entry]);
595
+ }
596
+ await Promise.all([...byKey].map(async ([key, keyEntries]) => {
597
+ const grants = [];
598
+ const leases = [];
599
+ for (const entry of keyEntries) {
600
+ const id = parsePairKey(entry.key)[1].slice(entry.value.kind.length + 1);
601
+ if (entry.value.kind === 'grant') {
602
+ grants.push({
603
+ grantId: id,
604
+ claimed: entry.value.claimed,
605
+ released: entry.value.released,
606
+ });
607
+ }
608
+ else {
609
+ leases.push({
610
+ leaseId: id,
611
+ drawn: entry.value.drawn,
612
+ released: entry.value.released,
613
+ ...(entry.value.retired ? { retired: true } : {}),
614
+ });
615
+ }
616
+ }
617
+ let synced;
618
+ try {
619
+ synced = await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_deadline).call(this, 'sync', () => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_server, "f").sync(key, {
620
+ type: 'sync',
621
+ v: 1,
622
+ clientId: __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_clientId, "f"),
623
+ grants,
624
+ leases,
625
+ }));
626
+ }
627
+ catch (error) {
628
+ reportFailure('durability.semaphore_queue.sync_failed', { key }, error);
629
+ for (const entry of keyEntries) {
630
+ entry.retry();
631
+ }
632
+ return;
633
+ }
634
+ await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transaction(() => {
635
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_applyResults).call(this, key, synced.grantResults);
636
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_applyLeaseGrants).call(this, key, synced.leases ?? []);
637
+ if (synced.contended) {
638
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_returnIdleLeases).call(this, key);
639
+ }
640
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_refreshKey).call(this, key);
641
+ });
642
+ }));
643
+ const onError = (error) => reportFailure('durability.semaphore_queue.notify_failed', {}, error);
644
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").waitUntil(__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_notify).call(this).catch(onError), onError);
645
+ }, _DurabilitySemaphoreQueue_releaseFinished = function _DurabilitySemaphoreQueue_releaseFinished(now) {
646
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_checking, __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_checking, "f") ?? __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_checkFinished).call(this, now).finally(() => {
647
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_checking, undefined, "f");
648
+ }), "f");
649
+ return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_checking, "f");
650
+ }, _DurabilitySemaphoreQueue_checkFinished = async function _DurabilitySemaphoreQueue_checkFinished(now) {
651
+ const finished = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_finished, "f");
652
+ // An idle run must not write: every transaction reconciles the alarm.
653
+ if (!finished || __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").listCheckDue(now, 1).length === 0) {
654
+ return;
655
+ }
656
+ // Schedule the next check before asking, as admit() claims its batch: rows
657
+ // left due while finished() runs would re-arm the alarm at once.
658
+ const later = now + __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_finishedCheckMs, "f");
659
+ const due = await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transaction(() => {
660
+ const rows = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").listCheckDue(now, __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_maxAdmitBatch, "f"));
661
+ for (const row of rows) {
662
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").updateItem({ ...row, next_attempt_at: later });
663
+ }
664
+ return rows;
665
+ });
666
+ if (due.length === 0) {
667
+ return;
668
+ }
669
+ let done = [];
670
+ try {
671
+ done = await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_deadline).call(this, 'finished', () => finished(due.map((row) => row.id)));
672
+ }
673
+ catch (error) {
674
+ reportFailure('durability.semaphore_queue.finished_failed', { count: due.length }, error);
675
+ }
676
+ const asked = new Set(due.map((row) => row.id));
677
+ for (const id of new Set(done)) {
678
+ if (asked.has(id)) {
679
+ // eslint-disable-next-line no-await-in-loop
680
+ await this.release(id);
681
+ }
682
+ }
683
+ }, _DurabilitySemaphoreQueue_notify = function _DurabilitySemaphoreQueue_notify() {
684
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_notifying, __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_notifying, "f") ?? __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_notifyBatch).call(this).finally(() => {
685
+ __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_notifying, undefined, "f");
686
+ }), "f");
687
+ return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_notifying, "f");
688
+ }, _DurabilitySemaphoreQueue_notifyBatch = async function _DurabilitySemaphoreQueue_notifyBatch() {
689
+ const store = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f");
690
+ const due = store.listUnnotified(Date.now(), __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_maxAdmitBatch, "f"));
691
+ if (due.length === 0) {
692
+ return;
693
+ }
694
+ const release = await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").acquirePermit();
695
+ try {
696
+ const deadline = Date.now() + __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f");
697
+ // Claim the batch so a concurrent alarm cannot notify the same items.
698
+ const claimed = await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transaction(() => {
699
+ const rows = [];
700
+ for (const item of due) {
701
+ const current = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").getItem(item.id);
702
+ if (!current || current.status !== 'admitted' || current.notified) {
703
+ continue;
704
+ }
705
+ const row = {
706
+ ...current,
707
+ attempt: current.attempt + 1,
708
+ next_attempt_at: deadline,
709
+ };
710
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").updateItem(row);
711
+ rows.push(row);
712
+ }
713
+ return rows;
714
+ });
715
+ if (claimed.length === 0) {
716
+ return;
717
+ }
718
+ const admitted = claimed.map((row) => ({
719
+ id: row.id,
720
+ key: row.key,
721
+ group: row.grp,
722
+ body: deserialize(row.payload),
723
+ enqueuedAt: row.created_at,
724
+ admittedAt: row.admitted_at ?? row.created_at,
725
+ via: row.lease_id === null ? 'grant' : 'lease',
726
+ }));
727
+ let failure;
728
+ try {
729
+ await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_deadline).call(this, 'admit', () => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_admit, "f").call(this, admitted));
730
+ }
731
+ catch (error) {
732
+ failure = error;
733
+ }
734
+ await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transaction(() => {
735
+ for (const row of claimed) {
736
+ const current = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").getItem(row.id);
737
+ if (!current || current.attempt !== row.attempt) {
738
+ continue;
739
+ }
740
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").updateItem(failure === undefined
741
+ ? {
742
+ ...current,
743
+ notified: 1,
744
+ next_attempt_at: Date.now() + __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_finishedCheckMs, "f"),
745
+ }
746
+ : {
747
+ ...current,
748
+ next_attempt_at: Date.now() + __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_retryDelay).call(this, row.attempt),
749
+ });
750
+ }
751
+ });
752
+ if (failure !== undefined) {
753
+ reportFailure('durability.semaphore_queue.admit_failed', { count: claimed.length, error: serializeError(failure).message }, failure);
754
+ return;
755
+ }
756
+ for (const item of admitted) {
757
+ __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_emit, "f").call(this, {
758
+ entityKind: 'semaphore_item',
759
+ type: 'admitted',
760
+ timestamp: item.admittedAt,
761
+ id: item.id,
762
+ key: item.key,
763
+ group: item.group,
764
+ waitMs: Math.max(0, item.admittedAt - item.enqueuedAt),
765
+ });
766
+ }
767
+ }
768
+ finally {
769
+ release();
770
+ }
771
+ }, _DurabilitySemaphoreQueue_retryDelay = function _DurabilitySemaphoreQueue_retryDelay(attempt) {
772
+ try {
773
+ const delay = Math.round(__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_delay, "f").call(this, attempt));
774
+ if (Number.isSafeInteger(delay) && delay >= 0 && delay <= maxDelayMs) {
775
+ return delay;
776
+ }
777
+ }
778
+ catch (error) {
779
+ reportFailure('durability.semaphore_queue.retry_policy_failed', {}, error);
780
+ }
781
+ return 60000;
782
+ }, _DurabilitySemaphoreQueue_deadline = async function _DurabilitySemaphoreQueue_deadline(what, run) {
783
+ let timer;
784
+ try {
785
+ return await Promise.race([
786
+ Promise.resolve().then(run),
787
+ new Promise((_resolve, reject) => {
788
+ timer = setTimeout(() => reject(new Error(`Semaphore ${what} timed out after ${__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f")}ms`)), __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f"));
789
+ }),
790
+ ]);
791
+ }
792
+ finally {
793
+ if (timer !== undefined) {
794
+ clearTimeout(timer);
795
+ }
796
+ }
797
+ };