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
@@ -0,0 +1,107 @@
1
+ import * as v from 'valibot';
2
+ export declare const queueItemRowSchema: v.ObjectSchema<{
3
+ readonly id: v.StringSchema<undefined>;
4
+ readonly key: v.StringSchema<undefined>;
5
+ readonly grp: v.StringSchema<undefined>;
6
+ readonly payload: v.StringSchema<undefined>;
7
+ /** queued: waiting for permits; claiming: counted against a grant, awaiting confirmation; admitted: running. */
8
+ readonly status: v.PicklistSchema<["queued", "claiming", "admitted"], undefined>;
9
+ readonly grant_id: v.NullableSchema<v.StringSchema<undefined>, undefined>;
10
+ readonly lease_id: v.NullableSchema<v.StringSchema<undefined>, undefined>;
11
+ readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
12
+ readonly admitted_at: v.NullableSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>, undefined>;
13
+ /** 0 until `admit()` has returned for this item. */
14
+ readonly notified: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>, v.MaxValueAction<number, 1, undefined>]>;
15
+ readonly attempt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
16
+ readonly next_attempt_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
17
+ }, undefined>;
18
+ export type QueueItemRow = v.InferOutput<typeof queueItemRowSchema>;
19
+ export declare const queueGrantRowSchema: v.ObjectSchema<{
20
+ readonly grant_id: v.StringSchema<undefined>;
21
+ readonly key: v.StringSchema<undefined>;
22
+ readonly grp: v.StringSchema<undefined>;
23
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
24
+ /** Permits counted against this grant locally, as a high-water mark. */
25
+ readonly claimed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
26
+ /** Claims the holder has confirmed. */
27
+ readonly confirmed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
28
+ /** Items handed to `admit()` from this grant; never derived from live rows. */
29
+ readonly admitted: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
30
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
31
+ readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
32
+ }, undefined>;
33
+ export type QueueGrantRow = v.InferOutput<typeof queueGrantRowSchema>;
34
+ export declare const queueKeyRowSchema: v.ObjectSchema<{
35
+ readonly key: v.StringSchema<undefined>;
36
+ /** Earliest time the next permit request for this key may be sent. */
37
+ readonly next_request_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
38
+ readonly queued: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
39
+ }, undefined>;
40
+ export type QueueKeyRow = v.InferOutput<typeof queueKeyRowSchema>;
41
+ export declare const queueLeaseRowSchema: v.ObjectSchema<{
42
+ readonly lease_id: v.StringSchema<undefined>;
43
+ readonly key: v.StringSchema<undefined>;
44
+ readonly grp: v.StringSchema<undefined>;
45
+ readonly permits: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
46
+ /** Permits drawn so far; a drawn permit is never drawable again. */
47
+ readonly drawn: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
48
+ readonly released: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
49
+ readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
50
+ readonly expires_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
51
+ /** When to report the lease again while it has permits in use, so the holder keeps it. */
52
+ readonly renew_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
53
+ /** Half the lease's lifetime at the holder, from its last refresh. */
54
+ readonly renew_ms: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.GtValueAction<number, 0, undefined>]>;
55
+ /** 1 once the holder has settled the stopped lease; until then it is reported again. */
56
+ readonly confirmed: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>, v.MaxValueAction<number, 1, undefined>]>;
57
+ }, undefined>;
58
+ export type QueueLeaseRow = v.InferOutput<typeof queueLeaseRowSchema>;
59
+ /**
60
+ * One queue channel of a semaphore client: items, the grants it holds per
61
+ * key, and per-key request pacing. Every query is scoped to the channel, so
62
+ * queues sharing an object may reuse item IDs and keys. Due work is a key
63
+ * whose request time has passed with items queued, an admitted item whose
64
+ * `admit()` notification is outstanding, or a lease the holder must hear
65
+ * about again.
66
+ */
67
+ export declare class QueueStore {
68
+ private readonly sql;
69
+ private readonly channel;
70
+ constructor(sql: SqlStorage, channel: string);
71
+ getItem(id: string): QueueItemRow | undefined;
72
+ insertItem(row: QueueItemRow): boolean;
73
+ updateItem(row: QueueItemRow): void;
74
+ removeItem(id: string): void;
75
+ listItems(key: string, status: QueueItemRow['status'], limit: number): QueueItemRow[];
76
+ listItemsByGrant(grantId: string): QueueItemRow[];
77
+ /** Notified items whose `finished` check is due; `next_attempt_at` holds the check time. */
78
+ listCheckDue(now: number, limit: number): QueueItemRow[];
79
+ listUnnotified(now: number, limit: number): QueueItemRow[];
80
+ countItems(): Record<QueueItemRow['status'], number>;
81
+ getGrant(grantId: string): QueueGrantRow | undefined;
82
+ insertGrant(row: QueueGrantRow): boolean;
83
+ updateGrant(row: QueueGrantRow): void;
84
+ removeGrant(grantId: string): void;
85
+ listGrants(key: string): QueueGrantRow[];
86
+ getLease(leaseId: string): QueueLeaseRow | undefined;
87
+ insertLease(row: QueueLeaseRow): boolean;
88
+ updateLease(row: QueueLeaseRow): void;
89
+ /**
90
+ * Leases the holder must hear about again: those with permits in use, and
91
+ * stopped ones it has not settled. Both stay counted at the holder until
92
+ * reported, so a lost report must be repeated.
93
+ */
94
+ listRenewalsDue(now: number, limit: number): QueueLeaseRow[];
95
+ /** Drops settled leases that stopped drawing before `before`. */
96
+ removeSpentLeases(before: number): void;
97
+ listLeases(key: string): QueueLeaseRow[];
98
+ getKey(key: string): QueueKeyRow | undefined;
99
+ upsertKey(row: QueueKeyRow): void;
100
+ removeKey(key: string): void;
101
+ listDueKeys(now: number): QueueKeyRow[];
102
+ nextPendingAt({ unnotified, notified, }: {
103
+ unnotified: boolean;
104
+ notified: boolean;
105
+ }): number | undefined;
106
+ private items;
107
+ }
@@ -0,0 +1,258 @@
1
+ import * as v from 'valibot';
2
+ export const queueItemRowSchema = v.object({
3
+ id: v.string(),
4
+ key: v.string(),
5
+ grp: v.string(),
6
+ payload: v.string(),
7
+ /** queued: waiting for permits; claiming: counted against a grant, awaiting confirmation; admitted: running. */
8
+ status: v.picklist(['queued', 'claiming', 'admitted']),
9
+ grant_id: v.nullable(v.string()),
10
+ lease_id: v.nullable(v.string()),
11
+ created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
12
+ admitted_at: v.nullable(v.pipe(v.number(), v.safeInteger(), v.minValue(0))),
13
+ /** 0 until `admit()` has returned for this item. */
14
+ notified: v.pipe(v.number(), v.safeInteger(), v.minValue(0), v.maxValue(1)),
15
+ attempt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
16
+ next_attempt_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
17
+ });
18
+ export const queueGrantRowSchema = v.object({
19
+ grant_id: v.string(),
20
+ key: v.string(),
21
+ grp: v.string(),
22
+ permits: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
23
+ /** Permits counted against this grant locally, as a high-water mark. */
24
+ claimed: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
25
+ /** Claims the holder has confirmed. */
26
+ confirmed: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
27
+ /** Items handed to `admit()` from this grant; never derived from live rows. */
28
+ admitted: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
29
+ released: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
30
+ created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
31
+ });
32
+ export const queueKeyRowSchema = v.object({
33
+ key: v.string(),
34
+ /** Earliest time the next permit request for this key may be sent. */
35
+ next_request_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
36
+ queued: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
37
+ });
38
+ export const queueLeaseRowSchema = v.object({
39
+ lease_id: v.string(),
40
+ key: v.string(),
41
+ grp: v.string(),
42
+ permits: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
43
+ /** Permits drawn so far; a drawn permit is never drawable again. */
44
+ drawn: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
45
+ released: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
46
+ created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
47
+ expires_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
48
+ /** When to report the lease again while it has permits in use, so the holder keeps it. */
49
+ renew_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
50
+ /** Half the lease's lifetime at the holder, from its last refresh. */
51
+ renew_ms: v.pipe(v.number(), v.safeInteger(), v.gtValue(0)),
52
+ /** 1 once the holder has settled the stopped lease; until then it is reported again. */
53
+ confirmed: v.pipe(v.number(), v.safeInteger(), v.minValue(0), v.maxValue(1)),
54
+ });
55
+ const queueItemColumns = v.keyof(queueItemRowSchema).options;
56
+ const queueGrantColumns = v.keyof(queueGrantRowSchema).options;
57
+ const queueLeaseColumns = v.keyof(queueLeaseRowSchema).options;
58
+ const statusCountSchema = v.object({
59
+ status: queueItemRowSchema.entries.status,
60
+ count: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
61
+ });
62
+ /**
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.
69
+ */
70
+ export class QueueStore {
71
+ constructor(sql, channel) {
72
+ this.sql = sql;
73
+ this.channel = channel;
74
+ }
75
+ getItem(id) {
76
+ return this.items(`SELECT * FROM durability_semaphore_queue_items
77
+ WHERE channel = ? AND id = ? LIMIT 1`, this.channel, id)[0];
78
+ }
79
+ insertItem(row) {
80
+ return (this.sql
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]))
85
+ .toArray().length > 0);
86
+ }
87
+ updateItem(row) {
88
+ this.sql.exec(`UPDATE durability_semaphore_queue_items
89
+ SET status = ?, grant_id = ?, lease_id = ?, admitted_at = ?, notified = ?,
90
+ attempt = ?, next_attempt_at = ?
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);
92
+ }
93
+ removeItem(id) {
94
+ this.sql.exec('DELETE FROM durability_semaphore_queue_items WHERE channel = ? AND id = ?', this.channel, id);
95
+ }
96
+ listItems(key, status, limit) {
97
+ return this.items(`SELECT * FROM durability_semaphore_queue_items
98
+ WHERE channel = ? AND key = ? AND status = ?
99
+ ORDER BY created_at ASC, id ASC
100
+ LIMIT ?`, this.channel, key, status, limit);
101
+ }
102
+ listItemsByGrant(grantId) {
103
+ return this.items(`SELECT * FROM durability_semaphore_queue_items
104
+ WHERE channel = ? AND grant_id = ?
105
+ ORDER BY created_at ASC, id ASC`, this.channel, grantId);
106
+ }
107
+ /** Notified items whose `finished` check is due; `next_attempt_at` holds the check time. */
108
+ listCheckDue(now, limit) {
109
+ return this.items(`SELECT * FROM durability_semaphore_queue_items
110
+ WHERE channel = ? AND status = 'admitted' AND notified = 1
111
+ AND next_attempt_at <= ?
112
+ ORDER BY next_attempt_at ASC
113
+ LIMIT ?`, this.channel, now, limit);
114
+ }
115
+ listUnnotified(now, limit) {
116
+ return this.items(`SELECT * FROM durability_semaphore_queue_items
117
+ WHERE channel = ? AND status = 'admitted' AND notified = 0
118
+ AND next_attempt_at <= ?
119
+ ORDER BY next_attempt_at ASC
120
+ LIMIT ?`, this.channel, now, limit);
121
+ }
122
+ countItems() {
123
+ const counts = { queued: 0, claiming: 0, admitted: 0 };
124
+ for (const row of v.parse(v.array(statusCountSchema), this.sql
125
+ .exec(`SELECT status, COUNT(*) AS count FROM durability_semaphore_queue_items
126
+ WHERE channel = ? GROUP BY status`, this.channel)
127
+ .toArray())) {
128
+ counts[row.status] = row.count;
129
+ }
130
+ return counts;
131
+ }
132
+ getGrant(grantId) {
133
+ return v.parse(v.optional(queueGrantRowSchema), this.sql
134
+ .exec(`SELECT * FROM durability_semaphore_queue_grants
135
+ WHERE channel = ? AND grant_id = ? LIMIT 1`, this.channel, grantId)
136
+ .toArray()[0]);
137
+ }
138
+ insertGrant(row) {
139
+ return (this.sql
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]))
144
+ .toArray().length > 0);
145
+ }
146
+ updateGrant(row) {
147
+ this.sql.exec(`UPDATE durability_semaphore_queue_grants
148
+ SET permits = ?, claimed = ?, confirmed = ?, admitted = ?, released = ?
149
+ WHERE channel = ? AND grant_id = ?`, row.permits, row.claimed, row.confirmed, row.admitted, row.released, this.channel, row.grant_id);
150
+ }
151
+ removeGrant(grantId) {
152
+ this.sql.exec('DELETE FROM durability_semaphore_queue_grants WHERE channel = ? AND grant_id = ?', this.channel, grantId);
153
+ }
154
+ listGrants(key) {
155
+ return v.parse(v.array(queueGrantRowSchema), this.sql
156
+ .exec(`SELECT * FROM durability_semaphore_queue_grants
157
+ WHERE channel = ? AND key = ? ORDER BY created_at ASC, grant_id ASC`, this.channel, key)
158
+ .toArray());
159
+ }
160
+ getLease(leaseId) {
161
+ return v.parse(v.optional(queueLeaseRowSchema), this.sql
162
+ .exec(`SELECT * FROM durability_semaphore_queue_leases
163
+ WHERE channel = ? AND lease_id = ? LIMIT 1`, this.channel, leaseId)
164
+ .toArray()[0]);
165
+ }
166
+ insertLease(row) {
167
+ return (this.sql
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]))
172
+ .toArray().length > 0);
173
+ }
174
+ updateLease(row) {
175
+ this.sql.exec(`UPDATE durability_semaphore_queue_leases
176
+ SET permits = ?, drawn = ?, released = ?, expires_at = ?,
177
+ renew_at = ?, renew_ms = ?, confirmed = ?
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);
179
+ }
180
+ /**
181
+ * Leases the holder must hear about again: those with permits in use, and
182
+ * stopped ones it has not settled. Both stay counted at the holder until
183
+ * reported, so a lost report must be repeated.
184
+ */
185
+ listRenewalsDue(now, limit) {
186
+ return v.parse(v.array(queueLeaseRowSchema), this.sql
187
+ .exec(`SELECT * FROM durability_semaphore_queue_leases
188
+ WHERE channel = ? AND confirmed = 0 AND renew_at <= ?
189
+ AND (released < drawn OR expires_at <= ?)
190
+ ORDER BY renew_at ASC LIMIT ?`, this.channel, now, now, limit)
191
+ .toArray());
192
+ }
193
+ /** Drops settled leases that stopped drawing before `before`. */
194
+ removeSpentLeases(before) {
195
+ this.sql.exec(`DELETE FROM durability_semaphore_queue_leases
196
+ WHERE channel = ? AND confirmed = 1 AND expires_at < ?
197
+ AND released >= drawn`, this.channel, before);
198
+ }
199
+ listLeases(key) {
200
+ return v.parse(v.array(queueLeaseRowSchema), this.sql
201
+ .exec(`SELECT * FROM durability_semaphore_queue_leases
202
+ WHERE channel = ? AND key = ? ORDER BY created_at ASC, lease_id ASC`, this.channel, key)
203
+ .toArray());
204
+ }
205
+ getKey(key) {
206
+ return v.parse(v.optional(queueKeyRowSchema), this.sql
207
+ .exec(`SELECT * FROM durability_semaphore_queue_keys
208
+ WHERE channel = ? AND key = ? LIMIT 1`, this.channel, key)
209
+ .toArray()[0]);
210
+ }
211
+ upsertKey(row) {
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);
219
+ }
220
+ removeKey(key) {
221
+ this.sql.exec('DELETE FROM durability_semaphore_queue_keys WHERE channel = ? AND key = ?', this.channel, key);
222
+ }
223
+ listDueKeys(now) {
224
+ return v.parse(v.array(queueKeyRowSchema), this.sql
225
+ .exec(`SELECT * FROM durability_semaphore_queue_keys
226
+ WHERE channel = ? AND queued > 0 AND next_request_at <= ?
227
+ ORDER BY next_request_at ASC`, this.channel, now)
228
+ .toArray());
229
+ }
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;
254
+ }
255
+ items(query, ...bindings) {
256
+ return v.parse(v.array(queueItemRowSchema), this.sql.exec(query, ...bindings).toArray());
257
+ }
258
+ }
@@ -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()`. */