durability 5.0.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.
Files changed (89) hide show
  1. package/README.md +238 -7
  2. package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +23 -6
  3. package/dist/{durability-alarms.js → alarms/index.js} +96 -52
  4. package/dist/alarms/migrations.d.ts +9 -0
  5. package/dist/alarms/migrations.js +133 -0
  6. package/dist/errors.d.ts +15 -0
  7. package/dist/errors.js +24 -0
  8. package/dist/experimental.d.ts +11 -8
  9. package/dist/experimental.js +8 -5
  10. package/dist/{fanout.d.ts → fanout/index.d.ts} +13 -6
  11. package/dist/{fanout.js → fanout/index.js} +55 -49
  12. package/dist/fanout/migrations.d.ts +9 -0
  13. package/dist/fanout/migrations.js +181 -0
  14. package/dist/{stored-message.d.ts → fanout/stored-message.d.ts} +1 -1
  15. package/dist/index.d.ts +14 -9
  16. package/dist/index.js +13 -8
  17. package/dist/{log.d.ts → log/index.d.ts} +15 -3
  18. package/dist/{log.js → log/index.js} +152 -124
  19. package/dist/log/migrations.d.ts +18 -0
  20. package/dist/log/migrations.js +297 -0
  21. package/dist/{memoize.d.ts → memoize/index.d.ts} +1 -1
  22. package/dist/{memoize.js → memoize/index.js} +14 -11
  23. package/dist/memoize/migrations.d.ts +10 -0
  24. package/dist/memoize/migrations.js +21 -0
  25. package/dist/migrations.d.ts +1 -103
  26. package/dist/migrations.js +1 -570
  27. package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
  28. package/dist/{durability.js → operations/index.js} +63 -46
  29. package/dist/operations/migrations.d.ts +10 -0
  30. package/dist/operations/migrations.js +150 -0
  31. package/dist/{outbox.d.ts → outbox/index.d.ts} +4 -4
  32. package/dist/{outbox.js → outbox/index.js} +18 -63
  33. package/dist/outbox/migrations.d.ts +9 -0
  34. package/dist/outbox/migrations.js +43 -0
  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/index.d.ts +165 -0
  40. package/dist/schedule/index.js +667 -0
  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} +122 -22
  44. package/dist/scheduler/index.js +925 -0
  45. package/dist/{semaphore-client.d.ts → semaphore/client.d.ts} +1 -1
  46. package/dist/{semaphore-client.js → semaphore/client.js} +2 -2
  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 → semaphore/protocol.d.ts} +16 -8
  54. package/dist/{semaphore-protocol.js → semaphore/protocol.js} +37 -14
  55. package/dist/{semaphore-queue-store.d.ts → semaphore/queue-store.d.ts} +12 -6
  56. package/dist/{semaphore-queue-store.js → semaphore/queue-store.js} +87 -75
  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} +51 -24
  61. package/dist/{semaphore-store.d.ts → semaphore/store.d.ts} +8 -3
  62. package/dist/{semaphore-store.js → semaphore/store.js} +35 -33
  63. package/dist/{client.d.ts → socket/client.d.ts} +3 -3
  64. package/dist/{client.js → socket/client.js} +4 -4
  65. package/dist/{socket.d.ts → socket/index.d.ts} +15 -4
  66. package/dist/{socket.js → socket/index.js} +66 -20
  67. package/dist/socket/migrations.d.ts +9 -0
  68. package/dist/socket/migrations.js +57 -0
  69. package/dist/{socket-protocol.d.ts → socket/protocol.d.ts} +1 -1
  70. package/dist/storage.d.ts +9 -2
  71. package/dist/storage.js +24 -11
  72. package/dist/{topology-client.d.ts → topology/client.d.ts} +2 -2
  73. package/dist/{topology-client.js → topology/client.js} +3 -3
  74. package/dist/{topology.d.ts → topology/index.d.ts} +17 -7
  75. package/dist/{topology.js → topology/index.js} +92 -58
  76. package/dist/topology/migrations.d.ts +10 -0
  77. package/dist/topology/migrations.js +138 -0
  78. package/dist/{tracker.d.ts → tracker/index.d.ts} +3 -3
  79. package/dist/{tracker.js → tracker/index.js} +2 -2
  80. package/package.json +11 -11
  81. package/dist/scheduler.js +0 -577
  82. /package/dist/{stored-message.js → fanout/stored-message.js} +0 -0
  83. /package/dist/{routing-client.d.ts → routing/client.d.ts} +0 -0
  84. /package/dist/{load.js → routing/load.js} +0 -0
  85. /package/dist/{socket-protocol.js → socket/protocol.js} +0 -0
  86. /package/dist/{topology-snapshot.d.ts → topology/snapshot.d.ts} +0 -0
  87. /package/dist/{topology-snapshot.js → topology/snapshot.js} +0 -0
  88. /package/dist/{tracked-source.d.ts → tracker/source.d.ts} +0 -0
  89. /package/dist/{tracked-source.js → tracker/source.js} +0 -0
@@ -60,118 +60,122 @@ const statusCountSchema = z.object({
60
60
  count: z.number().int().nonnegative(),
61
61
  });
62
62
  /**
63
- * A semaphore client's queue: items, the grants it holds per key, and per-key
64
- * request pacing. Due work is a key whose request time has passed with items
65
- * queued, an admitted item whose `admit()` notification is outstanding, or a
66
- * lease the holder must hear about again.
63
+ * One queue channel of a semaphore client: items, the grants it holds per
64
+ * key, and per-key request pacing. Every query is scoped to the channel, so
65
+ * queues sharing an object may reuse item IDs and keys. Due work is a key
66
+ * whose request time has passed with items queued, an admitted item whose
67
+ * `admit()` notification is outstanding, or a lease the holder must hear
68
+ * about again.
67
69
  */
68
70
  export class QueueStore {
69
- constructor(sql) {
71
+ constructor(sql, channel) {
70
72
  this.sql = sql;
73
+ this.channel = channel;
71
74
  }
72
75
  getItem(id) {
73
- return this.items('SELECT * FROM durability_semaphore_queue_items WHERE id = ? LIMIT 1', id)[0];
76
+ return this.items(`SELECT * FROM durability_semaphore_queue_items
77
+ WHERE channel = ? AND id = ? LIMIT 1`, this.channel, id)[0];
74
78
  }
75
79
  insertItem(row) {
76
80
  return (this.sql
77
- .exec(`INSERT INTO durability_semaphore_queue_items (${queueItemColumns.join(', ')})
78
- VALUES (${queueItemColumns.map(() => '?').join(', ')})
79
- ON CONFLICT(id) DO NOTHING
80
- RETURNING id`, ...queueItemColumns.map((column) => row[column]))
81
+ .exec(`INSERT INTO durability_semaphore_queue_items (channel, ${queueItemColumns.join(', ')})
82
+ VALUES (?, ${queueItemColumns.map(() => '?').join(', ')})
83
+ ON CONFLICT(channel, id) DO NOTHING
84
+ RETURNING id`, this.channel, ...queueItemColumns.map((column) => row[column]))
81
85
  .toArray().length > 0);
82
86
  }
83
87
  updateItem(row) {
84
88
  this.sql.exec(`UPDATE durability_semaphore_queue_items
85
89
  SET status = ?, grant_id = ?, lease_id = ?, admitted_at = ?, notified = ?,
86
90
  attempt = ?, next_attempt_at = ?
87
- WHERE id = ?`, row.status, row.grant_id, row.lease_id, row.admitted_at, row.notified, row.attempt, row.next_attempt_at, row.id);
91
+ WHERE channel = ? AND id = ?`, row.status, row.grant_id, row.lease_id, row.admitted_at, row.notified, row.attempt, row.next_attempt_at, this.channel, row.id);
88
92
  }
89
93
  removeItem(id) {
90
- this.sql.exec('DELETE FROM durability_semaphore_queue_items WHERE id = ?', id);
94
+ this.sql.exec('DELETE FROM durability_semaphore_queue_items WHERE channel = ? AND id = ?', this.channel, id);
91
95
  }
92
96
  listItems(key, status, limit) {
93
97
  return this.items(`SELECT * FROM durability_semaphore_queue_items
94
- WHERE key = ? AND status = ?
98
+ WHERE channel = ? AND key = ? AND status = ?
95
99
  ORDER BY created_at ASC, id ASC
96
- LIMIT ?`, key, status, limit);
100
+ LIMIT ?`, this.channel, key, status, limit);
97
101
  }
98
102
  listItemsByGrant(grantId) {
99
103
  return this.items(`SELECT * FROM durability_semaphore_queue_items
100
- WHERE grant_id = ?
101
- ORDER BY created_at ASC, id ASC`, grantId);
104
+ WHERE channel = ? AND grant_id = ?
105
+ ORDER BY created_at ASC, id ASC`, this.channel, grantId);
102
106
  }
103
107
  /** Notified items whose `finished` check is due; `next_attempt_at` holds the check time. */
104
108
  listCheckDue(now, limit) {
105
109
  return this.items(`SELECT * FROM durability_semaphore_queue_items
106
- WHERE status = 'admitted' AND notified = 1 AND next_attempt_at <= ?
110
+ WHERE channel = ? AND status = 'admitted' AND notified = 1
111
+ AND next_attempt_at <= ?
107
112
  ORDER BY next_attempt_at ASC
108
- LIMIT ?`, now, limit);
113
+ LIMIT ?`, this.channel, now, limit);
109
114
  }
110
115
  listUnnotified(now, limit) {
111
116
  return this.items(`SELECT * FROM durability_semaphore_queue_items
112
- WHERE status = 'admitted' AND notified = 0 AND next_attempt_at <= ?
117
+ WHERE channel = ? AND status = 'admitted' AND notified = 0
118
+ AND next_attempt_at <= ?
113
119
  ORDER BY next_attempt_at ASC
114
- LIMIT ?`, now, limit);
120
+ LIMIT ?`, this.channel, now, limit);
115
121
  }
116
122
  countItems() {
117
123
  const counts = { queued: 0, claiming: 0, admitted: 0 };
118
124
  for (const row of statusCountSchema.array().parse(this.sql
119
125
  .exec(`SELECT status, COUNT(*) AS count FROM durability_semaphore_queue_items
120
- GROUP BY status`)
126
+ WHERE channel = ? GROUP BY status`, this.channel)
121
127
  .toArray())) {
122
128
  counts[row.status] = row.count;
123
129
  }
124
130
  return counts;
125
131
  }
126
132
  getGrant(grantId) {
127
- return queueGrantRowSchema
128
- .optional()
129
- .parse(this.sql
130
- .exec('SELECT * FROM durability_semaphore_queue_grants WHERE grant_id = ? LIMIT 1', grantId)
133
+ return queueGrantRowSchema.optional().parse(this.sql
134
+ .exec(`SELECT * FROM durability_semaphore_queue_grants
135
+ WHERE channel = ? AND grant_id = ? LIMIT 1`, this.channel, grantId)
131
136
  .toArray()[0]);
132
137
  }
133
138
  insertGrant(row) {
134
139
  return (this.sql
135
- .exec(`INSERT INTO durability_semaphore_queue_grants (${queueGrantColumns.join(', ')})
136
- VALUES (${queueGrantColumns.map(() => '?').join(', ')})
137
- ON CONFLICT(grant_id) DO NOTHING
138
- RETURNING grant_id`, ...queueGrantColumns.map((column) => row[column]))
140
+ .exec(`INSERT INTO durability_semaphore_queue_grants (channel, ${queueGrantColumns.join(', ')})
141
+ VALUES (?, ${queueGrantColumns.map(() => '?').join(', ')})
142
+ ON CONFLICT(channel, grant_id) DO NOTHING
143
+ RETURNING grant_id`, this.channel, ...queueGrantColumns.map((column) => row[column]))
139
144
  .toArray().length > 0);
140
145
  }
141
146
  updateGrant(row) {
142
147
  this.sql.exec(`UPDATE durability_semaphore_queue_grants
143
148
  SET permits = ?, claimed = ?, confirmed = ?, admitted = ?, released = ?
144
- WHERE grant_id = ?`, row.permits, row.claimed, row.confirmed, row.admitted, row.released, row.grant_id);
149
+ WHERE channel = ? AND grant_id = ?`, row.permits, row.claimed, row.confirmed, row.admitted, row.released, this.channel, row.grant_id);
145
150
  }
146
151
  removeGrant(grantId) {
147
- this.sql.exec('DELETE FROM durability_semaphore_queue_grants WHERE grant_id = ?', grantId);
152
+ this.sql.exec('DELETE FROM durability_semaphore_queue_grants WHERE channel = ? AND grant_id = ?', this.channel, grantId);
148
153
  }
149
154
  listGrants(key) {
150
155
  return queueGrantRowSchema.array().parse(this.sql
151
156
  .exec(`SELECT * FROM durability_semaphore_queue_grants
152
- WHERE key = ? ORDER BY created_at ASC, grant_id ASC`, key)
157
+ WHERE channel = ? AND key = ? ORDER BY created_at ASC, grant_id ASC`, this.channel, key)
153
158
  .toArray());
154
159
  }
155
160
  getLease(leaseId) {
156
- return queueLeaseRowSchema
157
- .optional()
158
- .parse(this.sql
159
- .exec('SELECT * FROM durability_semaphore_queue_leases WHERE lease_id = ? LIMIT 1', leaseId)
161
+ return queueLeaseRowSchema.optional().parse(this.sql
162
+ .exec(`SELECT * FROM durability_semaphore_queue_leases
163
+ WHERE channel = ? AND lease_id = ? LIMIT 1`, this.channel, leaseId)
160
164
  .toArray()[0]);
161
165
  }
162
166
  insertLease(row) {
163
167
  return (this.sql
164
- .exec(`INSERT INTO durability_semaphore_queue_leases (${queueLeaseColumns.join(', ')})
165
- VALUES (${queueLeaseColumns.map(() => '?').join(', ')})
166
- ON CONFLICT(lease_id) DO NOTHING
167
- RETURNING lease_id`, ...queueLeaseColumns.map((column) => row[column]))
168
+ .exec(`INSERT INTO durability_semaphore_queue_leases (channel, ${queueLeaseColumns.join(', ')})
169
+ VALUES (?, ${queueLeaseColumns.map(() => '?').join(', ')})
170
+ ON CONFLICT(channel, lease_id) DO NOTHING
171
+ RETURNING lease_id`, this.channel, ...queueLeaseColumns.map((column) => row[column]))
168
172
  .toArray().length > 0);
169
173
  }
170
174
  updateLease(row) {
171
175
  this.sql.exec(`UPDATE durability_semaphore_queue_leases
172
176
  SET permits = ?, drawn = ?, released = ?, expires_at = ?,
173
177
  renew_at = ?, renew_ms = ?, confirmed = ?
174
- WHERE lease_id = ?`, row.permits, row.drawn, row.released, row.expires_at, row.renew_at, row.renew_ms, row.confirmed, row.lease_id);
178
+ WHERE channel = ? AND lease_id = ?`, row.permits, row.drawn, row.released, row.expires_at, row.renew_at, row.renew_ms, row.confirmed, this.channel, row.lease_id);
175
179
  }
176
180
  /**
177
181
  * Leases the holder must hear about again: those with permits in use, and
@@ -181,64 +185,72 @@ export class QueueStore {
181
185
  listRenewalsDue(now, limit) {
182
186
  return queueLeaseRowSchema.array().parse(this.sql
183
187
  .exec(`SELECT * FROM durability_semaphore_queue_leases
184
- WHERE confirmed = 0 AND renew_at <= ?
188
+ WHERE channel = ? AND confirmed = 0 AND renew_at <= ?
185
189
  AND (released < drawn OR expires_at <= ?)
186
- ORDER BY renew_at ASC LIMIT ?`, now, now, limit)
190
+ ORDER BY renew_at ASC LIMIT ?`, this.channel, now, now, limit)
187
191
  .toArray());
188
192
  }
189
193
  /** Drops settled leases that stopped drawing before `before`. */
190
194
  removeSpentLeases(before) {
191
195
  this.sql.exec(`DELETE FROM durability_semaphore_queue_leases
192
- WHERE confirmed = 1 AND expires_at < ? AND released >= drawn`, before);
196
+ WHERE channel = ? AND confirmed = 1 AND expires_at < ?
197
+ AND released >= drawn`, this.channel, before);
193
198
  }
194
199
  listLeases(key) {
195
200
  return queueLeaseRowSchema.array().parse(this.sql
196
201
  .exec(`SELECT * FROM durability_semaphore_queue_leases
197
- WHERE key = ? ORDER BY created_at ASC, lease_id ASC`, key)
202
+ WHERE channel = ? AND key = ? ORDER BY created_at ASC, lease_id ASC`, this.channel, key)
198
203
  .toArray());
199
204
  }
200
205
  getKey(key) {
201
- return queueKeyRowSchema
202
- .optional()
203
- .parse(this.sql
204
- .exec('SELECT * FROM durability_semaphore_queue_keys WHERE key = ? LIMIT 1', key)
206
+ return queueKeyRowSchema.optional().parse(this.sql
207
+ .exec(`SELECT * FROM durability_semaphore_queue_keys
208
+ WHERE channel = ? AND key = ? LIMIT 1`, this.channel, key)
205
209
  .toArray()[0]);
206
210
  }
207
211
  upsertKey(row) {
208
- this.sql.exec(`INSERT INTO durability_semaphore_queue_keys (key, next_request_at, queued)
209
- VALUES (?, ?, ?)
210
- ON CONFLICT(key) DO UPDATE SET
211
- next_request_at = excluded.next_request_at, queued = excluded.queued`, row.key, row.next_request_at, row.queued);
212
+ this.sql.exec(`INSERT INTO durability_semaphore_queue_keys
213
+ (channel, key, next_request_at, queued)
214
+ VALUES (?, ?, ?, ?)
215
+ ON CONFLICT(channel, key) DO UPDATE SET
216
+ next_request_at = excluded.next_request_at, queued = excluded.queued
217
+ WHERE next_request_at != excluded.next_request_at
218
+ OR queued != excluded.queued`, this.channel, row.key, row.next_request_at, row.queued);
212
219
  }
213
220
  removeKey(key) {
214
- this.sql.exec('DELETE FROM durability_semaphore_queue_keys WHERE key = ?', key);
221
+ this.sql.exec('DELETE FROM durability_semaphore_queue_keys WHERE channel = ? AND key = ?', this.channel, key);
215
222
  }
216
223
  listDueKeys(now) {
217
224
  return queueKeyRowSchema.array().parse(this.sql
218
225
  .exec(`SELECT * FROM durability_semaphore_queue_keys
219
- WHERE queued > 0 AND next_request_at <= ?
220
- ORDER BY next_request_at ASC`, now)
226
+ WHERE channel = ? AND queued > 0 AND next_request_at <= ?
227
+ ORDER BY next_request_at ASC`, this.channel, now)
221
228
  .toArray());
222
229
  }
223
- nextPendingAt(checksFinished) {
224
- const candidates = [
225
- this.sql
226
- .exec(`SELECT next_request_at AS at FROM durability_semaphore_queue_keys
227
- WHERE queued > 0 ORDER BY next_request_at ASC LIMIT 1`)
228
- .toArray()[0]?.['at'],
229
- this.sql
230
- .exec(`SELECT next_attempt_at AS at FROM durability_semaphore_queue_items
231
- WHERE status = 'admitted' AND (notified = 0 OR ?)
232
- ORDER BY next_attempt_at ASC LIMIT 1`, checksFinished ? 1 : 0)
233
- .toArray()[0]?.['at'],
234
- // An idle lease is due when it stops at its expiry, not before.
235
- this.sql
236
- .exec(`SELECT MIN(CASE WHEN released < drawn THEN renew_at
237
- ELSE MAX(renew_at, expires_at) END) AS at
238
- FROM durability_semaphore_queue_leases WHERE confirmed = 0`)
239
- .toArray()[0]?.['at'],
240
- ].filter((value) => typeof value === 'number');
241
- return candidates.length === 0 ? undefined : Math.min(...candidates);
230
+ nextPendingAt({ unnotified, notified, }) {
231
+ // An idle lease is due when it stops at its expiry, not before.
232
+ const at = this.sql
233
+ .exec(`SELECT MIN(at) AS at FROM (
234
+ SELECT (
235
+ SELECT next_request_at FROM durability_semaphore_queue_keys
236
+ WHERE channel = ? AND queued > 0
237
+ ORDER BY next_request_at ASC LIMIT 1
238
+ ) AS at
239
+ UNION ALL
240
+ SELECT (
241
+ SELECT next_attempt_at FROM durability_semaphore_queue_items
242
+ WHERE channel = ? AND status = 'admitted'
243
+ AND ((notified = 0 AND ?) OR (notified = 1 AND ?))
244
+ ORDER BY next_attempt_at ASC LIMIT 1
245
+ ) AS at
246
+ UNION ALL
247
+ SELECT MIN(CASE WHEN released < drawn THEN renew_at
248
+ ELSE MAX(renew_at, expires_at) END) AS at
249
+ FROM durability_semaphore_queue_leases
250
+ WHERE channel = ? AND confirmed = 0
251
+ )`, this.channel, this.channel, unnotified ? 1 : 0, notified ? 1 : 0, this.channel)
252
+ .toArray()[0]?.['at'];
253
+ return typeof at === 'number' ? at : undefined;
242
254
  }
243
255
  items(query, ...bindings) {
244
256
  return queueItemRowSchema
@@ -1,8 +1,8 @@
1
- import { type DurabilityMigrationResult } from './migrations.js';
2
- import { type DurabilityRetryOptions } from './policy.js';
3
- import { type SchedulerAttachment } from './scheduler.js';
4
- import type { PermitGrant, PermitRequest, PermitSync, PermitSyncResult, SemaphoreBlockedReason } from './semaphore-protocol.js';
5
- import { type QueueItemRow } from './semaphore-queue-store.js';
1
+ import { type DurabilityMigrationResult } from '../migrations.js';
2
+ import { type DurabilityRetryOptions } from '../policy.js';
3
+ import { type SchedulerAttachment } from '../scheduler/index.js';
4
+ import type { PermitGrant, PermitRequest, PermitSync, PermitSyncResult, SemaphoreBlockedReason } from './protocol.js';
5
+ import { type QueueItemRow } from './queue-store.js';
6
6
  export type SemaphoreInput<Body> = {
7
7
  /** Stable item ID; enqueueing a known ID again is a no-op. Defaults to random. */
8
8
  id?: string;
@@ -40,6 +40,16 @@ export type SemaphoreHolder = {
40
40
  sync(key: string, message: PermitSync): Promise<PermitSyncResult>;
41
41
  };
42
42
  export type DurabilitySemaphoreQueueConfig<Body> = SchedulerAttachment & {
43
+ /**
44
+ * Stable queue ID. Queues on one object share storage, partitioned by
45
+ * channel, and may reuse item IDs and keys. The holder counts a named
46
+ * queue's permits under `clientId` and this channel, so queues sharing a
47
+ * `clientId` never see each other's grants or leases; a named queue admits
48
+ * nothing through a holder too old to acknowledge its channel. Which holder
49
+ * pool a key reaches is decided by `server`, not by this channel. Defaults
50
+ * to `default`.
51
+ */
52
+ channel?: string;
43
53
  /** The holder, from `DurabilitySemaphore.client()`; caller transforms apply. */
44
54
  server: SemaphoreHolder;
45
55
  /** Stable identity of this client Durable Object, e.g. `ctx.id.toString()`. */
@@ -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
  /**