durability 5.1.0 → 5.2.1

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.
Files changed (98) hide show
  1. package/README.md +154 -21
  2. package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +13 -5
  3. package/dist/{durability-alarms.js → alarms/index.js} +85 -74
  4. package/dist/alarms/migrations.d.ts +9 -0
  5. package/dist/alarms/migrations.js +133 -0
  6. package/dist/experimental.d.ts +10 -10
  7. package/dist/experimental.js +7 -7
  8. package/dist/{fanout.d.ts → fanout/index.d.ts} +13 -6
  9. package/dist/{fanout.js → fanout/index.js} +91 -83
  10. package/dist/fanout/migrations.d.ts +9 -0
  11. package/dist/fanout/migrations.js +181 -0
  12. package/dist/{stored-message.d.ts → fanout/stored-message.d.ts} +1 -1
  13. package/dist/index.d.ts +14 -9
  14. package/dist/index.js +13 -8
  15. package/dist/{log.d.ts → log/index.d.ts} +27 -20
  16. package/dist/{log.js → log/index.js} +200 -166
  17. package/dist/log/migrations.d.ts +18 -0
  18. package/dist/log/migrations.js +297 -0
  19. package/dist/{memoize.d.ts → memoize/index.d.ts} +1 -1
  20. package/dist/{memoize.js → memoize/index.js} +23 -21
  21. package/dist/memoize/migrations.d.ts +10 -0
  22. package/dist/memoize/migrations.js +21 -0
  23. package/dist/migrations.d.ts +0 -111
  24. package/dist/migrations.js +12 -628
  25. package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
  26. package/dist/{durability.js → operations/index.js} +76 -70
  27. package/dist/operations/migrations.d.ts +10 -0
  28. package/dist/operations/migrations.js +150 -0
  29. package/dist/{outbox.d.ts → outbox/index.d.ts} +4 -4
  30. package/dist/{outbox.js → outbox/index.js} +35 -77
  31. package/dist/outbox/migrations.d.ts +9 -0
  32. package/dist/outbox/migrations.js +43 -0
  33. package/dist/policy.d.ts +19 -19
  34. package/dist/policy.js +13 -12
  35. package/dist/{routing-client.js → routing/client.js} +1 -1
  36. package/dist/{routing.d.ts → routing/index.d.ts} +3 -3
  37. package/dist/{routing.js → routing/index.js} +3 -3
  38. package/dist/{load.d.ts → routing/load.d.ts} +1 -1
  39. package/dist/{schedule.d.ts → schedule/index.d.ts} +16 -8
  40. package/dist/{schedule.js → schedule/index.js} +97 -83
  41. package/dist/schedule/migrations.d.ts +9 -0
  42. package/dist/schedule/migrations.js +118 -0
  43. package/dist/{scheduler.d.ts → scheduler/index.d.ts} +75 -25
  44. package/dist/{scheduler.js → scheduler/index.js} +308 -70
  45. package/dist/{semaphore-client.d.ts → semaphore/client.d.ts} +1 -1
  46. package/dist/{semaphore-client.js → semaphore/client.js} +8 -7
  47. package/dist/{semaphore.d.ts → semaphore/index.d.ts} +10 -10
  48. package/dist/{semaphore.js → semaphore/index.js} +7 -7
  49. package/dist/{semaphore-leases.d.ts → semaphore/leases.d.ts} +17 -6
  50. package/dist/{semaphore-leases.js → semaphore/leases.js} +28 -12
  51. package/dist/semaphore/migrations.d.ts +18 -0
  52. package/dist/semaphore/migrations.js +444 -0
  53. package/dist/semaphore/protocol.d.ts +451 -0
  54. package/dist/semaphore/protocol.js +114 -0
  55. package/dist/semaphore/queue-store.d.ts +107 -0
  56. package/dist/semaphore/queue-store.js +258 -0
  57. package/dist/{semaphore-queue.d.ts → semaphore/queue.d.ts} +15 -5
  58. package/dist/{semaphore-queue.js → semaphore/queue.js} +105 -46
  59. package/dist/{semaphore-server.d.ts → semaphore/server.d.ts} +12 -4
  60. package/dist/{semaphore-server.js → semaphore/server.js} +56 -28
  61. package/dist/semaphore/store.d.ts +53 -0
  62. package/dist/semaphore/store.js +107 -0
  63. package/dist/serialization.d.ts +5 -0
  64. package/dist/serialization.js +41 -16
  65. package/dist/{client.d.ts → socket/client.d.ts} +3 -3
  66. package/dist/{client.js → socket/client.js} +4 -4
  67. package/dist/{socket.d.ts → socket/index.d.ts} +15 -4
  68. package/dist/{socket.js → socket/index.js} +66 -20
  69. package/dist/socket/migrations.d.ts +9 -0
  70. package/dist/socket/migrations.js +57 -0
  71. package/dist/{socket-protocol.d.ts → socket/protocol.d.ts} +1 -1
  72. package/dist/storage.d.ts +4 -0
  73. package/dist/storage.js +22 -15
  74. package/dist/{topology-client.d.ts → topology/client.d.ts} +2 -2
  75. package/dist/{topology-client.js → topology/client.js} +3 -3
  76. package/dist/{topology.d.ts → topology/index.d.ts} +17 -7
  77. package/dist/{topology.js → topology/index.js} +106 -72
  78. package/dist/topology/migrations.d.ts +10 -0
  79. package/dist/topology/migrations.js +138 -0
  80. package/dist/topology/snapshot.d.ts +50 -0
  81. package/dist/topology/snapshot.js +38 -0
  82. package/dist/{tracker.d.ts → tracker/index.d.ts} +3 -3
  83. package/dist/{tracker.js → tracker/index.js} +9 -9
  84. package/package.json +11 -11
  85. package/dist/semaphore-protocol.d.ts +0 -231
  86. package/dist/semaphore-protocol.js +0 -106
  87. package/dist/semaphore-queue-store.d.ts +0 -95
  88. package/dist/semaphore-queue-store.js +0 -252
  89. package/dist/semaphore-store.d.ts +0 -43
  90. package/dist/semaphore-store.js +0 -108
  91. package/dist/topology-snapshot.d.ts +0 -70
  92. package/dist/topology-snapshot.js +0 -48
  93. /package/dist/{stored-message.js → fanout/stored-message.js} +0 -0
  94. /package/dist/{routing-client.d.ts → routing/client.d.ts} +0 -0
  95. /package/dist/{load.js → routing/load.js} +0 -0
  96. /package/dist/{socket-protocol.js → socket/protocol.js} +0 -0
  97. /package/dist/{tracked-source.d.ts → tracker/source.d.ts} +0 -0
  98. /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, _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_retrySoon, _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 { reportFailure, serializeError } from './errors.js';
15
- import { durabilitySemaphoreQueueMigrations, migrateCapability, } from './migrations.js';
16
- import { DurabilityOutbox, stageWithin } from './outbox.js';
17
- import { assertPositiveInteger, } from './policy.js';
18
- import { DurabilityScheduler, engineFor, } from './scheduler.js';
19
- import { deserialize, serialize } from './serialization.js';
20
- import { QueueStore, } from './semaphore-queue-store.js';
21
- import { exponential, jitter } from './utils.js';
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
- const outboxChannel = 'durability.semaphore-queue';
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
- _DurabilitySemaphoreQueue_clientId.set(this, void 0);
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, _DurabilitySemaphoreQueue_clientId, config.clientId, "f");
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: outboxChannel,
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").register({
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(__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_finished, "f") !== undefined),
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(), _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) {
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
- await Promise.all(keys
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 _DurabilitySemaphoreQueue_retrySoon(key, queued) {
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() + __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_retryDelay).call(this, 1),
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
- v: 1,
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
- // Always present: it opts this client into leases when the holder offers them.
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", _DurabilitySemaphoreQueue_retrySoon).call(this, key, queued.length);
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", _DurabilitySemaphoreQueue_retrySoon).call(this, key, queued.length);
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
- v: 1,
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 = function _DurabilitySemaphoreQueue_releaseFinished(now) {
711
- __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_checking, __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_checking, "f") ?? __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_checkFinished).call(this, now).finally(() => {
712
- __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_checking, undefined, "f");
713
- }), "f");
714
- return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_checking, "f");
715
- }, _DurabilitySemaphoreQueue_checkFinished = async function _DurabilitySemaphoreQueue_checkFinished(now) {
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 = function _DurabilitySemaphoreQueue_notify() {
749
- __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_notifying, __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_notifying, "f") ?? __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_instances, "m", _DurabilitySemaphoreQueue_notifyBatch).call(this).finally(() => {
750
- __classPrivateFieldSet(this, _DurabilitySemaphoreQueue_notifying, undefined, "f");
751
- }), "f");
752
- return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_notifying, "f");
753
- }, _DurabilitySemaphoreQueue_notifyBatch = async function _DurabilitySemaphoreQueue_notifyBatch() {
754
- const store = __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_store, "f");
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 './migrations.js';
2
- import { type LeaseEvent, type SemaphoreLeaseOptions } from './semaphore-leases.js';
3
- import { type SchedulerAttachment } from './scheduler.js';
4
- import { type SemaphoreBlockedReason, type SemaphoreRequest, type SemaphoreResponse } from './semaphore-protocol.js';
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,16 @@ 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 './dev.js';
14
- import { SemaphoreProtocolError, reportFailure } from './errors.js';
15
- import { durabilitySemaphoreMigrations, migrateCapability, } from './migrations.js';
16
- import { assertPositiveInteger } from './policy.js';
17
- import { allocateLeases, applyLeaseReports, budgets, leaseFootprint, resolveLeasePolicy, } from './semaphore-leases.js';
18
- import { engineFor, } from './scheduler.js';
19
- import { semaphoreRequestSchema, } from './semaphore-protocol.js';
20
- import { HolderStore } from './semaphore-store.js';
13
+ import * as v from 'valibot';
14
+ import { traceLifecycle, traceMethods } from '../dev.js';
15
+ import { SemaphoreProtocolError, reportFailure } from '../errors.js';
16
+ import { durabilitySemaphoreMigrations } from './migrations.js';
17
+ import { migrateCapability, } from '../migrations.js';
18
+ import { assertPositiveInteger } from '../policy.js';
19
+ import { allocateLeases, applyLeaseReports, budgets, clientKey, leaseFootprint, ownedBy, resolveLeasePolicy, } from './leases.js';
20
+ import { assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js';
21
+ import { semaphoreRequestSchema, } from './protocol.js';
22
+ import { HolderStore } from './store.js';
21
23
  const grantsTable = {
22
24
  name: 'durability_semaphore_grants',
23
25
  key: 'grant_id',
@@ -27,6 +29,20 @@ const maxDelayMs = 2 ** 31 - 1;
27
29
  /** Unclaimed permits are reclaimed once the claim window passes. */
28
30
  const effectivePermits = (grant, now) => now >= grant.expires_at ? grant.claimed : grant.permits;
29
31
  const outstanding = (grant, now) => Math.max(0, effectivePermits(grant, now) - grant.released);
32
+ const clientOf = (message) => ({
33
+ clientId: message.clientId,
34
+ clientChannel: message.clientChannel ?? defaultChannel,
35
+ });
36
+ /**
37
+ * Echoes the protocol identity so the client can verify which queue was counted.
38
+ * Version 1 retains the response shape older clients expect.
39
+ */
40
+ const acknowledgement = (message) => ({
41
+ v: message.v,
42
+ ...(message.clientChannel === undefined
43
+ ? {}
44
+ : { clientChannel: message.clientChannel }),
45
+ });
30
46
  /** Permits left queued per (group, reason) across one round's allocations. */
31
47
  const blockedCounts = (allocations) => {
32
48
  const counts = new Map();
@@ -75,6 +91,7 @@ export class DurabilitySemaphoreServer {
75
91
  _DurabilitySemaphoreServer_lastRoundAt.set(this, -Infinity);
76
92
  // From the last round; tells clients in later syncs to return idle lease headroom.
77
93
  _DurabilitySemaphoreServer_contended.set(this, false);
94
+ const channel = assertChannel('Semaphore', config.channel ?? defaultChannel);
78
95
  const limit = config.limit;
79
96
  __classPrivateFieldSet(this, _DurabilitySemaphoreServer_limit, typeof limit === 'function' ? limit : () => limit, "f");
80
97
  if (typeof limit === 'number' &&
@@ -100,9 +117,12 @@ export class DurabilitySemaphoreServer {
100
117
  __classPrivateFieldSet(this, _DurabilitySemaphoreServer_engine, engineFor(config), "f");
101
118
  __classPrivateFieldSet(this, _DurabilitySemaphoreServer_emit, traceLifecycle(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").emitter(config.onLifecycleEvent)), "f");
102
119
  __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").register({
120
+ __classPrivateFieldSet(this, _DurabilitySemaphoreServer_store, new HolderStore(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").sql, channel), "f");
121
+ __classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").registerChannel({
105
122
  table: grantsTable,
123
+ channelTables: ['durability_semaphore_leases'],
124
+ channel,
125
+ helper: 'A semaphore',
106
126
  runDue: () => __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_reclaimExpired).call(this),
107
127
  nextPendingAt: () => __classPrivateFieldGet(this, _DurabilitySemaphoreServer_store, "f").nextPendingAt(),
108
128
  });
@@ -120,16 +140,21 @@ export class DurabilitySemaphoreServer {
120
140
  * fails and the client asks again.
121
141
  */
122
142
  handle(message) {
123
- const parsed = semaphoreRequestSchema.safeParse(message);
143
+ const parsed = v.safeParse(semaphoreRequestSchema, message);
124
144
  if (!parsed.success) {
125
145
  return Promise.reject(new SemaphoreProtocolError('Malformed semaphore message'));
126
146
  }
127
- if (parsed.data.type === 'sync') {
128
- return __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_sync).call(this, parsed.data);
147
+ if (parsed.output.type === 'sync') {
148
+ return __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_sync).call(this, parsed.output);
129
149
  }
130
- const request = parsed.data;
150
+ const request = parsed.output;
131
151
  return new Promise((resolve, reject) => {
132
- __classPrivateFieldGet(this, _DurabilitySemaphoreServer_pending, "f").push({ request, resolve, reject });
152
+ __classPrivateFieldGet(this, _DurabilitySemaphoreServer_pending, "f").push({
153
+ request,
154
+ client: clientOf(request),
155
+ resolve,
156
+ reject,
157
+ });
133
158
  __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_scheduleRound).call(this);
134
159
  });
135
160
  }
@@ -223,7 +248,7 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
223
248
  const { leases, ...rest } = allocation;
224
249
  entry.resolve({
225
250
  type: 'grant',
226
- v: 1,
251
+ ...acknowledgement(entry.request),
227
252
  ...rest,
228
253
  nextRequestAt,
229
254
  ...(entry.request.leases === undefined
@@ -258,11 +283,10 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
258
283
  let unknownDebt = 0;
259
284
  const drawn = new Map();
260
285
  for (const entry of pending) {
261
- const { clientId } = entry.request;
262
- const grantResults = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_applyReports).call(this, store, clientId, entry.request.grants, now);
286
+ const grantResults = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_applyReports).call(this, store, entry.client, entry.request.grants, now);
263
287
  const leases = [];
264
288
  if (__classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f") && entry.request.leases) {
265
- const outcome = applyLeaseReports(store, __classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f"), clientId, entry.request.leases, now);
289
+ const outcome = applyLeaseReports(store, __classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f"), entry.client, entry.request.leases, now);
266
290
  leases.push(...outcome.echoes);
267
291
  unknownDebt += outcome.unknownDebt;
268
292
  for (const [leaseId, count] of outcome.drawn) {
@@ -293,7 +317,8 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
293
317
  const demands = pending
294
318
  .flatMap((entry) => entry.request.demand.map((demand) => ({ entry, ...demand })))
295
319
  .sort((left, right) => left.oldestAt - right.oldestAt ||
296
- left.entry.request.clientId.localeCompare(right.entry.request.clientId));
320
+ left.entry.client.clientId.localeCompare(right.entry.client.clientId) ||
321
+ left.entry.client.clientChannel.localeCompare(right.entry.client.clientChannel));
297
322
  let requested = 0;
298
323
  let granted = 0;
299
324
  for (const demand of demands) {
@@ -313,7 +338,8 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
313
338
  if (give > 0) {
314
339
  const grant = {
315
340
  grant_id: crypto.randomUUID(),
316
- client_id: demand.entry.request.clientId,
341
+ client_id: demand.entry.client.clientId,
342
+ client_channel: demand.entry.client.clientChannel,
317
343
  grp: demand.group,
318
344
  permits: give,
319
345
  claimed: 0,
@@ -345,7 +371,7 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
345
371
  const candidates = pending.flatMap((entry) => entry.request.leases === undefined
346
372
  ? []
347
373
  : entry.request.demand.map((demand) => ({
348
- clientId: entry.request.clientId,
374
+ ...entry.client,
349
375
  group: demand.group,
350
376
  oldestAt: demand.oldestAt,
351
377
  })));
@@ -373,7 +399,7 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
373
399
  // One echo per lease; the allocator's view (e.g. retired) supersedes the report's.
374
400
  const byLease = new Map([
375
401
  ...allocation.leases,
376
- ...(outcome.grants.get(entry.request.clientId) ?? []),
402
+ ...(outcome.grants.get(clientKey(entry.client.clientId, entry.client.clientChannel)) ?? []),
377
403
  ].map((lease) => [lease.leaseId, lease]));
378
404
  allocation.leases = [...byLease.values()];
379
405
  }
@@ -407,17 +433,18 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
407
433
  };
408
434
  }, _DurabilitySemaphoreServer_sync = async function _DurabilitySemaphoreServer_sync(message) {
409
435
  const now = Date.now();
436
+ const client = clientOf(message);
410
437
  const result = await __classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").transaction(() => {
411
438
  const store = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_store, "f");
412
- const grantResults = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_applyReports).call(this, store, message.clientId, message.grants, now);
439
+ const grantResults = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_applyReports).call(this, store, client, message.grants, now);
413
440
  const leaseOutcome = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f") && message.leases
414
- ? applyLeaseReports(store, __classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f"), message.clientId, message.leases, now)
441
+ ? applyLeaseReports(store, __classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f"), client, message.leases, now)
415
442
  : undefined;
416
443
  return { grantResults, leaseEchoes: leaseOutcome?.echoes };
417
444
  });
418
445
  return {
419
446
  type: 'synced',
420
- v: 1,
447
+ ...acknowledgement(message),
421
448
  grantResults: result.grantResults,
422
449
  ...(result.leaseEchoes === undefined
423
450
  ? {}
@@ -426,11 +453,11 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
426
453
  ...(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_contended, "f") ? { contended: true } : {}),
427
454
  }),
428
455
  };
429
- }, _DurabilitySemaphoreServer_applyReports = function _DurabilitySemaphoreServer_applyReports(store, clientId, reports, now) {
456
+ }, _DurabilitySemaphoreServer_applyReports = function _DurabilitySemaphoreServer_applyReports(store, client, reports, now) {
430
457
  const results = [];
431
458
  for (const report of reports) {
432
459
  const grant = store.get(report.grantId);
433
- if (!grant || grant.client_id !== clientId) {
460
+ if (!grant || !ownedBy(grant, client)) {
434
461
  results.push({
435
462
  grantId: report.grantId,
436
463
  claimed: 0,
@@ -478,6 +505,7 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
478
505
  timestamp: now,
479
506
  grantId: grant.grant_id,
480
507
  clientId: grant.client_id,
508
+ clientChannel: grant.client_channel,
481
509
  group: grant.grp,
482
510
  permits: grant.permits - grant.claimed,
483
511
  });
@@ -0,0 +1,53 @@
1
+ import * as v from 'valibot';
2
+ export declare const grantRowSchema: v.ObjectSchema<{
3
+ readonly grant_id: v.StringSchema<undefined>;
4
+ readonly client_id: v.StringSchema<undefined>;
5
+ /** The client's queue channel; `clientId` alone does not identify a client. */
6
+ readonly client_channel: v.StringSchema<undefined>;
7
+ readonly grp: v.StringSchema<undefined>;
8
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
9
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
10
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
11
+ readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
12
+ /** Unclaimed permits are reclaimed once this passes. */
13
+ readonly expires_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
14
+ }, undefined>;
15
+ export type GrantRow = v.InferOutput<typeof grantRowSchema>;
16
+ /** Permits a client may draw locally without a round trip; held by the holder. */
17
+ export declare const leaseRowSchema: v.ObjectSchema<{
18
+ readonly lease_id: v.StringSchema<undefined>;
19
+ readonly client_id: v.StringSchema<undefined>;
20
+ readonly client_channel: v.StringSchema<undefined>;
21
+ readonly grp: v.StringSchema<undefined>;
22
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
23
+ /** Drawn permits the client has returned, as a high-water mark. */
24
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
25
+ readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
26
+ /** Refreshed by every report; the lease is swept once this passes. */
27
+ readonly expires_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
28
+ /** 1 once no more permits may be drawn; footprint stays until released. */
29
+ readonly retired: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>, v.MaxValueAction<number, 1, undefined>]>;
30
+ }, undefined>;
31
+ export type LeaseRow = v.InferOutput<typeof leaseRowSchema>;
32
+ /**
33
+ * Permits one semaphore channel has handed out. Every query is scoped to the
34
+ * channel, so semaphores sharing an object never see each other's grants or
35
+ * leases. Grants and leases are small and bounded by live clients, so
36
+ * aggregates are computed from `list()`.
37
+ */
38
+ export declare class HolderStore {
39
+ private readonly sql;
40
+ private readonly channel;
41
+ constructor(sql: SqlStorage, channel: string);
42
+ list(): GrantRow[];
43
+ get(grantId: string): GrantRow | undefined;
44
+ nextPendingAt(): number | undefined;
45
+ insert(row: GrantRow): boolean;
46
+ update(row: GrantRow): void;
47
+ remove(grantId: string): void;
48
+ listLeases(): LeaseRow[];
49
+ getLease(leaseId: string): LeaseRow | undefined;
50
+ insertLease(row: LeaseRow): boolean;
51
+ updateLease(row: LeaseRow): void;
52
+ removeLease(leaseId: string): void;
53
+ }