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.
- package/README.md +154 -21
- package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +13 -5
- package/dist/{durability-alarms.js → alarms/index.js} +85 -74
- 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} +91 -83
- 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} +27 -20
- package/dist/{log.js → log/index.js} +200 -166
- 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} +23 -21
- 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 +12 -628
- package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
- package/dist/{durability.js → operations/index.js} +76 -70
- 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} +35 -77
- package/dist/outbox/migrations.d.ts +9 -0
- package/dist/outbox/migrations.js +43 -0
- package/dist/policy.d.ts +19 -19
- package/dist/policy.js +13 -12
- 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} +97 -83
- 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} +308 -70
- package/dist/{semaphore-client.d.ts → semaphore/client.d.ts} +1 -1
- package/dist/{semaphore-client.js → semaphore/client.js} +8 -7
- 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 +451 -0
- package/dist/semaphore/protocol.js +114 -0
- package/dist/semaphore/queue-store.d.ts +107 -0
- package/dist/semaphore/queue-store.js +258 -0
- 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} +56 -28
- package/dist/semaphore/store.d.ts +53 -0
- package/dist/semaphore/store.js +107 -0
- package/dist/serialization.d.ts +5 -0
- package/dist/serialization.js +41 -16
- 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 +22 -15
- 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} +106 -72
- package/dist/topology/migrations.d.ts +10 -0
- package/dist/topology/migrations.js +138 -0
- package/dist/topology/snapshot.d.ts +50 -0
- package/dist/topology/snapshot.js +38 -0
- package/dist/{tracker.d.ts → tracker/index.d.ts} +3 -3
- package/dist/{tracker.js → tracker/index.js} +9 -9
- package/package.json +11 -11
- package/dist/semaphore-protocol.d.ts +0 -231
- package/dist/semaphore-protocol.js +0 -106
- package/dist/semaphore-queue-store.d.ts +0 -95
- package/dist/semaphore-queue-store.js +0 -252
- package/dist/semaphore-store.d.ts +0 -43
- package/dist/semaphore-store.js +0 -108
- package/dist/topology-snapshot.d.ts +0 -70
- package/dist/topology-snapshot.js +0 -48
- /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/{tracked-source.d.ts → tracker/source.d.ts} +0 -0
- /package/dist/{tracked-source.js → tracker/source.js} +0 -0
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
export const queueItemRowSchema = z.object({
|
|
3
|
-
id: z.string(),
|
|
4
|
-
key: z.string(),
|
|
5
|
-
grp: z.string(),
|
|
6
|
-
payload: z.string(),
|
|
7
|
-
/** queued: waiting for permits; claiming: counted against a grant, awaiting confirmation; admitted: running. */
|
|
8
|
-
status: z.enum(['queued', 'claiming', 'admitted']),
|
|
9
|
-
grant_id: z.string().nullable(),
|
|
10
|
-
lease_id: z.string().nullable(),
|
|
11
|
-
created_at: z.number().int().nonnegative(),
|
|
12
|
-
admitted_at: z.number().int().nonnegative().nullable(),
|
|
13
|
-
/** 0 until `admit()` has returned for this item. */
|
|
14
|
-
notified: z.number().int().min(0).max(1),
|
|
15
|
-
attempt: z.number().int().nonnegative(),
|
|
16
|
-
next_attempt_at: z.number().int().nonnegative(),
|
|
17
|
-
});
|
|
18
|
-
export const queueGrantRowSchema = z.object({
|
|
19
|
-
grant_id: z.string(),
|
|
20
|
-
key: z.string(),
|
|
21
|
-
grp: z.string(),
|
|
22
|
-
permits: z.number().int().nonnegative(),
|
|
23
|
-
/** Permits counted against this grant locally, as a high-water mark. */
|
|
24
|
-
claimed: z.number().int().nonnegative(),
|
|
25
|
-
/** Claims the holder has confirmed. */
|
|
26
|
-
confirmed: z.number().int().nonnegative(),
|
|
27
|
-
/** Items handed to `admit()` from this grant; never derived from live rows. */
|
|
28
|
-
admitted: z.number().int().nonnegative(),
|
|
29
|
-
released: z.number().int().nonnegative(),
|
|
30
|
-
created_at: z.number().int().nonnegative(),
|
|
31
|
-
});
|
|
32
|
-
export const queueKeyRowSchema = z.object({
|
|
33
|
-
key: z.string(),
|
|
34
|
-
/** Earliest time the next permit request for this key may be sent. */
|
|
35
|
-
next_request_at: z.number().int().nonnegative(),
|
|
36
|
-
queued: z.number().int().nonnegative(),
|
|
37
|
-
});
|
|
38
|
-
export const queueLeaseRowSchema = z.object({
|
|
39
|
-
lease_id: z.string(),
|
|
40
|
-
key: z.string(),
|
|
41
|
-
grp: z.string(),
|
|
42
|
-
permits: z.number().int().nonnegative(),
|
|
43
|
-
/** Permits drawn so far; a drawn permit is never drawable again. */
|
|
44
|
-
drawn: z.number().int().nonnegative(),
|
|
45
|
-
released: z.number().int().nonnegative(),
|
|
46
|
-
created_at: z.number().int().nonnegative(),
|
|
47
|
-
expires_at: z.number().int().nonnegative(),
|
|
48
|
-
/** When to report the lease again while it has permits in use, so the holder keeps it. */
|
|
49
|
-
renew_at: z.number().int().nonnegative(),
|
|
50
|
-
/** Half the lease's lifetime at the holder, from its last refresh. */
|
|
51
|
-
renew_ms: z.number().int().positive(),
|
|
52
|
-
/** 1 once the holder has settled the stopped lease; until then it is reported again. */
|
|
53
|
-
confirmed: z.number().int().min(0).max(1),
|
|
54
|
-
});
|
|
55
|
-
const queueItemColumns = queueItemRowSchema.keyof().options;
|
|
56
|
-
const queueGrantColumns = queueGrantRowSchema.keyof().options;
|
|
57
|
-
const queueLeaseColumns = queueLeaseRowSchema.keyof().options;
|
|
58
|
-
const statusCountSchema = z.object({
|
|
59
|
-
status: queueItemRowSchema.shape.status,
|
|
60
|
-
count: z.number().int().nonnegative(),
|
|
61
|
-
});
|
|
62
|
-
/**
|
|
63
|
-
* A semaphore client's queue: items, the grants it holds per key, and per-key
|
|
64
|
-
* request pacing. Due work is a key whose request time has passed with items
|
|
65
|
-
* queued, an admitted item whose `admit()` notification is outstanding, or a
|
|
66
|
-
* lease the holder must hear about again.
|
|
67
|
-
*/
|
|
68
|
-
export class QueueStore {
|
|
69
|
-
constructor(sql) {
|
|
70
|
-
this.sql = sql;
|
|
71
|
-
}
|
|
72
|
-
getItem(id) {
|
|
73
|
-
return this.items('SELECT * FROM durability_semaphore_queue_items WHERE id = ? LIMIT 1', id)[0];
|
|
74
|
-
}
|
|
75
|
-
insertItem(row) {
|
|
76
|
-
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
|
-
.toArray().length > 0);
|
|
82
|
-
}
|
|
83
|
-
updateItem(row) {
|
|
84
|
-
this.sql.exec(`UPDATE durability_semaphore_queue_items
|
|
85
|
-
SET status = ?, grant_id = ?, lease_id = ?, admitted_at = ?, notified = ?,
|
|
86
|
-
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);
|
|
88
|
-
}
|
|
89
|
-
removeItem(id) {
|
|
90
|
-
this.sql.exec('DELETE FROM durability_semaphore_queue_items WHERE id = ?', id);
|
|
91
|
-
}
|
|
92
|
-
listItems(key, status, limit) {
|
|
93
|
-
return this.items(`SELECT * FROM durability_semaphore_queue_items
|
|
94
|
-
WHERE key = ? AND status = ?
|
|
95
|
-
ORDER BY created_at ASC, id ASC
|
|
96
|
-
LIMIT ?`, key, status, limit);
|
|
97
|
-
}
|
|
98
|
-
listItemsByGrant(grantId) {
|
|
99
|
-
return this.items(`SELECT * FROM durability_semaphore_queue_items
|
|
100
|
-
WHERE grant_id = ?
|
|
101
|
-
ORDER BY created_at ASC, id ASC`, grantId);
|
|
102
|
-
}
|
|
103
|
-
/** Notified items whose `finished` check is due; `next_attempt_at` holds the check time. */
|
|
104
|
-
listCheckDue(now, limit) {
|
|
105
|
-
return this.items(`SELECT * FROM durability_semaphore_queue_items
|
|
106
|
-
WHERE status = 'admitted' AND notified = 1 AND next_attempt_at <= ?
|
|
107
|
-
ORDER BY next_attempt_at ASC
|
|
108
|
-
LIMIT ?`, now, limit);
|
|
109
|
-
}
|
|
110
|
-
listUnnotified(now, limit) {
|
|
111
|
-
return this.items(`SELECT * FROM durability_semaphore_queue_items
|
|
112
|
-
WHERE status = 'admitted' AND notified = 0 AND next_attempt_at <= ?
|
|
113
|
-
ORDER BY next_attempt_at ASC
|
|
114
|
-
LIMIT ?`, now, limit);
|
|
115
|
-
}
|
|
116
|
-
countItems() {
|
|
117
|
-
const counts = { queued: 0, claiming: 0, admitted: 0 };
|
|
118
|
-
for (const row of statusCountSchema.array().parse(this.sql
|
|
119
|
-
.exec(`SELECT status, COUNT(*) AS count FROM durability_semaphore_queue_items
|
|
120
|
-
GROUP BY status`)
|
|
121
|
-
.toArray())) {
|
|
122
|
-
counts[row.status] = row.count;
|
|
123
|
-
}
|
|
124
|
-
return counts;
|
|
125
|
-
}
|
|
126
|
-
getGrant(grantId) {
|
|
127
|
-
return queueGrantRowSchema
|
|
128
|
-
.optional()
|
|
129
|
-
.parse(this.sql
|
|
130
|
-
.exec('SELECT * FROM durability_semaphore_queue_grants WHERE grant_id = ? LIMIT 1', grantId)
|
|
131
|
-
.toArray()[0]);
|
|
132
|
-
}
|
|
133
|
-
insertGrant(row) {
|
|
134
|
-
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]))
|
|
139
|
-
.toArray().length > 0);
|
|
140
|
-
}
|
|
141
|
-
updateGrant(row) {
|
|
142
|
-
this.sql.exec(`UPDATE durability_semaphore_queue_grants
|
|
143
|
-
SET permits = ?, claimed = ?, confirmed = ?, admitted = ?, released = ?
|
|
144
|
-
WHERE grant_id = ?`, row.permits, row.claimed, row.confirmed, row.admitted, row.released, row.grant_id);
|
|
145
|
-
}
|
|
146
|
-
removeGrant(grantId) {
|
|
147
|
-
this.sql.exec('DELETE FROM durability_semaphore_queue_grants WHERE grant_id = ?', grantId);
|
|
148
|
-
}
|
|
149
|
-
listGrants(key) {
|
|
150
|
-
return queueGrantRowSchema.array().parse(this.sql
|
|
151
|
-
.exec(`SELECT * FROM durability_semaphore_queue_grants
|
|
152
|
-
WHERE key = ? ORDER BY created_at ASC, grant_id ASC`, key)
|
|
153
|
-
.toArray());
|
|
154
|
-
}
|
|
155
|
-
getLease(leaseId) {
|
|
156
|
-
return queueLeaseRowSchema
|
|
157
|
-
.optional()
|
|
158
|
-
.parse(this.sql
|
|
159
|
-
.exec('SELECT * FROM durability_semaphore_queue_leases WHERE lease_id = ? LIMIT 1', leaseId)
|
|
160
|
-
.toArray()[0]);
|
|
161
|
-
}
|
|
162
|
-
insertLease(row) {
|
|
163
|
-
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
|
-
.toArray().length > 0);
|
|
169
|
-
}
|
|
170
|
-
updateLease(row) {
|
|
171
|
-
this.sql.exec(`UPDATE durability_semaphore_queue_leases
|
|
172
|
-
SET permits = ?, drawn = ?, released = ?, expires_at = ?,
|
|
173
|
-
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);
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Leases the holder must hear about again: those with permits in use, and
|
|
178
|
-
* stopped ones it has not settled. Both stay counted at the holder until
|
|
179
|
-
* reported, so a lost report must be repeated.
|
|
180
|
-
*/
|
|
181
|
-
listRenewalsDue(now, limit) {
|
|
182
|
-
return queueLeaseRowSchema.array().parse(this.sql
|
|
183
|
-
.exec(`SELECT * FROM durability_semaphore_queue_leases
|
|
184
|
-
WHERE confirmed = 0 AND renew_at <= ?
|
|
185
|
-
AND (released < drawn OR expires_at <= ?)
|
|
186
|
-
ORDER BY renew_at ASC LIMIT ?`, now, now, limit)
|
|
187
|
-
.toArray());
|
|
188
|
-
}
|
|
189
|
-
/** Drops settled leases that stopped drawing before `before`. */
|
|
190
|
-
removeSpentLeases(before) {
|
|
191
|
-
this.sql.exec(`DELETE FROM durability_semaphore_queue_leases
|
|
192
|
-
WHERE confirmed = 1 AND expires_at < ? AND released >= drawn`, before);
|
|
193
|
-
}
|
|
194
|
-
listLeases(key) {
|
|
195
|
-
return queueLeaseRowSchema.array().parse(this.sql
|
|
196
|
-
.exec(`SELECT * FROM durability_semaphore_queue_leases
|
|
197
|
-
WHERE key = ? ORDER BY created_at ASC, lease_id ASC`, key)
|
|
198
|
-
.toArray());
|
|
199
|
-
}
|
|
200
|
-
getKey(key) {
|
|
201
|
-
return queueKeyRowSchema
|
|
202
|
-
.optional()
|
|
203
|
-
.parse(this.sql
|
|
204
|
-
.exec('SELECT * FROM durability_semaphore_queue_keys WHERE key = ? LIMIT 1', key)
|
|
205
|
-
.toArray()[0]);
|
|
206
|
-
}
|
|
207
|
-
upsertKey(row) {
|
|
208
|
-
this.sql.exec(`INSERT INTO durability_semaphore_queue_keys (key, next_request_at, queued)
|
|
209
|
-
VALUES (?, ?, ?)
|
|
210
|
-
ON CONFLICT(key) DO UPDATE SET
|
|
211
|
-
next_request_at = excluded.next_request_at, queued = excluded.queued
|
|
212
|
-
WHERE next_request_at != excluded.next_request_at
|
|
213
|
-
OR queued != excluded.queued`, row.key, row.next_request_at, row.queued);
|
|
214
|
-
}
|
|
215
|
-
removeKey(key) {
|
|
216
|
-
this.sql.exec('DELETE FROM durability_semaphore_queue_keys WHERE key = ?', key);
|
|
217
|
-
}
|
|
218
|
-
listDueKeys(now) {
|
|
219
|
-
return queueKeyRowSchema.array().parse(this.sql
|
|
220
|
-
.exec(`SELECT * FROM durability_semaphore_queue_keys
|
|
221
|
-
WHERE queued > 0 AND next_request_at <= ?
|
|
222
|
-
ORDER BY next_request_at ASC`, now)
|
|
223
|
-
.toArray());
|
|
224
|
-
}
|
|
225
|
-
nextPendingAt(checksFinished) {
|
|
226
|
-
// An idle lease is due when it stops at its expiry, not before.
|
|
227
|
-
const at = this.sql
|
|
228
|
-
.exec(`SELECT MIN(at) AS at FROM (
|
|
229
|
-
SELECT (
|
|
230
|
-
SELECT next_request_at FROM durability_semaphore_queue_keys
|
|
231
|
-
WHERE queued > 0 ORDER BY next_request_at ASC LIMIT 1
|
|
232
|
-
) AS at
|
|
233
|
-
UNION ALL
|
|
234
|
-
SELECT (
|
|
235
|
-
SELECT next_attempt_at FROM durability_semaphore_queue_items
|
|
236
|
-
WHERE status = 'admitted' AND (notified = 0 OR ?)
|
|
237
|
-
ORDER BY next_attempt_at ASC LIMIT 1
|
|
238
|
-
) AS at
|
|
239
|
-
UNION ALL
|
|
240
|
-
SELECT MIN(CASE WHEN released < drawn THEN renew_at
|
|
241
|
-
ELSE MAX(renew_at, expires_at) END) AS at
|
|
242
|
-
FROM durability_semaphore_queue_leases WHERE confirmed = 0
|
|
243
|
-
)`, checksFinished ? 1 : 0)
|
|
244
|
-
.toArray()[0]?.['at'];
|
|
245
|
-
return typeof at === 'number' ? at : undefined;
|
|
246
|
-
}
|
|
247
|
-
items(query, ...bindings) {
|
|
248
|
-
return queueItemRowSchema
|
|
249
|
-
.array()
|
|
250
|
-
.parse(this.sql.exec(query, ...bindings).toArray());
|
|
251
|
-
}
|
|
252
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
export declare const grantRowSchema: z.ZodObject<{
|
|
3
|
-
grant_id: z.ZodString;
|
|
4
|
-
client_id: z.ZodString;
|
|
5
|
-
grp: z.ZodString;
|
|
6
|
-
permits: z.ZodNumber;
|
|
7
|
-
claimed: z.ZodNumber;
|
|
8
|
-
released: z.ZodNumber;
|
|
9
|
-
created_at: z.ZodNumber;
|
|
10
|
-
expires_at: z.ZodNumber;
|
|
11
|
-
}, z.core.$strip>;
|
|
12
|
-
export type GrantRow = z.infer<typeof grantRowSchema>;
|
|
13
|
-
/** Permits a client may draw locally without a round trip; held by the holder. */
|
|
14
|
-
export declare const leaseRowSchema: z.ZodObject<{
|
|
15
|
-
lease_id: z.ZodString;
|
|
16
|
-
client_id: z.ZodString;
|
|
17
|
-
grp: z.ZodString;
|
|
18
|
-
permits: z.ZodNumber;
|
|
19
|
-
released: z.ZodNumber;
|
|
20
|
-
created_at: z.ZodNumber;
|
|
21
|
-
expires_at: z.ZodNumber;
|
|
22
|
-
retired: z.ZodNumber;
|
|
23
|
-
}, z.core.$strip>;
|
|
24
|
-
export type LeaseRow = z.infer<typeof leaseRowSchema>;
|
|
25
|
-
/**
|
|
26
|
-
* Permits a semaphore has handed out. Grants and leases are small and bounded
|
|
27
|
-
* by live clients, so aggregates are computed from `list()`.
|
|
28
|
-
*/
|
|
29
|
-
export declare class HolderStore {
|
|
30
|
-
private readonly sql;
|
|
31
|
-
constructor(sql: SqlStorage);
|
|
32
|
-
list(): GrantRow[];
|
|
33
|
-
get(grantId: string): GrantRow | undefined;
|
|
34
|
-
nextPendingAt(): number | undefined;
|
|
35
|
-
insert(row: GrantRow): boolean;
|
|
36
|
-
update(row: GrantRow): void;
|
|
37
|
-
remove(grantId: string): void;
|
|
38
|
-
listLeases(): LeaseRow[];
|
|
39
|
-
getLease(leaseId: string): LeaseRow | undefined;
|
|
40
|
-
insertLease(row: LeaseRow): boolean;
|
|
41
|
-
updateLease(row: LeaseRow): void;
|
|
42
|
-
removeLease(leaseId: string): void;
|
|
43
|
-
}
|
package/dist/semaphore-store.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
export const grantRowSchema = z.object({
|
|
3
|
-
grant_id: z.string(),
|
|
4
|
-
client_id: z.string(),
|
|
5
|
-
grp: z.string(),
|
|
6
|
-
permits: z.number().int().nonnegative(),
|
|
7
|
-
claimed: z.number().int().nonnegative(),
|
|
8
|
-
released: z.number().int().nonnegative(),
|
|
9
|
-
created_at: z.number().int().nonnegative(),
|
|
10
|
-
/** Unclaimed permits are reclaimed once this passes. */
|
|
11
|
-
expires_at: z.number().int().nonnegative(),
|
|
12
|
-
});
|
|
13
|
-
/** Permits a client may draw locally without a round trip; held by the holder. */
|
|
14
|
-
export const leaseRowSchema = z.object({
|
|
15
|
-
lease_id: z.string(),
|
|
16
|
-
client_id: z.string(),
|
|
17
|
-
grp: z.string(),
|
|
18
|
-
permits: z.number().int().nonnegative(),
|
|
19
|
-
/** Drawn permits the client has returned, as a high-water mark. */
|
|
20
|
-
released: z.number().int().nonnegative(),
|
|
21
|
-
created_at: z.number().int().nonnegative(),
|
|
22
|
-
/** Refreshed by every report; the lease is swept once this passes. */
|
|
23
|
-
expires_at: z.number().int().nonnegative(),
|
|
24
|
-
/** 1 once no more permits may be drawn; footprint stays until released. */
|
|
25
|
-
retired: z.number().int().min(0).max(1),
|
|
26
|
-
});
|
|
27
|
-
const grantColumns = grantRowSchema.keyof().options;
|
|
28
|
-
const leaseColumns = leaseRowSchema.keyof().options;
|
|
29
|
-
/**
|
|
30
|
-
* Permits a semaphore has handed out. Grants and leases are small and bounded
|
|
31
|
-
* by live clients, so aggregates are computed from `list()`.
|
|
32
|
-
*/
|
|
33
|
-
export class HolderStore {
|
|
34
|
-
constructor(sql) {
|
|
35
|
-
this.sql = sql;
|
|
36
|
-
}
|
|
37
|
-
list() {
|
|
38
|
-
return grantRowSchema
|
|
39
|
-
.array()
|
|
40
|
-
.parse(this.sql
|
|
41
|
-
.exec('SELECT * FROM durability_semaphore_grants ORDER BY created_at')
|
|
42
|
-
.toArray());
|
|
43
|
-
}
|
|
44
|
-
get(grantId) {
|
|
45
|
-
return grantRowSchema
|
|
46
|
-
.optional()
|
|
47
|
-
.parse(this.sql
|
|
48
|
-
.exec('SELECT * FROM durability_semaphore_grants WHERE grant_id = ? LIMIT 1', grantId)
|
|
49
|
-
.toArray()[0]);
|
|
50
|
-
}
|
|
51
|
-
nextPendingAt() {
|
|
52
|
-
return grantRowSchema
|
|
53
|
-
.pick({ expires_at: true })
|
|
54
|
-
.optional()
|
|
55
|
-
.parse(this.sql
|
|
56
|
-
.exec(`SELECT expires_at FROM durability_semaphore_grants
|
|
57
|
-
WHERE claimed < permits
|
|
58
|
-
ORDER BY expires_at ASC
|
|
59
|
-
LIMIT 1`)
|
|
60
|
-
.toArray()[0])?.expires_at;
|
|
61
|
-
}
|
|
62
|
-
insert(row) {
|
|
63
|
-
return (this.sql
|
|
64
|
-
.exec(`INSERT INTO durability_semaphore_grants (${grantColumns.join(', ')})
|
|
65
|
-
VALUES (${grantColumns.map(() => '?').join(', ')})
|
|
66
|
-
ON CONFLICT(grant_id) DO NOTHING
|
|
67
|
-
RETURNING grant_id`, ...grantColumns.map((column) => row[column]))
|
|
68
|
-
.toArray().length > 0);
|
|
69
|
-
}
|
|
70
|
-
update(row) {
|
|
71
|
-
this.sql.exec(`UPDATE durability_semaphore_grants
|
|
72
|
-
SET permits = ?, claimed = ?, released = ?, expires_at = ?
|
|
73
|
-
WHERE grant_id = ?`, row.permits, row.claimed, row.released, row.expires_at, row.grant_id);
|
|
74
|
-
}
|
|
75
|
-
remove(grantId) {
|
|
76
|
-
this.sql.exec('DELETE FROM durability_semaphore_grants WHERE grant_id = ?', grantId);
|
|
77
|
-
}
|
|
78
|
-
listLeases() {
|
|
79
|
-
return leaseRowSchema
|
|
80
|
-
.array()
|
|
81
|
-
.parse(this.sql
|
|
82
|
-
.exec('SELECT * FROM durability_semaphore_leases ORDER BY created_at')
|
|
83
|
-
.toArray());
|
|
84
|
-
}
|
|
85
|
-
getLease(leaseId) {
|
|
86
|
-
return leaseRowSchema
|
|
87
|
-
.optional()
|
|
88
|
-
.parse(this.sql
|
|
89
|
-
.exec('SELECT * FROM durability_semaphore_leases WHERE lease_id = ? LIMIT 1', leaseId)
|
|
90
|
-
.toArray()[0]);
|
|
91
|
-
}
|
|
92
|
-
insertLease(row) {
|
|
93
|
-
return (this.sql
|
|
94
|
-
.exec(`INSERT INTO durability_semaphore_leases (${leaseColumns.join(', ')})
|
|
95
|
-
VALUES (${leaseColumns.map(() => '?').join(', ')})
|
|
96
|
-
ON CONFLICT(lease_id) DO NOTHING
|
|
97
|
-
RETURNING lease_id`, ...leaseColumns.map((column) => row[column]))
|
|
98
|
-
.toArray().length > 0);
|
|
99
|
-
}
|
|
100
|
-
updateLease(row) {
|
|
101
|
-
this.sql.exec(`UPDATE durability_semaphore_leases
|
|
102
|
-
SET permits = ?, released = ?, expires_at = ?, retired = ?
|
|
103
|
-
WHERE lease_id = ?`, row.permits, row.released, row.expires_at, row.retired, row.lease_id);
|
|
104
|
-
}
|
|
105
|
-
removeLease(leaseId) {
|
|
106
|
-
this.sql.exec('DELETE FROM durability_semaphore_leases WHERE lease_id = ?', leaseId);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
/** `active` members are in the write and read sets; `draining` members only in the read set. */
|
|
3
|
-
export type TopologyMemberState = 'active' | 'draining';
|
|
4
|
-
type MetaField<Meta> = undefined extends Meta ? {
|
|
5
|
-
meta?: Meta;
|
|
6
|
-
} : {
|
|
7
|
-
meta: Meta;
|
|
8
|
-
};
|
|
9
|
-
/** A member as the application declares it. */
|
|
10
|
-
export type TopologyMemberInput<Meta = undefined> = {
|
|
11
|
-
/** The member's Durable Object name. */
|
|
12
|
-
id: string;
|
|
13
|
-
state: TopologyMemberState;
|
|
14
|
-
locationHint?: DurableObjectLocationHint;
|
|
15
|
-
} & MetaField<Meta>;
|
|
16
|
-
export type TopologyMember<Meta = undefined> = TopologyMemberInput<Meta> & {
|
|
17
|
-
joinedAt: number;
|
|
18
|
-
};
|
|
19
|
-
export type TopologySnapshot<Meta = undefined> = {
|
|
20
|
-
group: string;
|
|
21
|
-
/** Increases on every membership change, and keeps increasing across deletes. */
|
|
22
|
-
version: number;
|
|
23
|
-
/** ID of the coordinator Durable Object that published this snapshot. */
|
|
24
|
-
coordinator: string;
|
|
25
|
-
/** Sorted by ID. */
|
|
26
|
-
members: readonly TopologyMember<Meta>[];
|
|
27
|
-
};
|
|
28
|
-
export declare const memberInputSchema: z.ZodObject<{
|
|
29
|
-
id: z.ZodString;
|
|
30
|
-
state: z.ZodEnum<{
|
|
31
|
-
active: "active";
|
|
32
|
-
draining: "draining";
|
|
33
|
-
}>;
|
|
34
|
-
locationHint: z.ZodOptional<z.ZodEnum<{
|
|
35
|
-
wnam: "wnam";
|
|
36
|
-
enam: "enam";
|
|
37
|
-
sam: "sam";
|
|
38
|
-
weur: "weur";
|
|
39
|
-
eeur: "eeur";
|
|
40
|
-
apac: "apac";
|
|
41
|
-
oc: "oc";
|
|
42
|
-
afr: "afr";
|
|
43
|
-
me: "me";
|
|
44
|
-
}>>;
|
|
45
|
-
meta: z.ZodOptional<z.ZodJSONSchema>;
|
|
46
|
-
}, z.core.$strip>;
|
|
47
|
-
export declare const membersSchema: z.ZodArray<z.ZodObject<{
|
|
48
|
-
id: z.ZodString;
|
|
49
|
-
state: z.ZodEnum<{
|
|
50
|
-
active: "active";
|
|
51
|
-
draining: "draining";
|
|
52
|
-
}>;
|
|
53
|
-
locationHint: z.ZodOptional<z.ZodEnum<{
|
|
54
|
-
wnam: "wnam";
|
|
55
|
-
enam: "enam";
|
|
56
|
-
sam: "sam";
|
|
57
|
-
weur: "weur";
|
|
58
|
-
eeur: "eeur";
|
|
59
|
-
apac: "apac";
|
|
60
|
-
oc: "oc";
|
|
61
|
-
afr: "afr";
|
|
62
|
-
me: "me";
|
|
63
|
-
}>>;
|
|
64
|
-
meta: z.ZodOptional<z.ZodJSONSchema>;
|
|
65
|
-
joinedAt: z.ZodNumber;
|
|
66
|
-
}, z.core.$strip>>;
|
|
67
|
-
export declare const encodeSnapshot: <Meta>(snapshot: TopologySnapshot<Meta>) => string;
|
|
68
|
-
/** The stored value crossed a trust boundary, so every field is re-validated. */
|
|
69
|
-
export declare const decodeSnapshot: <Meta>(encoded: string) => TopologySnapshot<Meta>;
|
|
70
|
-
export {};
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
const locationHints = [
|
|
3
|
-
'wnam',
|
|
4
|
-
'enam',
|
|
5
|
-
'sam',
|
|
6
|
-
'weur',
|
|
7
|
-
'eeur',
|
|
8
|
-
'apac',
|
|
9
|
-
'oc',
|
|
10
|
-
'afr',
|
|
11
|
-
'me',
|
|
12
|
-
];
|
|
13
|
-
// Unknown keys are stripped, so current members (which carry `joinedAt`) can be returned as input.
|
|
14
|
-
export const memberInputSchema = z.object({
|
|
15
|
-
id: z.string().min(1),
|
|
16
|
-
state: z.enum(['active', 'draining']),
|
|
17
|
-
locationHint: z.enum(locationHints).optional(),
|
|
18
|
-
meta: z.json().optional(),
|
|
19
|
-
});
|
|
20
|
-
const memberSchema = memberInputSchema.extend({
|
|
21
|
-
joinedAt: z.number().int().nonnegative(),
|
|
22
|
-
});
|
|
23
|
-
export const membersSchema = memberSchema
|
|
24
|
-
.array()
|
|
25
|
-
.superRefine((members, ctx) => {
|
|
26
|
-
for (let index = 1; index < members.length; index++) {
|
|
27
|
-
if (members[index - 1].id >= members[index].id) {
|
|
28
|
-
ctx.addIssue({
|
|
29
|
-
code: 'custom',
|
|
30
|
-
message: 'Members must be sorted by ID and unique',
|
|
31
|
-
path: [index, 'id'],
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
const encodedSchema = z.object({
|
|
37
|
-
format: z.literal(1),
|
|
38
|
-
group: z.string().min(1),
|
|
39
|
-
version: z.number().int().positive(),
|
|
40
|
-
coordinator: z.string().min(1),
|
|
41
|
-
members: membersSchema.min(1),
|
|
42
|
-
});
|
|
43
|
-
export const encodeSnapshot = (snapshot) => JSON.stringify({ format: 1, ...snapshot });
|
|
44
|
-
/** The stored value crossed a trust boundary, so every field is re-validated. */
|
|
45
|
-
export const decodeSnapshot = (encoded) => {
|
|
46
|
-
const { format: _format, ...snapshot } = encodedSchema.parse(JSON.parse(encoded));
|
|
47
|
-
return snapshot;
|
|
48
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|