durability 5.1.0 → 5.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.
- package/README.md +145 -12
- package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +13 -5
- package/dist/{durability-alarms.js → alarms/index.js} +69 -55
- package/dist/alarms/migrations.d.ts +9 -0
- package/dist/alarms/migrations.js +133 -0
- package/dist/experimental.d.ts +10 -10
- package/dist/experimental.js +7 -7
- package/dist/{fanout.d.ts → fanout/index.d.ts} +13 -6
- package/dist/{fanout.js → fanout/index.js} +54 -46
- package/dist/fanout/migrations.d.ts +9 -0
- package/dist/fanout/migrations.js +181 -0
- package/dist/{stored-message.d.ts → fanout/stored-message.d.ts} +1 -1
- package/dist/index.d.ts +14 -9
- package/dist/index.js +13 -8
- package/dist/{log.d.ts → log/index.d.ts} +15 -3
- package/dist/{log.js → log/index.js} +152 -124
- package/dist/log/migrations.d.ts +18 -0
- package/dist/log/migrations.js +297 -0
- package/dist/{memoize.d.ts → memoize/index.d.ts} +1 -1
- package/dist/{memoize.js → memoize/index.js} +14 -11
- package/dist/memoize/migrations.d.ts +10 -0
- package/dist/memoize/migrations.js +21 -0
- package/dist/migrations.d.ts +0 -111
- package/dist/migrations.js +0 -612
- package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
- package/dist/{durability.js → operations/index.js} +59 -47
- package/dist/operations/migrations.d.ts +10 -0
- package/dist/operations/migrations.js +150 -0
- package/dist/{outbox.d.ts → outbox/index.d.ts} +4 -4
- package/dist/{outbox.js → outbox/index.js} +16 -60
- package/dist/outbox/migrations.d.ts +9 -0
- package/dist/outbox/migrations.js +43 -0
- package/dist/{routing-client.js → routing/client.js} +1 -1
- package/dist/{routing.d.ts → routing/index.d.ts} +3 -3
- package/dist/{routing.js → routing/index.js} +3 -3
- package/dist/{load.d.ts → routing/load.d.ts} +1 -1
- package/dist/{schedule.d.ts → schedule/index.d.ts} +16 -8
- package/dist/{schedule.js → schedule/index.js} +65 -49
- package/dist/schedule/migrations.d.ts +9 -0
- package/dist/schedule/migrations.js +118 -0
- package/dist/{scheduler.d.ts → scheduler/index.d.ts} +75 -25
- package/dist/{scheduler.js → scheduler/index.js} +300 -63
- package/dist/{semaphore-client.d.ts → semaphore/client.d.ts} +1 -1
- package/dist/{semaphore-client.js → semaphore/client.js} +2 -2
- package/dist/{semaphore.d.ts → semaphore/index.d.ts} +10 -10
- package/dist/{semaphore.js → semaphore/index.js} +7 -7
- package/dist/{semaphore-leases.d.ts → semaphore/leases.d.ts} +17 -6
- package/dist/{semaphore-leases.js → semaphore/leases.js} +28 -12
- package/dist/semaphore/migrations.d.ts +18 -0
- package/dist/semaphore/migrations.js +444 -0
- package/dist/{semaphore-protocol.d.ts → semaphore/protocol.d.ts} +16 -8
- package/dist/{semaphore-protocol.js → semaphore/protocol.js} +37 -14
- package/dist/{semaphore-queue-store.d.ts → semaphore/queue-store.d.ts} +12 -6
- package/dist/{semaphore-queue-store.js → semaphore/queue-store.js} +69 -61
- package/dist/{semaphore-queue.d.ts → semaphore/queue.d.ts} +15 -5
- package/dist/{semaphore-queue.js → semaphore/queue.js} +105 -46
- package/dist/{semaphore-server.d.ts → semaphore/server.d.ts} +12 -4
- package/dist/{semaphore-server.js → semaphore/server.js} +51 -24
- package/dist/{semaphore-store.d.ts → semaphore/store.d.ts} +8 -3
- package/dist/{semaphore-store.js → semaphore/store.js} +35 -33
- package/dist/{client.d.ts → socket/client.d.ts} +3 -3
- package/dist/{client.js → socket/client.js} +4 -4
- package/dist/{socket.d.ts → socket/index.d.ts} +15 -4
- package/dist/{socket.js → socket/index.js} +66 -20
- package/dist/socket/migrations.d.ts +9 -0
- package/dist/socket/migrations.js +57 -0
- package/dist/{socket-protocol.d.ts → socket/protocol.d.ts} +1 -1
- package/dist/storage.d.ts +4 -0
- package/dist/storage.js +13 -6
- package/dist/{topology-client.d.ts → topology/client.d.ts} +2 -2
- package/dist/{topology-client.js → topology/client.js} +3 -3
- package/dist/{topology.d.ts → topology/index.d.ts} +17 -7
- package/dist/{topology.js → topology/index.js} +92 -58
- package/dist/topology/migrations.d.ts +10 -0
- package/dist/topology/migrations.js +138 -0
- package/dist/{tracker.d.ts → tracker/index.d.ts} +3 -3
- package/dist/{tracker.js → tracker/index.js} +2 -2
- package/package.json +11 -11
- /package/dist/{stored-message.js → fanout/stored-message.js} +0 -0
- /package/dist/{routing-client.d.ts → routing/client.d.ts} +0 -0
- /package/dist/{load.js → routing/load.js} +0 -0
- /package/dist/{socket-protocol.js → socket/protocol.js} +0 -0
- /package/dist/{topology-snapshot.d.ts → topology/snapshot.d.ts} +0 -0
- /package/dist/{topology-snapshot.js → topology/snapshot.js} +0 -0
- /package/dist/{tracked-source.d.ts → tracker/source.d.ts} +0 -0
- /package/dist/{tracked-source.js → tracker/source.js} +0 -0
|
@@ -9,21 +9,29 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
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
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _DurabilitySemaphoreQueue_instances, _DurabilitySemaphoreQueue_engine, _DurabilitySemaphoreQueue_store, _DurabilitySemaphoreQueue_server,
|
|
13
|
-
import { traceLifecycle, traceMethods } from '
|
|
14
|
-
import { reportFailure, serializeError } from '
|
|
15
|
-
import { durabilitySemaphoreQueueMigrations
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
12
|
+
var _DurabilitySemaphoreQueue_instances, _DurabilitySemaphoreQueue_engine, _DurabilitySemaphoreQueue_store, _DurabilitySemaphoreQueue_server, _DurabilitySemaphoreQueue_identity, _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_retryAfter, _DurabilitySemaphoreQueue_acknowledged, _DurabilitySemaphoreQueue_request, _DurabilitySemaphoreQueue_claimLocally, _DurabilitySemaphoreQueue_drawLeases, _DurabilitySemaphoreQueue_returnIdleLeases, _DurabilitySemaphoreQueue_applyLeaseGrants, _DurabilitySemaphoreQueue_renewLeases, _DurabilitySemaphoreQueue_applyResults, _DurabilitySemaphoreQueue_refreshKey, _DurabilitySemaphoreQueue_stageReport, _DurabilitySemaphoreQueue_stageLeaseReport, _DurabilitySemaphoreQueue_publishReports, _DurabilitySemaphoreQueue_releaseFinished, _DurabilitySemaphoreQueue_checkFinished, _DurabilitySemaphoreQueue_notify, _DurabilitySemaphoreQueue_notifyBatch, _DurabilitySemaphoreQueue_retryDelay, _DurabilitySemaphoreQueue_deadline;
|
|
13
|
+
import { traceLifecycle, traceMethods } from '../dev.js';
|
|
14
|
+
import { SemaphoreProtocolError, reportFailure, serializeError, } from '../errors.js';
|
|
15
|
+
import { durabilitySemaphoreQueueMigrations } from './migrations.js';
|
|
16
|
+
import { migrateCapability, } from '../migrations.js';
|
|
17
|
+
import { DurabilityOutbox, stageWithin, } from '../outbox/index.js';
|
|
18
|
+
import { assertPositiveInteger, } from '../policy.js';
|
|
19
|
+
import { DurabilityScheduler, assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js';
|
|
20
|
+
import { deserialize, serialize } from '../serialization.js';
|
|
21
|
+
import { QueueStore, } from './queue-store.js';
|
|
22
|
+
import { exponential, jitter } from '../utils.js';
|
|
22
23
|
const queueItemsTable = {
|
|
23
24
|
name: 'durability_semaphore_queue_items',
|
|
24
25
|
key: 'id',
|
|
25
26
|
};
|
|
26
|
-
|
|
27
|
+
/**
|
|
28
|
+
* The internal outbox channel carrying a queue's reports. The default queue
|
|
29
|
+
* keeps the name earlier versions staged reports under; a named queue's is a
|
|
30
|
+
* JSON tuple, so no user string can make two queues share one.
|
|
31
|
+
*/
|
|
32
|
+
const outboxChannelFor = (channel) => channel === defaultChannel
|
|
33
|
+
? 'durability.semaphore-queue'
|
|
34
|
+
: JSON.stringify(['durability.semaphore-queue', channel]);
|
|
27
35
|
/** Half the holder's default grantTimeoutMs. */
|
|
28
36
|
const maxReportRetryMs = 5000;
|
|
29
37
|
/** Queued items read per key when building a request's demand. */
|
|
@@ -72,7 +80,7 @@ export class DurabilitySemaphoreQueue {
|
|
|
72
80
|
_DurabilitySemaphoreQueue_engine.set(this, void 0);
|
|
73
81
|
_DurabilitySemaphoreQueue_store.set(this, void 0);
|
|
74
82
|
_DurabilitySemaphoreQueue_server.set(this, void 0);
|
|
75
|
-
|
|
83
|
+
_DurabilitySemaphoreQueue_identity.set(this, void 0);
|
|
76
84
|
_DurabilitySemaphoreQueue_admit.set(this, void 0);
|
|
77
85
|
_DurabilitySemaphoreQueue_finished.set(this, void 0);
|
|
78
86
|
_DurabilitySemaphoreQueue_finishedCheckMs.set(this, void 0);
|
|
@@ -85,6 +93,7 @@ export class DurabilitySemaphoreQueue {
|
|
|
85
93
|
_DurabilitySemaphoreQueue_blocked.set(this, new Map());
|
|
86
94
|
_DurabilitySemaphoreQueue_notifying.set(this, void 0);
|
|
87
95
|
_DurabilitySemaphoreQueue_checking.set(this, void 0);
|
|
96
|
+
const channel = assertChannel('Semaphore queue', config.channel ?? defaultChannel);
|
|
88
97
|
if (typeof config.clientId !== 'string' || config.clientId.length === 0) {
|
|
89
98
|
throw new TypeError('clientId must be a non-empty string');
|
|
90
99
|
}
|
|
@@ -92,7 +101,13 @@ export class DurabilitySemaphoreQueue {
|
|
|
92
101
|
throw new TypeError('Semaphore queue requires an admit function');
|
|
93
102
|
}
|
|
94
103
|
__classPrivateFieldSet(this, _DurabilitySemaphoreQueue_server, config.server, "f");
|
|
95
|
-
__classPrivateFieldSet(this,
|
|
104
|
+
__classPrivateFieldSet(this, _DurabilitySemaphoreQueue_identity, channel === defaultChannel
|
|
105
|
+
? { v: 1, clientId: config.clientId }
|
|
106
|
+
: {
|
|
107
|
+
v: 2,
|
|
108
|
+
clientId: config.clientId,
|
|
109
|
+
clientChannel: channel,
|
|
110
|
+
}, "f");
|
|
96
111
|
__classPrivateFieldSet(this, _DurabilitySemaphoreQueue_admit, config.admit, "f");
|
|
97
112
|
if (config.finished !== undefined &&
|
|
98
113
|
typeof config.finished !== 'function') {
|
|
@@ -116,12 +131,12 @@ export class DurabilitySemaphoreQueue {
|
|
|
116
131
|
// scheduler is created explicitly instead of letting each attach its own.
|
|
117
132
|
const scheduler = config.scheduler ?? new DurabilityScheduler(config);
|
|
118
133
|
__classPrivateFieldSet(this, _DurabilitySemaphoreQueue_engine, engineFor({ scheduler }), "f");
|
|
119
|
-
__classPrivateFieldSet(this, _DurabilitySemaphoreQueue_store, new QueueStore(__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").sql), "f");
|
|
134
|
+
__classPrivateFieldSet(this, _DurabilitySemaphoreQueue_store, new QueueStore(__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").sql, channel), "f");
|
|
120
135
|
__classPrivateFieldSet(this, _DurabilitySemaphoreQueue_emit, traceLifecycle(__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").emitter(config.onLifecycleEvent)), "f");
|
|
121
136
|
__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").migrateSchema('semaphoreQueue', durabilitySemaphoreQueueMigrations);
|
|
122
137
|
__classPrivateFieldSet(this, _DurabilitySemaphoreQueue_outbox, new DurabilityOutbox({
|
|
123
138
|
scheduler,
|
|
124
|
-
channel:
|
|
139
|
+
channel: outboxChannelFor(channel),
|
|
125
140
|
publish: (entries) => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_publishReports).call(this, entries),
|
|
126
141
|
// A claim the holder has not heard about within its grant window is
|
|
127
142
|
// reclaimed, so reports retry well inside it instead of backing off for minutes.
|
|
@@ -129,10 +144,24 @@ export class DurabilitySemaphoreQueue {
|
|
|
129
144
|
delay: (attempt) => Math.min(__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_retryDelay).call(this, attempt), maxReportRetryMs),
|
|
130
145
|
},
|
|
131
146
|
}), "f");
|
|
132
|
-
__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").
|
|
147
|
+
__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").registerChannel({
|
|
133
148
|
table: queueItemsTable,
|
|
149
|
+
channelTables: [
|
|
150
|
+
'durability_semaphore_queue_grants',
|
|
151
|
+
'durability_semaphore_queue_keys',
|
|
152
|
+
'durability_semaphore_queue_leases',
|
|
153
|
+
],
|
|
154
|
+
channel,
|
|
155
|
+
helper: 'A semaphore queue',
|
|
134
156
|
runDue: (now) => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_runDue).call(this, now),
|
|
135
|
-
nextPendingAt: () => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").nextPendingAt(
|
|
157
|
+
nextPendingAt: () => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").nextPendingAt({
|
|
158
|
+
unnotified: true,
|
|
159
|
+
notified: __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_finished, "f") !== undefined,
|
|
160
|
+
}),
|
|
161
|
+
nextTimerAt: () => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").nextPendingAt({
|
|
162
|
+
unnotified: __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_notifying, "f") === undefined,
|
|
163
|
+
notified: __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_finished, "f") !== undefined && __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_checking, "f") === undefined,
|
|
164
|
+
}),
|
|
136
165
|
});
|
|
137
166
|
}
|
|
138
167
|
static migrate(context, target) {
|
|
@@ -298,7 +327,7 @@ export class DurabilitySemaphoreQueue {
|
|
|
298
327
|
return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").countItems();
|
|
299
328
|
}
|
|
300
329
|
}
|
|
301
|
-
_DurabilitySemaphoreQueue_engine = new WeakMap(), _DurabilitySemaphoreQueue_store = new WeakMap(), _DurabilitySemaphoreQueue_server = new WeakMap(),
|
|
330
|
+
_DurabilitySemaphoreQueue_engine = new WeakMap(), _DurabilitySemaphoreQueue_store = new WeakMap(), _DurabilitySemaphoreQueue_server = new WeakMap(), _DurabilitySemaphoreQueue_identity = 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) {
|
|
302
331
|
// A spent lease row is kept as long as a late reply could still refer to
|
|
303
332
|
// it; deleted sooner, an older echo of the live lease would insert it again.
|
|
304
333
|
__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transactionSync(() => {
|
|
@@ -309,23 +338,30 @@ _DurabilitySemaphoreQueue_engine = new WeakMap(), _DurabilitySemaphoreQueue_stor
|
|
|
309
338
|
__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_notify).call(this),
|
|
310
339
|
__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_releaseFinished).call(this, now),
|
|
311
340
|
__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_renewLeases).call(this, now),
|
|
341
|
+
...keys
|
|
342
|
+
.filter(({ key }) => !__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_requesting, "f").has(key))
|
|
343
|
+
.map(({ key }) => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_request).call(this, key)),
|
|
312
344
|
]);
|
|
313
|
-
|
|
314
|
-
.filter(({ key }) => !__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_requesting, "f").has(key))
|
|
315
|
-
.map(({ key }) => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_request).call(this, key)));
|
|
316
|
-
}, _DurabilitySemaphoreQueue_retrySoon =
|
|
345
|
+
}, _DurabilitySemaphoreQueue_retryAfter =
|
|
317
346
|
/** Best effort: if this write fails too, the request deadline still applies. */
|
|
318
|
-
async function
|
|
347
|
+
async function _DurabilitySemaphoreQueue_retryAfter(key, queued, delay) {
|
|
319
348
|
await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f")
|
|
320
349
|
.transaction(() => {
|
|
321
350
|
const current = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").getKey(key);
|
|
322
351
|
__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").upsertKey({
|
|
323
352
|
key,
|
|
324
353
|
queued: current?.queued ?? queued,
|
|
325
|
-
next_request_at: Date.now() +
|
|
354
|
+
next_request_at: Date.now() + delay,
|
|
326
355
|
});
|
|
327
356
|
})
|
|
328
357
|
.catch(() => undefined);
|
|
358
|
+
}, _DurabilitySemaphoreQueue_acknowledged = function _DurabilitySemaphoreQueue_acknowledged(key, response) {
|
|
359
|
+
if (response.v === __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_identity, "f").v &&
|
|
360
|
+
response.clientChannel === __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_identity, "f").clientChannel) {
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
reportFailure('durability.semaphore_queue.channel_not_acknowledged', { key, channel: __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_identity, "f").clientChannel ?? defaultChannel }, new SemaphoreProtocolError(`The holder of "${key}" did not acknowledge semaphore queue channel "${__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_identity, "f").clientChannel ?? defaultChannel}"`));
|
|
364
|
+
return false;
|
|
329
365
|
}, _DurabilitySemaphoreQueue_request =
|
|
330
366
|
/** Asks the holder for permits covering this key's queued items. */
|
|
331
367
|
async function _DurabilitySemaphoreQueue_request(key) {
|
|
@@ -353,11 +389,10 @@ async function _DurabilitySemaphoreQueue_request(key) {
|
|
|
353
389
|
}
|
|
354
390
|
const message = {
|
|
355
391
|
type: 'request',
|
|
356
|
-
|
|
357
|
-
clientId: __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_clientId, "f"),
|
|
392
|
+
...__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_identity, "f"),
|
|
358
393
|
demand: [...demand].map(([group, entry]) => ({ group, ...entry })),
|
|
359
394
|
grants: grants.map(grantReport),
|
|
360
|
-
//
|
|
395
|
+
// Version 2 makes old holders reject named queues before allocating leases.
|
|
361
396
|
leases: leases.map(leaseReport),
|
|
362
397
|
};
|
|
363
398
|
// While the holder holds the request for its round, the key must not
|
|
@@ -376,7 +411,14 @@ async function _DurabilitySemaphoreQueue_request(key) {
|
|
|
376
411
|
}
|
|
377
412
|
catch (error) {
|
|
378
413
|
reportFailure('durability.semaphore_queue.request_failed', { key }, error);
|
|
379
|
-
await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m",
|
|
414
|
+
await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_retryAfter).call(this, key, queued.length, __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_retryDelay).call(this, 1));
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
if (!__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_acknowledged).call(this, key, grant)) {
|
|
418
|
+
// Unclaimed grants return to the holder's pool at its grant timeout;
|
|
419
|
+
// delay another request when a holder answers with a mismatched
|
|
420
|
+
// version or channel instead of repeatedly reserving more capacity.
|
|
421
|
+
await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_retryAfter).call(this, key, queued.length, __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f"));
|
|
380
422
|
return;
|
|
381
423
|
}
|
|
382
424
|
try {
|
|
@@ -413,7 +455,7 @@ async function _DurabilitySemaphoreQueue_request(key) {
|
|
|
413
455
|
catch (error) {
|
|
414
456
|
// The grant is lost with the transaction; ask again soon rather than
|
|
415
457
|
// at the request deadline, which only covers a crash.
|
|
416
|
-
await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m",
|
|
458
|
+
await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_retryAfter).call(this, key, queued.length, __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_retryDelay).call(this, 1));
|
|
417
459
|
throw error;
|
|
418
460
|
}
|
|
419
461
|
const counts = new Map();
|
|
@@ -683,8 +725,7 @@ async function _DurabilitySemaphoreQueue_publishReports(entries) {
|
|
|
683
725
|
try {
|
|
684
726
|
synced = await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_deadline).call(this, 'sync', () => __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_server, "f").sync(key, {
|
|
685
727
|
type: 'sync',
|
|
686
|
-
|
|
687
|
-
clientId: __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_clientId, "f"),
|
|
728
|
+
...__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_identity, "f"),
|
|
688
729
|
grants,
|
|
689
730
|
leases,
|
|
690
731
|
}));
|
|
@@ -696,6 +737,14 @@ async function _DurabilitySemaphoreQueue_publishReports(entries) {
|
|
|
696
737
|
}
|
|
697
738
|
return;
|
|
698
739
|
}
|
|
740
|
+
if (!__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_acknowledged).call(this, key, synced)) {
|
|
741
|
+
// The reports stay pending: claims stay unconfirmed and items stay
|
|
742
|
+
// stored until a holder that knows the channel answers.
|
|
743
|
+
for (const entry of keyEntries) {
|
|
744
|
+
entry.retry();
|
|
745
|
+
}
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
699
748
|
await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").transaction(() => {
|
|
700
749
|
__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_applyResults).call(this, key, synced.grantResults);
|
|
701
750
|
__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_applyLeaseGrants).call(this, key, synced.leases ?? []);
|
|
@@ -707,17 +756,22 @@ async function _DurabilitySemaphoreQueue_publishReports(entries) {
|
|
|
707
756
|
}));
|
|
708
757
|
const onError = (error) => reportFailure('durability.semaphore_queue.notify_failed', {}, error);
|
|
709
758
|
__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").waitUntil(__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_notify).call(this).catch(onError), onError);
|
|
710
|
-
}, _DurabilitySemaphoreQueue_releaseFinished =
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
}
|
|
759
|
+
}, _DurabilitySemaphoreQueue_releaseFinished =
|
|
760
|
+
/** Releases admitted items `finished` reports done, one check at a time. */
|
|
761
|
+
async function _DurabilitySemaphoreQueue_releaseFinished(now) {
|
|
762
|
+
if (__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_checking, "f")) {
|
|
763
|
+
return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_checking, "f");
|
|
764
|
+
}
|
|
716
765
|
const finished = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_finished, "f");
|
|
717
|
-
// An idle run must not write: every transaction reconciles the alarm.
|
|
718
766
|
if (!finished || __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").listCheckDue(now, 1).length === 0) {
|
|
719
767
|
return;
|
|
720
768
|
}
|
|
769
|
+
__classPrivateFieldSet(this, _DurabilitySemaphoreQueue_checking, __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_checkFinished).call(this, now, finished).finally(() => {
|
|
770
|
+
__classPrivateFieldSet(this, _DurabilitySemaphoreQueue_checking, undefined, "f");
|
|
771
|
+
return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").scheduleNextAlarm();
|
|
772
|
+
}), "f");
|
|
773
|
+
return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_checking, "f");
|
|
774
|
+
}, _DurabilitySemaphoreQueue_checkFinished = async function _DurabilitySemaphoreQueue_checkFinished(now, finished) {
|
|
721
775
|
// Schedule the next check before asking, as admit() claims its batch: rows
|
|
722
776
|
// left due while finished() runs would re-arm the alarm at once.
|
|
723
777
|
const later = now + __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_finishedCheckMs, "f");
|
|
@@ -745,17 +799,22 @@ async function _DurabilitySemaphoreQueue_publishReports(entries) {
|
|
|
745
799
|
await this.release(id);
|
|
746
800
|
}
|
|
747
801
|
}
|
|
748
|
-
}, _DurabilitySemaphoreQueue_notify =
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
}
|
|
754
|
-
const
|
|
755
|
-
const due = store.listUnnotified(Date.now(), __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_maxAdmitBatch, "f"));
|
|
802
|
+
}, _DurabilitySemaphoreQueue_notify =
|
|
803
|
+
/** Hands admitted, unnotified items to `admit()`, one batch at a time. */
|
|
804
|
+
async function _DurabilitySemaphoreQueue_notify() {
|
|
805
|
+
if (__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_notifying, "f")) {
|
|
806
|
+
return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_notifying, "f");
|
|
807
|
+
}
|
|
808
|
+
const due = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f").listUnnotified(Date.now(), __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_maxAdmitBatch, "f"));
|
|
756
809
|
if (due.length === 0) {
|
|
757
810
|
return;
|
|
758
811
|
}
|
|
812
|
+
__classPrivateFieldSet(this, _DurabilitySemaphoreQueue_notifying, __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_notifyBatch).call(this, due).finally(() => {
|
|
813
|
+
__classPrivateFieldSet(this, _DurabilitySemaphoreQueue_notifying, undefined, "f");
|
|
814
|
+
return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").scheduleNextAlarm();
|
|
815
|
+
}), "f");
|
|
816
|
+
return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_notifying, "f");
|
|
817
|
+
}, _DurabilitySemaphoreQueue_notifyBatch = async function _DurabilitySemaphoreQueue_notifyBatch(due) {
|
|
759
818
|
const release = await __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").acquirePermit();
|
|
760
819
|
try {
|
|
761
820
|
const deadline = Date.now() + __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f");
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type DurabilityMigrationResult } from '
|
|
2
|
-
import { type LeaseEvent, type SemaphoreLeaseOptions } from './
|
|
3
|
-
import { type SchedulerAttachment } from '
|
|
4
|
-
import { type SemaphoreBlockedReason, type SemaphoreRequest, type SemaphoreResponse } from './
|
|
1
|
+
import { type DurabilityMigrationResult } from '../migrations.js';
|
|
2
|
+
import { type LeaseEvent, type SemaphoreLeaseOptions } from './leases.js';
|
|
3
|
+
import { type SchedulerAttachment } from '../scheduler/index.js';
|
|
4
|
+
import { type SemaphoreBlockedReason, type SemaphoreRequest, type SemaphoreResponse } from './protocol.js';
|
|
5
5
|
export type DurabilitySemaphoreEvent = {
|
|
6
6
|
entityKind: 'semaphore';
|
|
7
7
|
type: 'round';
|
|
@@ -24,11 +24,19 @@ export type DurabilitySemaphoreEvent = {
|
|
|
24
24
|
timestamp: number;
|
|
25
25
|
grantId: string;
|
|
26
26
|
clientId: string;
|
|
27
|
+
clientChannel?: string;
|
|
27
28
|
group: string;
|
|
28
29
|
/** Unclaimed permits returned to the pool. */
|
|
29
30
|
permits: number;
|
|
30
31
|
};
|
|
31
32
|
export type DurabilitySemaphoreConfig = SchedulerAttachment & {
|
|
33
|
+
/**
|
|
34
|
+
* Stable pool ID. Semaphores on one object share storage, partitioned by
|
|
35
|
+
* channel, and each holds its own limit, grants, and leases. The
|
|
36
|
+
* application routes each request to a pool through its own RPC method.
|
|
37
|
+
* Defaults to `default`.
|
|
38
|
+
*/
|
|
39
|
+
channel?: string;
|
|
32
40
|
/** Evaluated once per round. Lowering it never evicts running work. */
|
|
33
41
|
limit: number | (() => number | Promise<number>);
|
|
34
42
|
/**
|
|
@@ -10,14 +10,15 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _DurabilitySemaphoreServer_instances, _DurabilitySemaphoreServer_engine, _DurabilitySemaphoreServer_store, _DurabilitySemaphoreServer_limit, _DurabilitySemaphoreServer_groupLimit, _DurabilitySemaphoreServer_canGrant, _DurabilitySemaphoreServer_roundWaitMs, _DurabilitySemaphoreServer_roundPeriodMs, _DurabilitySemaphoreServer_grantTimeoutMs, _DurabilitySemaphoreServer_leases, _DurabilitySemaphoreServer_emit, _DurabilitySemaphoreServer_pending, _DurabilitySemaphoreServer_roundTimer, _DurabilitySemaphoreServer_round, _DurabilitySemaphoreServer_lastRoundAt, _DurabilitySemaphoreServer_contended, _DurabilitySemaphoreServer_grantRoom, _DurabilitySemaphoreServer_scheduleRound, _DurabilitySemaphoreServer_runRound, _DurabilitySemaphoreServer_allocate, _DurabilitySemaphoreServer_sync, _DurabilitySemaphoreServer_applyReports, _DurabilitySemaphoreServer_reclaim, _DurabilitySemaphoreServer_reclaimExpired, _DurabilitySemaphoreServer_resolveLimit, _DurabilitySemaphoreServer_groupPolicy;
|
|
13
|
-
import { traceLifecycle, traceMethods } from '
|
|
14
|
-
import { SemaphoreProtocolError, reportFailure } from '
|
|
15
|
-
import { durabilitySemaphoreMigrations
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
13
|
+
import { traceLifecycle, traceMethods } from '../dev.js';
|
|
14
|
+
import { SemaphoreProtocolError, reportFailure } from '../errors.js';
|
|
15
|
+
import { durabilitySemaphoreMigrations } from './migrations.js';
|
|
16
|
+
import { migrateCapability, } from '../migrations.js';
|
|
17
|
+
import { assertPositiveInteger } from '../policy.js';
|
|
18
|
+
import { allocateLeases, applyLeaseReports, budgets, clientKey, leaseFootprint, ownedBy, resolveLeasePolicy, } from './leases.js';
|
|
19
|
+
import { assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js';
|
|
20
|
+
import { semaphoreRequestSchema, } from './protocol.js';
|
|
21
|
+
import { HolderStore } from './store.js';
|
|
21
22
|
const grantsTable = {
|
|
22
23
|
name: 'durability_semaphore_grants',
|
|
23
24
|
key: 'grant_id',
|
|
@@ -27,6 +28,20 @@ const maxDelayMs = 2 ** 31 - 1;
|
|
|
27
28
|
/** Unclaimed permits are reclaimed once the claim window passes. */
|
|
28
29
|
const effectivePermits = (grant, now) => now >= grant.expires_at ? grant.claimed : grant.permits;
|
|
29
30
|
const outstanding = (grant, now) => Math.max(0, effectivePermits(grant, now) - grant.released);
|
|
31
|
+
const clientOf = (message) => ({
|
|
32
|
+
clientId: message.clientId,
|
|
33
|
+
clientChannel: message.clientChannel ?? defaultChannel,
|
|
34
|
+
});
|
|
35
|
+
/**
|
|
36
|
+
* Echoes the protocol identity so the client can verify which queue was counted.
|
|
37
|
+
* Version 1 retains the response shape older clients expect.
|
|
38
|
+
*/
|
|
39
|
+
const acknowledgement = (message) => ({
|
|
40
|
+
v: message.v,
|
|
41
|
+
...(message.clientChannel === undefined
|
|
42
|
+
? {}
|
|
43
|
+
: { clientChannel: message.clientChannel }),
|
|
44
|
+
});
|
|
30
45
|
/** Permits left queued per (group, reason) across one round's allocations. */
|
|
31
46
|
const blockedCounts = (allocations) => {
|
|
32
47
|
const counts = new Map();
|
|
@@ -75,6 +90,7 @@ export class DurabilitySemaphoreServer {
|
|
|
75
90
|
_DurabilitySemaphoreServer_lastRoundAt.set(this, -Infinity);
|
|
76
91
|
// From the last round; tells clients in later syncs to return idle lease headroom.
|
|
77
92
|
_DurabilitySemaphoreServer_contended.set(this, false);
|
|
93
|
+
const channel = assertChannel('Semaphore', config.channel ?? defaultChannel);
|
|
78
94
|
const limit = config.limit;
|
|
79
95
|
__classPrivateFieldSet(this, _DurabilitySemaphoreServer_limit, typeof limit === 'function' ? limit : () => limit, "f");
|
|
80
96
|
if (typeof limit === 'number' &&
|
|
@@ -100,9 +116,12 @@ export class DurabilitySemaphoreServer {
|
|
|
100
116
|
__classPrivateFieldSet(this, _DurabilitySemaphoreServer_engine, engineFor(config), "f");
|
|
101
117
|
__classPrivateFieldSet(this, _DurabilitySemaphoreServer_emit, traceLifecycle(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").emitter(config.onLifecycleEvent)), "f");
|
|
102
118
|
__classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").migrateSchema('semaphore', durabilitySemaphoreMigrations);
|
|
103
|
-
__classPrivateFieldSet(this, _DurabilitySemaphoreServer_store, new HolderStore(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").sql), "f");
|
|
104
|
-
__classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").
|
|
119
|
+
__classPrivateFieldSet(this, _DurabilitySemaphoreServer_store, new HolderStore(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").sql, channel), "f");
|
|
120
|
+
__classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").registerChannel({
|
|
105
121
|
table: grantsTable,
|
|
122
|
+
channelTables: ['durability_semaphore_leases'],
|
|
123
|
+
channel,
|
|
124
|
+
helper: 'A semaphore',
|
|
106
125
|
runDue: () => __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_reclaimExpired).call(this),
|
|
107
126
|
nextPendingAt: () => __classPrivateFieldGet(this, _DurabilitySemaphoreServer_store, "f").nextPendingAt(),
|
|
108
127
|
});
|
|
@@ -129,7 +148,12 @@ export class DurabilitySemaphoreServer {
|
|
|
129
148
|
}
|
|
130
149
|
const request = parsed.data;
|
|
131
150
|
return new Promise((resolve, reject) => {
|
|
132
|
-
__classPrivateFieldGet(this, _DurabilitySemaphoreServer_pending, "f").push({
|
|
151
|
+
__classPrivateFieldGet(this, _DurabilitySemaphoreServer_pending, "f").push({
|
|
152
|
+
request,
|
|
153
|
+
client: clientOf(request),
|
|
154
|
+
resolve,
|
|
155
|
+
reject,
|
|
156
|
+
});
|
|
133
157
|
__classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_scheduleRound).call(this);
|
|
134
158
|
});
|
|
135
159
|
}
|
|
@@ -223,7 +247,7 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
|
|
|
223
247
|
const { leases, ...rest } = allocation;
|
|
224
248
|
entry.resolve({
|
|
225
249
|
type: 'grant',
|
|
226
|
-
|
|
250
|
+
...acknowledgement(entry.request),
|
|
227
251
|
...rest,
|
|
228
252
|
nextRequestAt,
|
|
229
253
|
...(entry.request.leases === undefined
|
|
@@ -258,11 +282,10 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
|
|
|
258
282
|
let unknownDebt = 0;
|
|
259
283
|
const drawn = new Map();
|
|
260
284
|
for (const entry of pending) {
|
|
261
|
-
const
|
|
262
|
-
const grantResults = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_applyReports).call(this, store, clientId, entry.request.grants, now);
|
|
285
|
+
const grantResults = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_applyReports).call(this, store, entry.client, entry.request.grants, now);
|
|
263
286
|
const leases = [];
|
|
264
287
|
if (__classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f") && entry.request.leases) {
|
|
265
|
-
const outcome = applyLeaseReports(store, __classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f"),
|
|
288
|
+
const outcome = applyLeaseReports(store, __classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f"), entry.client, entry.request.leases, now);
|
|
266
289
|
leases.push(...outcome.echoes);
|
|
267
290
|
unknownDebt += outcome.unknownDebt;
|
|
268
291
|
for (const [leaseId, count] of outcome.drawn) {
|
|
@@ -293,7 +316,8 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
|
|
|
293
316
|
const demands = pending
|
|
294
317
|
.flatMap((entry) => entry.request.demand.map((demand) => ({ entry, ...demand })))
|
|
295
318
|
.sort((left, right) => left.oldestAt - right.oldestAt ||
|
|
296
|
-
left.entry.
|
|
319
|
+
left.entry.client.clientId.localeCompare(right.entry.client.clientId) ||
|
|
320
|
+
left.entry.client.clientChannel.localeCompare(right.entry.client.clientChannel));
|
|
297
321
|
let requested = 0;
|
|
298
322
|
let granted = 0;
|
|
299
323
|
for (const demand of demands) {
|
|
@@ -313,7 +337,8 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
|
|
|
313
337
|
if (give > 0) {
|
|
314
338
|
const grant = {
|
|
315
339
|
grant_id: crypto.randomUUID(),
|
|
316
|
-
client_id: demand.entry.
|
|
340
|
+
client_id: demand.entry.client.clientId,
|
|
341
|
+
client_channel: demand.entry.client.clientChannel,
|
|
317
342
|
grp: demand.group,
|
|
318
343
|
permits: give,
|
|
319
344
|
claimed: 0,
|
|
@@ -345,7 +370,7 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
|
|
|
345
370
|
const candidates = pending.flatMap((entry) => entry.request.leases === undefined
|
|
346
371
|
? []
|
|
347
372
|
: entry.request.demand.map((demand) => ({
|
|
348
|
-
|
|
373
|
+
...entry.client,
|
|
349
374
|
group: demand.group,
|
|
350
375
|
oldestAt: demand.oldestAt,
|
|
351
376
|
})));
|
|
@@ -373,7 +398,7 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
|
|
|
373
398
|
// One echo per lease; the allocator's view (e.g. retired) supersedes the report's.
|
|
374
399
|
const byLease = new Map([
|
|
375
400
|
...allocation.leases,
|
|
376
|
-
...(outcome.grants.get(entry.
|
|
401
|
+
...(outcome.grants.get(clientKey(entry.client.clientId, entry.client.clientChannel)) ?? []),
|
|
377
402
|
].map((lease) => [lease.leaseId, lease]));
|
|
378
403
|
allocation.leases = [...byLease.values()];
|
|
379
404
|
}
|
|
@@ -407,17 +432,18 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
|
|
|
407
432
|
};
|
|
408
433
|
}, _DurabilitySemaphoreServer_sync = async function _DurabilitySemaphoreServer_sync(message) {
|
|
409
434
|
const now = Date.now();
|
|
435
|
+
const client = clientOf(message);
|
|
410
436
|
const result = await __classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").transaction(() => {
|
|
411
437
|
const store = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_store, "f");
|
|
412
|
-
const grantResults = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_applyReports).call(this, store,
|
|
438
|
+
const grantResults = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_applyReports).call(this, store, client, message.grants, now);
|
|
413
439
|
const leaseOutcome = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f") && message.leases
|
|
414
|
-
? applyLeaseReports(store, __classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f"),
|
|
440
|
+
? applyLeaseReports(store, __classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f"), client, message.leases, now)
|
|
415
441
|
: undefined;
|
|
416
442
|
return { grantResults, leaseEchoes: leaseOutcome?.echoes };
|
|
417
443
|
});
|
|
418
444
|
return {
|
|
419
445
|
type: 'synced',
|
|
420
|
-
|
|
446
|
+
...acknowledgement(message),
|
|
421
447
|
grantResults: result.grantResults,
|
|
422
448
|
...(result.leaseEchoes === undefined
|
|
423
449
|
? {}
|
|
@@ -426,11 +452,11 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
|
|
|
426
452
|
...(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_contended, "f") ? { contended: true } : {}),
|
|
427
453
|
}),
|
|
428
454
|
};
|
|
429
|
-
}, _DurabilitySemaphoreServer_applyReports = function _DurabilitySemaphoreServer_applyReports(store,
|
|
455
|
+
}, _DurabilitySemaphoreServer_applyReports = function _DurabilitySemaphoreServer_applyReports(store, client, reports, now) {
|
|
430
456
|
const results = [];
|
|
431
457
|
for (const report of reports) {
|
|
432
458
|
const grant = store.get(report.grantId);
|
|
433
|
-
if (!grant || grant
|
|
459
|
+
if (!grant || !ownedBy(grant, client)) {
|
|
434
460
|
results.push({
|
|
435
461
|
grantId: report.grantId,
|
|
436
462
|
claimed: 0,
|
|
@@ -478,6 +504,7 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
|
|
|
478
504
|
timestamp: now,
|
|
479
505
|
grantId: grant.grant_id,
|
|
480
506
|
clientId: grant.client_id,
|
|
507
|
+
clientChannel: grant.client_channel,
|
|
481
508
|
group: grant.grp,
|
|
482
509
|
permits: grant.permits - grant.claimed,
|
|
483
510
|
});
|
|
@@ -2,6 +2,7 @@ import { z } from 'zod';
|
|
|
2
2
|
export declare const grantRowSchema: z.ZodObject<{
|
|
3
3
|
grant_id: z.ZodString;
|
|
4
4
|
client_id: z.ZodString;
|
|
5
|
+
client_channel: z.ZodString;
|
|
5
6
|
grp: z.ZodString;
|
|
6
7
|
permits: z.ZodNumber;
|
|
7
8
|
claimed: z.ZodNumber;
|
|
@@ -14,6 +15,7 @@ export type GrantRow = z.infer<typeof grantRowSchema>;
|
|
|
14
15
|
export declare const leaseRowSchema: z.ZodObject<{
|
|
15
16
|
lease_id: z.ZodString;
|
|
16
17
|
client_id: z.ZodString;
|
|
18
|
+
client_channel: z.ZodString;
|
|
17
19
|
grp: z.ZodString;
|
|
18
20
|
permits: z.ZodNumber;
|
|
19
21
|
released: z.ZodNumber;
|
|
@@ -23,12 +25,15 @@ export declare const leaseRowSchema: z.ZodObject<{
|
|
|
23
25
|
}, z.core.$strip>;
|
|
24
26
|
export type LeaseRow = z.infer<typeof leaseRowSchema>;
|
|
25
27
|
/**
|
|
26
|
-
* Permits
|
|
27
|
-
*
|
|
28
|
+
* Permits one semaphore channel has handed out. Every query is scoped to the
|
|
29
|
+
* channel, so semaphores sharing an object never see each other's grants or
|
|
30
|
+
* leases. Grants and leases are small and bounded by live clients, so
|
|
31
|
+
* aggregates are computed from `list()`.
|
|
28
32
|
*/
|
|
29
33
|
export declare class HolderStore {
|
|
30
34
|
private readonly sql;
|
|
31
|
-
|
|
35
|
+
private readonly channel;
|
|
36
|
+
constructor(sql: SqlStorage, channel: string);
|
|
32
37
|
list(): GrantRow[];
|
|
33
38
|
get(grantId: string): GrantRow | undefined;
|
|
34
39
|
nextPendingAt(): number | undefined;
|