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
@@ -0,0 +1,444 @@
1
+ /**
2
+ * Ordered SQLite migrations for semaphore grants. Applied automatically on
3
+ * construction.
4
+ */
5
+ export const durabilitySemaphoreMigrations = [
6
+ {
7
+ name: 'durability_0007_create_semaphore',
8
+ up: `
9
+ CREATE TABLE IF NOT EXISTS durability_semaphore_grants (
10
+ grant_id TEXT PRIMARY KEY,
11
+ client_id TEXT NOT NULL,
12
+ grp TEXT NOT NULL,
13
+ permits INTEGER NOT NULL,
14
+ claimed INTEGER NOT NULL DEFAULT 0,
15
+ released INTEGER NOT NULL DEFAULT 0,
16
+ created_at INTEGER NOT NULL,
17
+ expires_at INTEGER NOT NULL
18
+ );
19
+ CREATE INDEX IF NOT EXISTS durability_semaphore_grants_expires_idx
20
+ ON durability_semaphore_grants (expires_at) WHERE claimed < permits;
21
+ `,
22
+ down: `
23
+ DROP INDEX IF EXISTS durability_semaphore_grants_expires_idx;
24
+ DROP TABLE IF EXISTS durability_semaphore_grants;
25
+ `,
26
+ },
27
+ {
28
+ name: 'durability_0009_create_semaphore_leases',
29
+ up: `
30
+ CREATE TABLE IF NOT EXISTS durability_semaphore_leases (
31
+ lease_id TEXT PRIMARY KEY,
32
+ client_id TEXT NOT NULL,
33
+ grp TEXT NOT NULL,
34
+ permits INTEGER NOT NULL,
35
+ released INTEGER NOT NULL DEFAULT 0,
36
+ created_at INTEGER NOT NULL,
37
+ expires_at INTEGER NOT NULL,
38
+ retired INTEGER NOT NULL DEFAULT 0
39
+ );
40
+ CREATE INDEX IF NOT EXISTS durability_semaphore_leases_expires_idx
41
+ ON durability_semaphore_leases (expires_at);
42
+ `,
43
+ down: `
44
+ DROP INDEX IF EXISTS durability_semaphore_leases_expires_idx;
45
+ DROP TABLE IF EXISTS durability_semaphore_leases;
46
+ `,
47
+ },
48
+ {
49
+ name: 'durability_0020_semaphore_channels',
50
+ up: `
51
+ ALTER TABLE durability_semaphore_grants RENAME TO durability_semaphore_grants_before_channels;
52
+ CREATE TABLE durability_semaphore_grants (
53
+ channel TEXT NOT NULL DEFAULT 'default',
54
+ grant_id TEXT NOT NULL,
55
+ client_id TEXT NOT NULL,
56
+ client_channel TEXT NOT NULL DEFAULT 'default',
57
+ grp TEXT NOT NULL,
58
+ permits INTEGER NOT NULL,
59
+ claimed INTEGER NOT NULL DEFAULT 0,
60
+ released INTEGER NOT NULL DEFAULT 0,
61
+ created_at INTEGER NOT NULL,
62
+ expires_at INTEGER NOT NULL,
63
+ PRIMARY KEY (channel, grant_id)
64
+ );
65
+ INSERT INTO durability_semaphore_grants
66
+ (channel, grant_id, client_id, client_channel, grp, permits, claimed,
67
+ released, created_at, expires_at)
68
+ SELECT 'default', grant_id, client_id, 'default', grp, permits, claimed,
69
+ released, created_at, expires_at
70
+ FROM durability_semaphore_grants_before_channels;
71
+ DROP TABLE durability_semaphore_grants_before_channels;
72
+ CREATE INDEX durability_semaphore_grants_expires_idx
73
+ ON durability_semaphore_grants (channel, expires_at) WHERE claimed < permits;
74
+
75
+ ALTER TABLE durability_semaphore_leases RENAME TO durability_semaphore_leases_before_channels;
76
+ CREATE TABLE durability_semaphore_leases (
77
+ channel TEXT NOT NULL DEFAULT 'default',
78
+ lease_id TEXT NOT NULL,
79
+ client_id TEXT NOT NULL,
80
+ client_channel TEXT NOT NULL DEFAULT 'default',
81
+ grp TEXT NOT NULL,
82
+ permits INTEGER NOT NULL,
83
+ released INTEGER NOT NULL DEFAULT 0,
84
+ created_at INTEGER NOT NULL,
85
+ expires_at INTEGER NOT NULL,
86
+ retired INTEGER NOT NULL DEFAULT 0,
87
+ PRIMARY KEY (channel, lease_id)
88
+ );
89
+ INSERT INTO durability_semaphore_leases
90
+ (channel, lease_id, client_id, client_channel, grp, permits, released,
91
+ created_at, expires_at, retired)
92
+ SELECT 'default', lease_id, client_id, 'default', grp, permits, released,
93
+ created_at, expires_at, retired
94
+ FROM durability_semaphore_leases_before_channels;
95
+ DROP TABLE durability_semaphore_leases_before_channels;
96
+ CREATE INDEX durability_semaphore_leases_expires_idx
97
+ ON durability_semaphore_leases (channel, expires_at);
98
+ `,
99
+ down: `
100
+ CREATE TABLE durability_semaphore_channel_guard (
101
+ channel TEXT CONSTRAINT cannot_downgrade_nondefault_channels CHECK (channel = 'default')
102
+ );
103
+ INSERT INTO durability_semaphore_channel_guard
104
+ SELECT channel FROM durability_semaphore_grants
105
+ UNION SELECT client_channel FROM durability_semaphore_grants
106
+ UNION SELECT channel FROM durability_semaphore_leases
107
+ UNION SELECT client_channel FROM durability_semaphore_leases;
108
+ DROP TABLE durability_semaphore_channel_guard;
109
+
110
+ ALTER TABLE durability_semaphore_grants RENAME TO durability_semaphore_grants_with_channels;
111
+ CREATE TABLE durability_semaphore_grants (
112
+ grant_id TEXT PRIMARY KEY,
113
+ client_id TEXT NOT NULL,
114
+ grp TEXT NOT NULL,
115
+ permits INTEGER NOT NULL,
116
+ claimed INTEGER NOT NULL DEFAULT 0,
117
+ released INTEGER NOT NULL DEFAULT 0,
118
+ created_at INTEGER NOT NULL,
119
+ expires_at INTEGER NOT NULL
120
+ );
121
+ INSERT INTO durability_semaphore_grants
122
+ SELECT grant_id, client_id, grp, permits, claimed, released, created_at,
123
+ expires_at
124
+ FROM durability_semaphore_grants_with_channels;
125
+ DROP TABLE durability_semaphore_grants_with_channels;
126
+ CREATE INDEX durability_semaphore_grants_expires_idx
127
+ ON durability_semaphore_grants (expires_at) WHERE claimed < permits;
128
+
129
+ ALTER TABLE durability_semaphore_leases RENAME TO durability_semaphore_leases_with_channels;
130
+ CREATE TABLE durability_semaphore_leases (
131
+ lease_id TEXT PRIMARY KEY,
132
+ client_id TEXT NOT NULL,
133
+ grp TEXT NOT NULL,
134
+ permits INTEGER NOT NULL,
135
+ released INTEGER NOT NULL DEFAULT 0,
136
+ created_at INTEGER NOT NULL,
137
+ expires_at INTEGER NOT NULL,
138
+ retired INTEGER NOT NULL DEFAULT 0
139
+ );
140
+ INSERT INTO durability_semaphore_leases
141
+ SELECT lease_id, client_id, grp, permits, released, created_at,
142
+ expires_at, retired
143
+ FROM durability_semaphore_leases_with_channels;
144
+ DROP TABLE durability_semaphore_leases_with_channels;
145
+ CREATE INDEX durability_semaphore_leases_expires_idx
146
+ ON durability_semaphore_leases (expires_at);
147
+ `,
148
+ },
149
+ ];
150
+ /**
151
+ * Ordered SQLite migrations for a semaphore client's queue. Applied
152
+ * automatically on construction.
153
+ */
154
+ export const durabilitySemaphoreQueueMigrations = [
155
+ {
156
+ name: 'durability_0008_create_semaphore_queue',
157
+ up: `
158
+ CREATE TABLE IF NOT EXISTS durability_semaphore_queue_items (
159
+ id TEXT PRIMARY KEY,
160
+ key TEXT NOT NULL,
161
+ grp TEXT NOT NULL,
162
+ payload TEXT NOT NULL,
163
+ status TEXT NOT NULL CHECK (status IN ('queued', 'claiming', 'admitted')),
164
+ grant_id TEXT,
165
+ created_at INTEGER NOT NULL,
166
+ admitted_at INTEGER,
167
+ notified INTEGER NOT NULL DEFAULT 0,
168
+ attempt INTEGER NOT NULL DEFAULT 0,
169
+ next_attempt_at INTEGER NOT NULL
170
+ );
171
+ CREATE INDEX IF NOT EXISTS durability_semaphore_queue_items_key_idx
172
+ ON durability_semaphore_queue_items (key, status, created_at, id);
173
+ CREATE INDEX IF NOT EXISTS durability_semaphore_queue_items_grant_idx
174
+ ON durability_semaphore_queue_items (grant_id, created_at, id);
175
+ CREATE INDEX IF NOT EXISTS durability_semaphore_queue_items_notify_idx
176
+ ON durability_semaphore_queue_items (next_attempt_at)
177
+ WHERE status = 'admitted' AND notified = 0;
178
+ CREATE TABLE IF NOT EXISTS durability_semaphore_queue_grants (
179
+ grant_id TEXT PRIMARY KEY,
180
+ key TEXT NOT NULL,
181
+ grp TEXT NOT NULL,
182
+ permits INTEGER NOT NULL,
183
+ claimed INTEGER NOT NULL DEFAULT 0,
184
+ confirmed INTEGER NOT NULL DEFAULT 0,
185
+ admitted INTEGER NOT NULL DEFAULT 0,
186
+ released INTEGER NOT NULL DEFAULT 0,
187
+ created_at INTEGER NOT NULL
188
+ );
189
+ CREATE INDEX IF NOT EXISTS durability_semaphore_queue_grants_key_idx
190
+ ON durability_semaphore_queue_grants (key, created_at);
191
+ CREATE TABLE IF NOT EXISTS durability_semaphore_queue_keys (
192
+ key TEXT PRIMARY KEY,
193
+ next_request_at INTEGER NOT NULL,
194
+ queued INTEGER NOT NULL
195
+ );
196
+ CREATE INDEX IF NOT EXISTS durability_semaphore_queue_keys_due_idx
197
+ ON durability_semaphore_queue_keys (next_request_at) WHERE queued > 0;
198
+ `,
199
+ down: `
200
+ DROP INDEX IF EXISTS durability_semaphore_queue_keys_due_idx;
201
+ DROP TABLE IF EXISTS durability_semaphore_queue_keys;
202
+ DROP INDEX IF EXISTS durability_semaphore_queue_grants_key_idx;
203
+ DROP TABLE IF EXISTS durability_semaphore_queue_grants;
204
+ DROP INDEX IF EXISTS durability_semaphore_queue_items_notify_idx;
205
+ DROP INDEX IF EXISTS durability_semaphore_queue_items_grant_idx;
206
+ DROP INDEX IF EXISTS durability_semaphore_queue_items_key_idx;
207
+ DROP TABLE IF EXISTS durability_semaphore_queue_items;
208
+ `,
209
+ },
210
+ {
211
+ name: 'durability_0010_add_semaphore_queue_leases',
212
+ up: `
213
+ ALTER TABLE durability_semaphore_queue_items ADD COLUMN lease_id TEXT;
214
+ CREATE TABLE IF NOT EXISTS durability_semaphore_queue_leases (
215
+ lease_id TEXT PRIMARY KEY,
216
+ key TEXT NOT NULL,
217
+ grp TEXT NOT NULL,
218
+ permits INTEGER NOT NULL,
219
+ drawn INTEGER NOT NULL DEFAULT 0,
220
+ released INTEGER NOT NULL DEFAULT 0,
221
+ created_at INTEGER NOT NULL,
222
+ expires_at INTEGER NOT NULL
223
+ );
224
+ CREATE INDEX IF NOT EXISTS durability_semaphore_queue_leases_key_idx
225
+ ON durability_semaphore_queue_leases (key, created_at);
226
+ `,
227
+ down: `
228
+ DROP INDEX IF EXISTS durability_semaphore_queue_leases_key_idx;
229
+ DROP TABLE IF EXISTS durability_semaphore_queue_leases;
230
+ ALTER TABLE durability_semaphore_queue_items DROP COLUMN lease_id;
231
+ `,
232
+ },
233
+ {
234
+ name: 'durability_0011_renew_semaphore_queue_leases',
235
+ up: `
236
+ ALTER TABLE durability_semaphore_queue_leases ADD COLUMN renew_at INTEGER NOT NULL DEFAULT 0;
237
+ ALTER TABLE durability_semaphore_queue_leases ADD COLUMN renew_ms INTEGER NOT NULL DEFAULT 30000;
238
+ ALTER TABLE durability_semaphore_queue_leases ADD COLUMN confirmed INTEGER NOT NULL DEFAULT 0;
239
+ `,
240
+ down: `
241
+ ALTER TABLE durability_semaphore_queue_leases DROP COLUMN confirmed;
242
+ ALTER TABLE durability_semaphore_queue_leases DROP COLUMN renew_ms;
243
+ ALTER TABLE durability_semaphore_queue_leases DROP COLUMN renew_at;
244
+ `,
245
+ },
246
+ {
247
+ name: 'durability_0021_semaphore_queue_channels',
248
+ up: `
249
+ ALTER TABLE durability_semaphore_queue_items RENAME TO durability_semaphore_queue_items_before_channels;
250
+ CREATE TABLE durability_semaphore_queue_items (
251
+ channel TEXT NOT NULL DEFAULT 'default',
252
+ id TEXT NOT NULL,
253
+ key TEXT NOT NULL,
254
+ grp TEXT NOT NULL,
255
+ payload TEXT NOT NULL,
256
+ status TEXT NOT NULL CHECK (status IN ('queued', 'claiming', 'admitted')),
257
+ grant_id TEXT,
258
+ lease_id TEXT,
259
+ created_at INTEGER NOT NULL,
260
+ admitted_at INTEGER,
261
+ notified INTEGER NOT NULL DEFAULT 0,
262
+ attempt INTEGER NOT NULL DEFAULT 0,
263
+ next_attempt_at INTEGER NOT NULL,
264
+ PRIMARY KEY (channel, id)
265
+ );
266
+ INSERT INTO durability_semaphore_queue_items
267
+ (channel, id, key, grp, payload, status, grant_id, lease_id, created_at,
268
+ admitted_at, notified, attempt, next_attempt_at)
269
+ SELECT 'default', id, key, grp, payload, status, grant_id, lease_id,
270
+ created_at, admitted_at, notified, attempt, next_attempt_at
271
+ FROM durability_semaphore_queue_items_before_channels;
272
+ DROP TABLE durability_semaphore_queue_items_before_channels;
273
+ CREATE INDEX durability_semaphore_queue_items_key_idx
274
+ ON durability_semaphore_queue_items (channel, key, status, created_at, id);
275
+ CREATE INDEX durability_semaphore_queue_items_grant_idx
276
+ ON durability_semaphore_queue_items (channel, grant_id, created_at, id);
277
+ CREATE INDEX durability_semaphore_queue_items_notify_idx
278
+ ON durability_semaphore_queue_items (channel, next_attempt_at)
279
+ WHERE status = 'admitted' AND notified = 0;
280
+
281
+ ALTER TABLE durability_semaphore_queue_grants RENAME TO durability_semaphore_queue_grants_before_channels;
282
+ CREATE TABLE durability_semaphore_queue_grants (
283
+ channel TEXT NOT NULL DEFAULT 'default',
284
+ grant_id TEXT NOT NULL,
285
+ key TEXT NOT NULL,
286
+ grp TEXT NOT NULL,
287
+ permits INTEGER NOT NULL,
288
+ claimed INTEGER NOT NULL DEFAULT 0,
289
+ confirmed INTEGER NOT NULL DEFAULT 0,
290
+ admitted INTEGER NOT NULL DEFAULT 0,
291
+ released INTEGER NOT NULL DEFAULT 0,
292
+ created_at INTEGER NOT NULL,
293
+ PRIMARY KEY (channel, grant_id)
294
+ );
295
+ INSERT INTO durability_semaphore_queue_grants
296
+ (channel, grant_id, key, grp, permits, claimed, confirmed, admitted,
297
+ released, created_at)
298
+ SELECT 'default', grant_id, key, grp, permits, claimed, confirmed,
299
+ admitted, released, created_at
300
+ FROM durability_semaphore_queue_grants_before_channels;
301
+ DROP TABLE durability_semaphore_queue_grants_before_channels;
302
+ CREATE INDEX durability_semaphore_queue_grants_key_idx
303
+ ON durability_semaphore_queue_grants (channel, key, created_at);
304
+
305
+ ALTER TABLE durability_semaphore_queue_keys RENAME TO durability_semaphore_queue_keys_before_channels;
306
+ CREATE TABLE durability_semaphore_queue_keys (
307
+ channel TEXT NOT NULL DEFAULT 'default',
308
+ key TEXT NOT NULL,
309
+ next_request_at INTEGER NOT NULL,
310
+ queued INTEGER NOT NULL,
311
+ PRIMARY KEY (channel, key)
312
+ );
313
+ INSERT INTO durability_semaphore_queue_keys
314
+ (channel, key, next_request_at, queued)
315
+ SELECT 'default', key, next_request_at, queued
316
+ FROM durability_semaphore_queue_keys_before_channels;
317
+ DROP TABLE durability_semaphore_queue_keys_before_channels;
318
+ CREATE INDEX durability_semaphore_queue_keys_due_idx
319
+ ON durability_semaphore_queue_keys (channel, next_request_at) WHERE queued > 0;
320
+
321
+ ALTER TABLE durability_semaphore_queue_leases RENAME TO durability_semaphore_queue_leases_before_channels;
322
+ CREATE TABLE durability_semaphore_queue_leases (
323
+ channel TEXT NOT NULL DEFAULT 'default',
324
+ lease_id TEXT NOT NULL,
325
+ key TEXT NOT NULL,
326
+ grp TEXT NOT NULL,
327
+ permits INTEGER NOT NULL,
328
+ drawn INTEGER NOT NULL DEFAULT 0,
329
+ released INTEGER NOT NULL DEFAULT 0,
330
+ created_at INTEGER NOT NULL,
331
+ expires_at INTEGER NOT NULL,
332
+ renew_at INTEGER NOT NULL DEFAULT 0,
333
+ renew_ms INTEGER NOT NULL DEFAULT 30000,
334
+ confirmed INTEGER NOT NULL DEFAULT 0,
335
+ PRIMARY KEY (channel, lease_id)
336
+ );
337
+ INSERT INTO durability_semaphore_queue_leases
338
+ (channel, lease_id, key, grp, permits, drawn, released, created_at,
339
+ expires_at, renew_at, renew_ms, confirmed)
340
+ SELECT 'default', lease_id, key, grp, permits, drawn, released,
341
+ created_at, expires_at, renew_at, renew_ms, confirmed
342
+ FROM durability_semaphore_queue_leases_before_channels;
343
+ DROP TABLE durability_semaphore_queue_leases_before_channels;
344
+ CREATE INDEX durability_semaphore_queue_leases_key_idx
345
+ ON durability_semaphore_queue_leases (channel, key, created_at);
346
+ `,
347
+ down: `
348
+ CREATE TABLE durability_semaphore_queue_channel_guard (
349
+ channel TEXT CONSTRAINT cannot_downgrade_nondefault_channels CHECK (channel = 'default')
350
+ );
351
+ INSERT INTO durability_semaphore_queue_channel_guard
352
+ SELECT channel FROM durability_semaphore_queue_items
353
+ UNION SELECT channel FROM durability_semaphore_queue_grants
354
+ UNION SELECT channel FROM durability_semaphore_queue_keys
355
+ UNION SELECT channel FROM durability_semaphore_queue_leases;
356
+ DROP TABLE durability_semaphore_queue_channel_guard;
357
+
358
+ ALTER TABLE durability_semaphore_queue_items RENAME TO durability_semaphore_queue_items_with_channels;
359
+ CREATE TABLE durability_semaphore_queue_items (
360
+ id TEXT PRIMARY KEY,
361
+ key TEXT NOT NULL,
362
+ grp TEXT NOT NULL,
363
+ payload TEXT NOT NULL,
364
+ status TEXT NOT NULL CHECK (status IN ('queued', 'claiming', 'admitted')),
365
+ grant_id TEXT,
366
+ created_at INTEGER NOT NULL,
367
+ admitted_at INTEGER,
368
+ notified INTEGER NOT NULL DEFAULT 0,
369
+ attempt INTEGER NOT NULL DEFAULT 0,
370
+ next_attempt_at INTEGER NOT NULL,
371
+ lease_id TEXT
372
+ );
373
+ INSERT INTO durability_semaphore_queue_items
374
+ (id, key, grp, payload, status, grant_id, created_at, admitted_at,
375
+ notified, attempt, next_attempt_at, lease_id)
376
+ SELECT id, key, grp, payload, status, grant_id, created_at, admitted_at,
377
+ notified, attempt, next_attempt_at, lease_id
378
+ FROM durability_semaphore_queue_items_with_channels;
379
+ DROP TABLE durability_semaphore_queue_items_with_channels;
380
+ CREATE INDEX durability_semaphore_queue_items_key_idx
381
+ ON durability_semaphore_queue_items (key, status, created_at, id);
382
+ CREATE INDEX durability_semaphore_queue_items_grant_idx
383
+ ON durability_semaphore_queue_items (grant_id, created_at, id);
384
+ CREATE INDEX durability_semaphore_queue_items_notify_idx
385
+ ON durability_semaphore_queue_items (next_attempt_at)
386
+ WHERE status = 'admitted' AND notified = 0;
387
+
388
+ ALTER TABLE durability_semaphore_queue_grants RENAME TO durability_semaphore_queue_grants_with_channels;
389
+ CREATE TABLE durability_semaphore_queue_grants (
390
+ grant_id TEXT PRIMARY KEY,
391
+ key TEXT NOT NULL,
392
+ grp TEXT NOT NULL,
393
+ permits INTEGER NOT NULL,
394
+ claimed INTEGER NOT NULL DEFAULT 0,
395
+ confirmed INTEGER NOT NULL DEFAULT 0,
396
+ admitted INTEGER NOT NULL DEFAULT 0,
397
+ released INTEGER NOT NULL DEFAULT 0,
398
+ created_at INTEGER NOT NULL
399
+ );
400
+ INSERT INTO durability_semaphore_queue_grants
401
+ SELECT grant_id, key, grp, permits, claimed, confirmed, admitted,
402
+ released, created_at
403
+ FROM durability_semaphore_queue_grants_with_channels;
404
+ DROP TABLE durability_semaphore_queue_grants_with_channels;
405
+ CREATE INDEX durability_semaphore_queue_grants_key_idx
406
+ ON durability_semaphore_queue_grants (key, created_at);
407
+
408
+ ALTER TABLE durability_semaphore_queue_keys RENAME TO durability_semaphore_queue_keys_with_channels;
409
+ CREATE TABLE durability_semaphore_queue_keys (
410
+ key TEXT PRIMARY KEY,
411
+ next_request_at INTEGER NOT NULL,
412
+ queued INTEGER NOT NULL
413
+ );
414
+ INSERT INTO durability_semaphore_queue_keys
415
+ SELECT key, next_request_at, queued
416
+ FROM durability_semaphore_queue_keys_with_channels;
417
+ DROP TABLE durability_semaphore_queue_keys_with_channels;
418
+ CREATE INDEX durability_semaphore_queue_keys_due_idx
419
+ ON durability_semaphore_queue_keys (next_request_at) WHERE queued > 0;
420
+
421
+ ALTER TABLE durability_semaphore_queue_leases RENAME TO durability_semaphore_queue_leases_with_channels;
422
+ CREATE TABLE durability_semaphore_queue_leases (
423
+ lease_id TEXT PRIMARY KEY,
424
+ key TEXT NOT NULL,
425
+ grp TEXT NOT NULL,
426
+ permits INTEGER NOT NULL,
427
+ drawn INTEGER NOT NULL DEFAULT 0,
428
+ released INTEGER NOT NULL DEFAULT 0,
429
+ created_at INTEGER NOT NULL,
430
+ expires_at INTEGER NOT NULL,
431
+ renew_at INTEGER NOT NULL DEFAULT 0,
432
+ renew_ms INTEGER NOT NULL DEFAULT 30000,
433
+ confirmed INTEGER NOT NULL DEFAULT 0
434
+ );
435
+ INSERT INTO durability_semaphore_queue_leases
436
+ SELECT lease_id, key, grp, permits, drawn, released, created_at,
437
+ expires_at, renew_at, renew_ms, confirmed
438
+ FROM durability_semaphore_queue_leases_with_channels;
439
+ DROP TABLE durability_semaphore_queue_leases_with_channels;
440
+ CREATE INDEX durability_semaphore_queue_leases_key_idx
441
+ ON durability_semaphore_queue_leases (key, created_at);
442
+ `,
443
+ },
444
+ ];
@@ -26,8 +26,9 @@ export declare const leaseGrantSchema: z.ZodObject<{
26
26
  export type LeaseGrant = z.infer<typeof leaseGrantSchema>;
27
27
  export declare const permitRequestSchema: z.ZodObject<{
28
28
  type: z.ZodLiteral<"request">;
29
- v: z.ZodLiteral<1>;
29
+ v: z.ZodUnion<readonly [z.ZodLiteral<1>, z.ZodLiteral<2>]>;
30
30
  clientId: z.ZodString;
31
+ clientChannel: z.ZodOptional<z.ZodString>;
31
32
  demand: z.ZodArray<z.ZodObject<{
32
33
  group: z.ZodString;
33
34
  count: z.ZodNumber;
@@ -48,8 +49,9 @@ export declare const permitRequestSchema: z.ZodObject<{
48
49
  export type PermitRequest = z.infer<typeof permitRequestSchema>;
49
50
  export declare const permitSyncSchema: z.ZodObject<{
50
51
  type: z.ZodLiteral<"sync">;
51
- v: z.ZodLiteral<1>;
52
+ v: z.ZodUnion<readonly [z.ZodLiteral<1>, z.ZodLiteral<2>]>;
52
53
  clientId: z.ZodString;
54
+ clientChannel: z.ZodOptional<z.ZodString>;
53
55
  grants: z.ZodArray<z.ZodObject<{
54
56
  grantId: z.ZodString;
55
57
  claimed: z.ZodNumber;
@@ -65,8 +67,9 @@ export declare const permitSyncSchema: z.ZodObject<{
65
67
  export type PermitSync = z.infer<typeof permitSyncSchema>;
66
68
  export declare const semaphoreRequestSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
67
69
  type: z.ZodLiteral<"request">;
68
- v: z.ZodLiteral<1>;
70
+ v: z.ZodUnion<readonly [z.ZodLiteral<1>, z.ZodLiteral<2>]>;
69
71
  clientId: z.ZodString;
72
+ clientChannel: z.ZodOptional<z.ZodString>;
70
73
  demand: z.ZodArray<z.ZodObject<{
71
74
  group: z.ZodString;
72
75
  count: z.ZodNumber;
@@ -85,8 +88,9 @@ export declare const semaphoreRequestSchema: z.ZodDiscriminatedUnion<[z.ZodObjec
85
88
  }, z.core.$strip>>>;
86
89
  }, z.core.$strip>, z.ZodObject<{
87
90
  type: z.ZodLiteral<"sync">;
88
- v: z.ZodLiteral<1>;
91
+ v: z.ZodUnion<readonly [z.ZodLiteral<1>, z.ZodLiteral<2>]>;
89
92
  clientId: z.ZodString;
93
+ clientChannel: z.ZodOptional<z.ZodString>;
90
94
  grants: z.ZodArray<z.ZodObject<{
91
95
  grantId: z.ZodString;
92
96
  claimed: z.ZodNumber;
@@ -119,7 +123,7 @@ export declare const grantResultSchema: z.ZodObject<{
119
123
  export type GrantResult = z.infer<typeof grantResultSchema>;
120
124
  export declare const permitGrantSchema: z.ZodObject<{
121
125
  type: z.ZodLiteral<"grant">;
122
- v: z.ZodLiteral<1>;
126
+ v: z.ZodUnion<readonly [z.ZodLiteral<1>, z.ZodLiteral<2>]>;
123
127
  grants: z.ZodArray<z.ZodObject<{
124
128
  grantId: z.ZodString;
125
129
  group: z.ZodString;
@@ -151,11 +155,12 @@ export declare const permitGrantSchema: z.ZodObject<{
151
155
  expiresAt: z.ZodNumber;
152
156
  }, z.core.$strip>>>;
153
157
  contended: z.ZodOptional<z.ZodBoolean>;
158
+ clientChannel: z.ZodOptional<z.ZodString>;
154
159
  }, z.core.$strip>;
155
160
  export type PermitGrant = z.infer<typeof permitGrantSchema>;
156
161
  export declare const permitSyncResultSchema: z.ZodObject<{
157
162
  type: z.ZodLiteral<"synced">;
158
- v: z.ZodLiteral<1>;
163
+ v: z.ZodUnion<readonly [z.ZodLiteral<1>, z.ZodLiteral<2>]>;
159
164
  grantResults: z.ZodArray<z.ZodObject<{
160
165
  grantId: z.ZodString;
161
166
  claimed: z.ZodNumber;
@@ -172,11 +177,12 @@ export declare const permitSyncResultSchema: z.ZodObject<{
172
177
  expiresAt: z.ZodNumber;
173
178
  }, z.core.$strip>>>;
174
179
  contended: z.ZodOptional<z.ZodBoolean>;
180
+ clientChannel: z.ZodOptional<z.ZodString>;
175
181
  }, z.core.$strip>;
176
182
  export type PermitSyncResult = z.infer<typeof permitSyncResultSchema>;
177
183
  export declare const semaphoreResponseSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
178
184
  type: z.ZodLiteral<"grant">;
179
- v: z.ZodLiteral<1>;
185
+ v: z.ZodUnion<readonly [z.ZodLiteral<1>, z.ZodLiteral<2>]>;
180
186
  grants: z.ZodArray<z.ZodObject<{
181
187
  grantId: z.ZodString;
182
188
  group: z.ZodString;
@@ -208,9 +214,10 @@ export declare const semaphoreResponseSchema: z.ZodDiscriminatedUnion<[z.ZodObje
208
214
  expiresAt: z.ZodNumber;
209
215
  }, z.core.$strip>>>;
210
216
  contended: z.ZodOptional<z.ZodBoolean>;
217
+ clientChannel: z.ZodOptional<z.ZodString>;
211
218
  }, z.core.$strip>, z.ZodObject<{
212
219
  type: z.ZodLiteral<"synced">;
213
- v: z.ZodLiteral<1>;
220
+ v: z.ZodUnion<readonly [z.ZodLiteral<1>, z.ZodLiteral<2>]>;
214
221
  grantResults: z.ZodArray<z.ZodObject<{
215
222
  grantId: z.ZodString;
216
223
  claimed: z.ZodNumber;
@@ -227,5 +234,6 @@ export declare const semaphoreResponseSchema: z.ZodDiscriminatedUnion<[z.ZodObje
227
234
  expiresAt: z.ZodNumber;
228
235
  }, z.core.$strip>>>;
229
236
  contended: z.ZodOptional<z.ZodBoolean>;
237
+ clientChannel: z.ZodOptional<z.ZodString>;
230
238
  }, z.core.$strip>], "type">;
231
239
  export type SemaphoreResponse = z.infer<typeof semaphoreResponseSchema>;
@@ -1,11 +1,20 @@
1
1
  import { z } from 'zod';
2
2
  /**
3
3
  * Messages between a semaphore client and the Durable Object holding the
4
- * limit. Both sides tolerate unknown fields so mixed deployments keep working;
5
- * new fields must be optional.
4
+ * limit. Default clients keep version 1. Named queues use version 2 so old
5
+ * holders reject them before allocating grants or leases under the wrong
6
+ * identity. Extra fields remain tolerated within each version.
6
7
  */
7
8
  const nonNegative = z.number().int().nonnegative();
8
9
  const nonEmpty = z.string().min(1);
10
+ const protocolVersion = z.union([z.literal(1), z.literal(2)]);
11
+ const versionMatchesChannel = (message) => message.v === (message.clientChannel === undefined ? 1 : 2);
12
+ const channelVersionError = 'Client channel does not match semaphore protocol version';
13
+ /**
14
+ * Which queue of the client object sent the message. Version 1 omits it;
15
+ * version 2 requires it. Older clients therefore keep their identity.
16
+ */
17
+ const clientChannel = nonEmpty.optional();
9
18
  /** Bounds one message's work so a client cannot monopolize the holder. */
10
19
  export const maxDemandEntries = 100;
11
20
  export const maxGrantReports = 200;
@@ -34,10 +43,12 @@ export const leaseGrantSchema = z.object({
34
43
  /** `expiresAt <= now` retires the lease: stop drawing immediately. */
35
44
  expiresAt: nonNegative,
36
45
  });
37
- export const permitRequestSchema = z.object({
46
+ export const permitRequestSchema = z
47
+ .object({
38
48
  type: z.literal('request'),
39
- v: z.literal(1),
49
+ v: protocolVersion,
40
50
  clientId: nonEmpty,
51
+ clientChannel,
41
52
  /** Sparse demand per group with the oldest queued time, for FIFO allocation. */
42
53
  demand: z
43
54
  .object({
@@ -49,14 +60,18 @@ export const permitRequestSchema = z.object({
49
60
  .max(maxDemandEntries),
50
61
  grants: grantReportSchema.array().max(maxGrantReports),
51
62
  leases: leaseReportSchema.array().max(maxGrantReports).optional(),
52
- });
53
- export const permitSyncSchema = z.object({
63
+ })
64
+ .refine(versionMatchesChannel, channelVersionError);
65
+ export const permitSyncSchema = z
66
+ .object({
54
67
  type: z.literal('sync'),
55
- v: z.literal(1),
68
+ v: protocolVersion,
56
69
  clientId: nonEmpty,
70
+ clientChannel,
57
71
  grants: grantReportSchema.array().max(maxGrantReports),
58
72
  leases: leaseReportSchema.array().max(maxGrantReports).optional(),
59
- });
73
+ })
74
+ .refine(versionMatchesChannel, channelVersionError);
60
75
  export const semaphoreRequestSchema = z.discriminatedUnion('type', [
61
76
  permitRequestSchema,
62
77
  permitSyncSchema,
@@ -73,9 +88,10 @@ export const grantResultSchema = z.object({
73
88
  claimed: nonNegative,
74
89
  status: z.enum(['ok', 'not_found', 'expired']),
75
90
  });
76
- export const permitGrantSchema = z.object({
91
+ export const permitGrantSchema = z
92
+ .object({
77
93
  type: z.literal('grant'),
78
- v: z.literal(1),
94
+ v: protocolVersion,
79
95
  grants: z
80
96
  .object({
81
97
  grantId: nonEmpty,
@@ -92,14 +108,21 @@ export const permitGrantSchema = z.object({
92
108
  leases: leaseGrantSchema.array().optional(),
93
109
  /** Demand went unserved: return undrawn lease permits in groups with nothing queued. */
94
110
  contended: z.boolean().optional(),
95
- });
96
- export const permitSyncResultSchema = z.object({
111
+ /** Echoes the request's client channel in version 2. */
112
+ clientChannel,
113
+ })
114
+ .refine(versionMatchesChannel, channelVersionError);
115
+ export const permitSyncResultSchema = z
116
+ .object({
97
117
  type: z.literal('synced'),
98
- v: z.literal(1),
118
+ v: protocolVersion,
99
119
  grantResults: grantResultSchema.array(),
100
120
  leases: leaseGrantSchema.array().optional(),
101
121
  contended: z.boolean().optional(),
102
- });
122
+ /** Echoes the sync's `clientChannel`, as for a grant. */
123
+ clientChannel,
124
+ })
125
+ .refine(versionMatchesChannel, channelVersionError);
103
126
  export const semaphoreResponseSchema = z.discriminatedUnion('type', [
104
127
  permitGrantSchema,
105
128
  permitSyncResultSchema,
@@ -51,14 +51,17 @@ export declare const queueLeaseRowSchema: z.ZodObject<{
51
51
  }, z.core.$strip>;
52
52
  export type QueueLeaseRow = z.infer<typeof queueLeaseRowSchema>;
53
53
  /**
54
- * A semaphore client's queue: items, the grants it holds per key, and per-key
55
- * request pacing. Due work is a key whose request time has passed with items
56
- * queued, an admitted item whose `admit()` notification is outstanding, or a
57
- * lease the holder must hear about again.
54
+ * One queue channel of a semaphore client: items, the grants it holds per
55
+ * key, and per-key request pacing. Every query is scoped to the channel, so
56
+ * queues sharing an object may reuse item IDs and keys. Due work is a key
57
+ * whose request time has passed with items queued, an admitted item whose
58
+ * `admit()` notification is outstanding, or a lease the holder must hear
59
+ * about again.
58
60
  */
59
61
  export declare class QueueStore {
60
62
  private readonly sql;
61
- constructor(sql: SqlStorage);
63
+ private readonly channel;
64
+ constructor(sql: SqlStorage, channel: string);
62
65
  getItem(id: string): QueueItemRow | undefined;
63
66
  insertItem(row: QueueItemRow): boolean;
64
67
  updateItem(row: QueueItemRow): void;
@@ -90,6 +93,9 @@ export declare class QueueStore {
90
93
  upsertKey(row: QueueKeyRow): void;
91
94
  removeKey(key: string): void;
92
95
  listDueKeys(now: number): QueueKeyRow[];
93
- nextPendingAt(checksFinished: boolean): number | undefined;
96
+ nextPendingAt({ unnotified, notified, }: {
97
+ unnotified: boolean;
98
+ notified: boolean;
99
+ }): number | undefined;
94
100
  private items;
95
101
  }