durability 5.1.0 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +145 -12
- package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +13 -5
- package/dist/{durability-alarms.js → alarms/index.js} +69 -55
- package/dist/alarms/migrations.d.ts +9 -0
- package/dist/alarms/migrations.js +133 -0
- package/dist/experimental.d.ts +10 -10
- package/dist/experimental.js +7 -7
- package/dist/{fanout.d.ts → fanout/index.d.ts} +13 -6
- package/dist/{fanout.js → fanout/index.js} +54 -46
- package/dist/fanout/migrations.d.ts +9 -0
- package/dist/fanout/migrations.js +181 -0
- package/dist/{stored-message.d.ts → fanout/stored-message.d.ts} +1 -1
- package/dist/index.d.ts +14 -9
- package/dist/index.js +13 -8
- package/dist/{log.d.ts → log/index.d.ts} +15 -3
- package/dist/{log.js → log/index.js} +152 -124
- package/dist/log/migrations.d.ts +18 -0
- package/dist/log/migrations.js +297 -0
- package/dist/{memoize.d.ts → memoize/index.d.ts} +1 -1
- package/dist/{memoize.js → memoize/index.js} +14 -11
- package/dist/memoize/migrations.d.ts +10 -0
- package/dist/memoize/migrations.js +21 -0
- package/dist/migrations.d.ts +0 -111
- package/dist/migrations.js +0 -612
- package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
- package/dist/{durability.js → operations/index.js} +59 -47
- package/dist/operations/migrations.d.ts +10 -0
- package/dist/operations/migrations.js +150 -0
- package/dist/{outbox.d.ts → outbox/index.d.ts} +4 -4
- package/dist/{outbox.js → outbox/index.js} +16 -60
- package/dist/outbox/migrations.d.ts +9 -0
- package/dist/outbox/migrations.js +43 -0
- package/dist/{routing-client.js → routing/client.js} +1 -1
- package/dist/{routing.d.ts → routing/index.d.ts} +3 -3
- package/dist/{routing.js → routing/index.js} +3 -3
- package/dist/{load.d.ts → routing/load.d.ts} +1 -1
- package/dist/{schedule.d.ts → schedule/index.d.ts} +16 -8
- package/dist/{schedule.js → schedule/index.js} +65 -49
- package/dist/schedule/migrations.d.ts +9 -0
- package/dist/schedule/migrations.js +118 -0
- package/dist/{scheduler.d.ts → scheduler/index.d.ts} +75 -25
- package/dist/{scheduler.js → scheduler/index.js} +300 -63
- package/dist/{semaphore-client.d.ts → semaphore/client.d.ts} +1 -1
- package/dist/{semaphore-client.js → semaphore/client.js} +2 -2
- package/dist/{semaphore.d.ts → semaphore/index.d.ts} +10 -10
- package/dist/{semaphore.js → semaphore/index.js} +7 -7
- package/dist/{semaphore-leases.d.ts → semaphore/leases.d.ts} +17 -6
- package/dist/{semaphore-leases.js → semaphore/leases.js} +28 -12
- package/dist/semaphore/migrations.d.ts +18 -0
- package/dist/semaphore/migrations.js +444 -0
- package/dist/{semaphore-protocol.d.ts → semaphore/protocol.d.ts} +16 -8
- package/dist/{semaphore-protocol.js → semaphore/protocol.js} +37 -14
- package/dist/{semaphore-queue-store.d.ts → semaphore/queue-store.d.ts} +12 -6
- package/dist/{semaphore-queue-store.js → semaphore/queue-store.js} +69 -61
- package/dist/{semaphore-queue.d.ts → semaphore/queue.d.ts} +15 -5
- package/dist/{semaphore-queue.js → semaphore/queue.js} +105 -46
- package/dist/{semaphore-server.d.ts → semaphore/server.d.ts} +12 -4
- package/dist/{semaphore-server.js → semaphore/server.js} +51 -24
- package/dist/{semaphore-store.d.ts → semaphore/store.d.ts} +8 -3
- package/dist/{semaphore-store.js → semaphore/store.js} +35 -33
- package/dist/{client.d.ts → socket/client.d.ts} +3 -3
- package/dist/{client.js → socket/client.js} +4 -4
- package/dist/{socket.d.ts → socket/index.d.ts} +15 -4
- package/dist/{socket.js → socket/index.js} +66 -20
- package/dist/socket/migrations.d.ts +9 -0
- package/dist/socket/migrations.js +57 -0
- package/dist/{socket-protocol.d.ts → socket/protocol.d.ts} +1 -1
- package/dist/storage.d.ts +4 -0
- package/dist/storage.js +13 -6
- package/dist/{topology-client.d.ts → topology/client.d.ts} +2 -2
- package/dist/{topology-client.js → topology/client.js} +3 -3
- package/dist/{topology.d.ts → topology/index.d.ts} +17 -7
- package/dist/{topology.js → topology/index.js} +92 -58
- package/dist/topology/migrations.d.ts +10 -0
- package/dist/topology/migrations.js +138 -0
- package/dist/{tracker.d.ts → tracker/index.d.ts} +3 -3
- package/dist/{tracker.js → tracker/index.js} +2 -2
- package/package.json +11 -11
- /package/dist/{stored-message.js → fanout/stored-message.js} +0 -0
- /package/dist/{routing-client.d.ts → routing/client.d.ts} +0 -0
- /package/dist/{load.js → routing/load.js} +0 -0
- /package/dist/{socket-protocol.js → socket/protocol.js} +0 -0
- /package/dist/{topology-snapshot.d.ts → topology/snapshot.d.ts} +0 -0
- /package/dist/{topology-snapshot.js → topology/snapshot.js} +0 -0
- /package/dist/{tracked-source.d.ts → tracker/source.d.ts} +0 -0
- /package/dist/{tracked-source.js → tracker/source.js} +0 -0
|
@@ -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.
|
|
5
|
-
*
|
|
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
|
|
46
|
+
export const permitRequestSchema = z
|
|
47
|
+
.object({
|
|
38
48
|
type: z.literal('request'),
|
|
39
|
-
v:
|
|
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
|
-
|
|
63
|
+
})
|
|
64
|
+
.refine(versionMatchesChannel, channelVersionError);
|
|
65
|
+
export const permitSyncSchema = z
|
|
66
|
+
.object({
|
|
54
67
|
type: z.literal('sync'),
|
|
55
|
-
v:
|
|
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
|
|
91
|
+
export const permitGrantSchema = z
|
|
92
|
+
.object({
|
|
77
93
|
type: z.literal('grant'),
|
|
78
|
-
v:
|
|
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
|
-
|
|
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:
|
|
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
|
-
*
|
|
55
|
-
* request pacing.
|
|
56
|
-
*
|
|
57
|
-
*
|
|
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
|
-
|
|
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(
|
|
96
|
+
nextPendingAt({ unnotified, notified, }: {
|
|
97
|
+
unnotified: boolean;
|
|
98
|
+
notified: boolean;
|
|
99
|
+
}): number | undefined;
|
|
94
100
|
private items;
|
|
95
101
|
}
|
|
@@ -60,118 +60,122 @@ const statusCountSchema = z.object({
|
|
|
60
60
|
count: z.number().int().nonnegative(),
|
|
61
61
|
});
|
|
62
62
|
/**
|
|
63
|
-
*
|
|
64
|
-
* request pacing.
|
|
65
|
-
*
|
|
66
|
-
*
|
|
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(
|
|
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
|
|
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
|
|
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
|
-
.
|
|
129
|
-
|
|
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
|
-
.
|
|
158
|
-
|
|
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,66 +185,70 @@ 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 < ?
|
|
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
|
-
.
|
|
203
|
-
|
|
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
|
|
209
|
-
|
|
210
|
-
|
|
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
|
|
211
216
|
next_request_at = excluded.next_request_at, queued = excluded.queued
|
|
212
217
|
WHERE next_request_at != excluded.next_request_at
|
|
213
|
-
OR queued != excluded.queued`, row.key, row.next_request_at, row.queued);
|
|
218
|
+
OR queued != excluded.queued`, this.channel, row.key, row.next_request_at, row.queued);
|
|
214
219
|
}
|
|
215
220
|
removeKey(key) {
|
|
216
|
-
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);
|
|
217
222
|
}
|
|
218
223
|
listDueKeys(now) {
|
|
219
224
|
return queueKeyRowSchema.array().parse(this.sql
|
|
220
225
|
.exec(`SELECT * FROM durability_semaphore_queue_keys
|
|
221
|
-
WHERE queued > 0 AND next_request_at <= ?
|
|
222
|
-
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)
|
|
223
228
|
.toArray());
|
|
224
229
|
}
|
|
225
|
-
nextPendingAt(
|
|
230
|
+
nextPendingAt({ unnotified, notified, }) {
|
|
226
231
|
// An idle lease is due when it stops at its expiry, not before.
|
|
227
232
|
const at = this.sql
|
|
228
233
|
.exec(`SELECT MIN(at) AS at FROM (
|
|
229
234
|
SELECT (
|
|
230
235
|
SELECT next_request_at FROM durability_semaphore_queue_keys
|
|
231
|
-
WHERE
|
|
236
|
+
WHERE channel = ? AND queued > 0
|
|
237
|
+
ORDER BY next_request_at ASC LIMIT 1
|
|
232
238
|
) AS at
|
|
233
239
|
UNION ALL
|
|
234
240
|
SELECT (
|
|
235
241
|
SELECT next_attempt_at FROM durability_semaphore_queue_items
|
|
236
|
-
WHERE
|
|
242
|
+
WHERE channel = ? AND status = 'admitted'
|
|
243
|
+
AND ((notified = 0 AND ?) OR (notified = 1 AND ?))
|
|
237
244
|
ORDER BY next_attempt_at ASC LIMIT 1
|
|
238
245
|
) AS at
|
|
239
246
|
UNION ALL
|
|
240
247
|
SELECT MIN(CASE WHEN released < drawn THEN renew_at
|
|
241
248
|
ELSE MAX(renew_at, expires_at) END) AS at
|
|
242
|
-
FROM durability_semaphore_queue_leases
|
|
243
|
-
|
|
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)
|
|
244
252
|
.toArray()[0]?.['at'];
|
|
245
253
|
return typeof at === 'number' ? at : undefined;
|
|
246
254
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { type DurabilityMigrationResult } from '
|
|
2
|
-
import { type DurabilityRetryOptions } from '
|
|
3
|
-
import { type SchedulerAttachment } from '
|
|
4
|
-
import type { PermitGrant, PermitRequest, PermitSync, PermitSyncResult, SemaphoreBlockedReason } from './
|
|
5
|
-
import { type QueueItemRow } from './
|
|
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()`. */
|