durability 5.2.0 → 5.3.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 +28 -13
- package/dist/alarms/index.js +18 -21
- package/dist/fanout/index.js +40 -53
- package/dist/log/index.d.ts +12 -17
- package/dist/log/index.js +59 -53
- package/dist/memoize/index.js +9 -10
- package/dist/migrations.js +48 -52
- package/dist/operations/index.js +22 -28
- package/dist/outbox/index.js +20 -31
- package/dist/policy.d.ts +19 -19
- package/dist/policy.js +13 -12
- package/dist/schedule/index.d.ts +13 -1
- package/dist/schedule/index.js +73 -60
- package/dist/scheduler/index.d.ts +19 -1
- package/dist/scheduler/index.js +53 -54
- package/dist/semaphore/client.js +6 -5
- package/dist/semaphore/protocol.d.ts +446 -234
- package/dist/semaphore/protocol.js +46 -61
- package/dist/semaphore/queue-store.d.ts +58 -52
- package/dist/semaphore/queue-store.js +55 -57
- package/dist/semaphore/queue.js +2 -15
- package/dist/semaphore/server.js +24 -14
- package/dist/semaphore/store.d.ts +30 -25
- package/dist/semaphore/store.js +31 -34
- package/dist/serialization.d.ts +5 -0
- package/dist/serialization.js +41 -16
- package/dist/shared-timer.d.ts +27 -0
- package/dist/shared-timer.js +191 -0
- package/dist/socket/client.js +25 -22
- package/dist/storage.js +9 -9
- package/dist/topology/client.js +29 -15
- package/dist/topology/index.js +18 -32
- package/dist/topology/snapshot.d.ts +20 -40
- package/dist/topology/snapshot.js +18 -28
- package/dist/tracker/index.js +15 -12
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -164,7 +164,7 @@ export class Subscription extends DurableObject<Env> {
|
|
|
164
164
|
}
|
|
165
165
|
```
|
|
166
166
|
|
|
167
|
-
Scheduling a name again replaces its pending occurrence. If that name is already running, the current handler continues and the replacement runs afterward. Different names may run concurrently, but one name never has more than one
|
|
167
|
+
Scheduling a name again replaces its pending occurrence. If that name is already running, the current handler continues and the replacement runs afterward. Different names may run concurrently, but one name never has more than one attempt in progress in the same instance. A handler that ignores the abort of a timed-out attempt may still be running beside the next one.
|
|
168
168
|
|
|
169
169
|
Named alarms inside the scheduler's `alarmMinDelayMs` window start from an in-memory timer at their requested time. The option accepts 1 to 15 seconds and defaults to 15 seconds; the scheduler never sets the physical alarm sooner than that delay; the physical alarm remains as a durable fallback for restarts. If concurrency capacity is full when the timer fires, execution waits in the shared FIFO queue. The scheduling method resolves after persistence and never waits for the handler to finish.
|
|
170
170
|
|
|
@@ -380,7 +380,7 @@ const durability = new Durability({
|
|
|
380
380
|
});
|
|
381
381
|
```
|
|
382
382
|
|
|
383
|
-
Each attempt receives its own `AbortSignal`. A timed-out operation follows the normal retry policy; a timed-out named alarm is terminal unless `retryTimeouts` is set.
|
|
383
|
+
Each attempt receives its own `AbortSignal`. A timed-out operation follows the normal retry policy; a timed-out named alarm is terminal unless `retryTimeouts` is set. A timeout ends the attempt: its concurrency permit is returned and the next attempt starts on schedule, whether or not the handler has stopped. Nothing can stop a running handler, so one that ignores abort keeps running beside the retry; its result is discarded, and once the retry has started its `memoize` calls reject. Stop work when the signal aborts, and make side effects safe to repeat.
|
|
384
384
|
|
|
385
385
|
Throw `NonRetryableError` to move work directly to `failed` without another attempt. Errors named `NonRetryableError` from other packages, such as `cloudflare:workflows`, are recognized too.
|
|
386
386
|
|
|
@@ -437,6 +437,20 @@ const removedAlarms = await this.alarms.purgeBefore(retentionCutoff);
|
|
|
437
437
|
|
|
438
438
|
The purge runs in one transaction. It best-effort aborts matching active handlers, but cannot force an abort-ignoring handler to stop. Operation IDs can be reused immediately; a late old generation cannot overwrite the replacement record.
|
|
439
439
|
|
|
440
|
+
## In-memory timeouts
|
|
441
|
+
|
|
442
|
+
The runtime makes the global `setTimeout` throw `QuotaExceededError` once 10,000 timers are pending in one Durable Object. The helpers keep their own timeouts in a min-heap behind one native timer, and `scheduler.setTimeout` puts yours in the same heap, so the number pending does not count against that limit:
|
|
443
|
+
|
|
444
|
+
```ts
|
|
445
|
+
const cancel = this.scheduler.setTimeout(() => {
|
|
446
|
+
this.sockets.get(id)?.close(4000, 'Idle');
|
|
447
|
+
}, 30_000);
|
|
448
|
+
|
|
449
|
+
cancel();
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
It returns a cancel function instead of an id. Callbacks due at the same time run one after another in one task, and one that throws is reported as `durability.timer.callback_failed` instead of becoming an uncaught exception. The timeout exists only in memory, so an eviction drops it; use a named alarm for work that must still run after one.
|
|
453
|
+
|
|
440
454
|
## Experimental options
|
|
441
455
|
|
|
442
456
|
These options, methods, and capabilities are opt-in: none takes effect unless you set, call, or construct it, so leaving them alone keeps every behavior described above. Their names and behavior may change in a minor release. Scheduler options can also be passed to a helper configured with `context`.
|
|
@@ -522,9 +536,10 @@ export class Cron extends DurableObject<Env> {
|
|
|
522
536
|
}
|
|
523
537
|
```
|
|
524
538
|
|
|
525
|
-
- **Versions.** `set({ expression, version })` stores the schedule and its next tick only when `version` is higher than the stored one, and resolves `true` when it did. A lower version is ignored. The same version changes nothing but reconciles the physical alarm, so resending it repairs a lost alarm. The expression is opaque; only `nextOccurrence(expression, after)` reads it, and it must return a safe integer after `after`, or `undefined` to stop ticking. `get()` returns `{ expression, version, jitterMs, nextAt }`, where `nextAt` is the next tick's nominal time, or `undefined` when no schedule is live.
|
|
539
|
+
- **Versions.** `set({ expression, version })` stores the schedule and its next tick only when `version` is higher than the stored one, and resolves `true` when it did. A lower version is ignored. The same version changes nothing but reconciles the physical alarm, so resending it repairs a lost alarm. The expression is opaque; only `nextOccurrence(expression, after)` reads it, and it must return a safe integer after `after`, or `undefined` to stop ticking. It must be synchronous; when its answers change, call `reschedule()`. `get()` returns `{ expression, version, jitterMs, nextAt }`, where `nextAt` is the next tick's nominal time, or `undefined` when no schedule is live.
|
|
526
540
|
- **Jitter.** `set({ expression, version, jitterMs })` delays every tick by one fixed offset, so many objects sharing a popular expression do not all fire at the same instant. `jitterMs` is either a non-negative integer, the offset itself, or `{ maxMs }`, which picks an offset uniformly in `[0, maxMs]` when the version is stored. It defaults to 0, and both forms are at most 2^31 - 1 ms. The offset is stored in the schedule's row, so it costs no write per run, and resending the same version keeps it; only a new version picks a new one. A tick's nominal time `scheduledTime` is what `nextOccurrence` returned, and it fires at `dueAt`, which is `scheduledTime + jitterMs`. `idempotencyKey` derives from `scheduledTime`, and the next tick is always searched from nominal times, so the offset never drifts. Keep `jitterMs` below the expression's interval; durability cannot know the interval of an opaque expression. A larger offset is allowed: every tick still fires once, in nominal order, each `jitterMs` after its nominal time, even when that is after the next tick's nominal time.
|
|
527
541
|
- **Cancel.** `cancel(version?)` stops future ticks and resolves `true` when a schedule was live. Runs already due or running still finish and report. With a version, the cancel is ignored when the stored version is higher, and is otherwise kept as a one-row tombstone so a delayed `set` of that version or lower cannot revive the schedule.
|
|
542
|
+
- **Reschedule.** `nextOccurrence` may give different answers over time, for example when it reads which ticks the object owns from its own storage. Durability stores the next tick when it fires the previous one, so after such a change the stored tick can be later than one the object now has to fire. `reschedule()` asks `nextOccurrence` again and resolves `true` when the stored next tick changed, moving the alarm earlier or later in the same transaction, as a tick does, so a tick less than `alarmMinDelayMs` away fires from the in-memory timer. It searches from `now - jitterMs`, so the new next tick is the first whose `dueAt` is still ahead: a tick whose nominal time passed but whose `dueAt` has not is kept, and ticks whose `dueAt` already passed are skipped, not collapsed into a catch-up run. A stored tick that is already due is left to the alarm, which fires it and then asks `nextOccurrence` as usual. `reschedule()` resolves `false` in that case, when nothing changed, and when no schedule is live, cancelled or never set; it then changes nothing. Only the next tick is rewritten: the version, the stored jitter (a `{ maxMs }` offset is not picked again), a versioned cancel's tombstone, runs already due, queued or running, and outcome retries stay as they were, so `set` and `cancel` keep ordering by version. Use it when the version cannot change, for example because it also orders cancels. No tick fires twice across a reschedule: every tick that fired had a `dueAt` at or before now, and the new next tick's `dueAt` is after now. When `nextOccurrence` now returns `undefined`, ticking stops as it does after a tick, and a later `reschedule()` can resume it.
|
|
528
543
|
- **One tick, one run.** A tick inserts its run in the transaction that advances the stored next tick, so a replayed or duplicated alarm finds the cursor already moved and cannot fire the tick again; deduplication needs no finished-run records. When ticks were missed, for example while the object was down, they collapse into one run for the earliest missed time, and the next tick is the first whose `dueAt` is after now. A `nextOccurrence` that throws or returns an invalid time rolls the tick back and fails the alarm run, which is retried at `alarmMinDelayMs` and reported through `onAlarmRetry`.
|
|
529
544
|
- **Runs.** `run({ scheduledTime, dueAt, idempotencyKey, signal })` starts outside the alarm pass that recorded it, so a long run never delays later ticks. `idempotencyKey` is `<objectId>:<scheduledTime>`; on a named channel it ends with `:` and the JSON-encoded channel name, such as `<objectId>:<scheduledTime>:"library"`, so it never equals a default-channel key or another channel's. At most `maxConcurrentRuns` (default 10) are in flight; later runs wait durably in tick order. This limit is separate from the scheduler's `alarmConcurrency`. A run that throws, or exceeds `runTimeoutMs` (default 15 minutes, which aborts its `signal`), is `errored`. A run is attempted once and never retried. A timed-out run keeps its slot until its handler actually settles.
|
|
530
545
|
- **Keeping the object alive.** While runs or outcome deliveries are in flight, the alarm pass stays pending, so an alarm invocation stays open and holds the object in memory; it fires the next ticks and outcome retries itself, at their exact times. Before the wall-time limit the scheduler hands off at `alarmHandoffMs`, and the next alarm joins the same loop. A tick started by an in-memory timer is joined by the next physical alarm. Other helpers on the same scheduler still run on each alarm pass, but work of theirs that becomes due during the hold waits for it unless the scheduler uses `liveTimers`.
|
|
@@ -623,14 +638,14 @@ import {
|
|
|
623
638
|
type DurabilityLifecycleEvent,
|
|
624
639
|
} from 'durability';
|
|
625
640
|
import { DurabilitySocket, event, type SocketCall } from 'durability/socket';
|
|
626
|
-
import
|
|
641
|
+
import * as v from 'valibot';
|
|
627
642
|
|
|
628
|
-
const TodoSchema =
|
|
629
|
-
id:
|
|
630
|
-
title:
|
|
631
|
-
done:
|
|
643
|
+
const TodoSchema = v.object({
|
|
644
|
+
id: v.string(),
|
|
645
|
+
title: v.string(),
|
|
646
|
+
done: v.boolean(),
|
|
632
647
|
});
|
|
633
|
-
type Todo =
|
|
648
|
+
type Todo = v.InferOutput<typeof TodoSchema>;
|
|
634
649
|
type Metadata = { userId: string };
|
|
635
650
|
|
|
636
651
|
export class TodoList extends DurableObject<Env> {
|
|
@@ -655,11 +670,11 @@ export class TodoList extends DurableObject<Env> {
|
|
|
655
670
|
list: () => [...this.todos.values()],
|
|
656
671
|
// Typed input requires a Standard Schema; here the handler is a durable operation.
|
|
657
672
|
createTodo: {
|
|
658
|
-
input:
|
|
673
|
+
input: v.object({ id: v.string(), payload: TodoSchema }),
|
|
659
674
|
handle: this.durability.createTodo,
|
|
660
675
|
},
|
|
661
676
|
toggle: {
|
|
662
|
-
input:
|
|
677
|
+
input: v.string(),
|
|
663
678
|
handle: (id: string, call: SocketCall<Metadata>) => {
|
|
664
679
|
const todo = this.todos.get(id);
|
|
665
680
|
if (!todo) {
|
|
@@ -851,7 +866,7 @@ readonly socket = new DurabilitySocket({
|
|
|
851
866
|
context: this.ctx,
|
|
852
867
|
inbound: {
|
|
853
868
|
work: {
|
|
854
|
-
input:
|
|
869
|
+
input: v.object({ cursor: v.optional(v.string()) }),
|
|
855
870
|
handle: (query: { cursor?: string }) => this.tracker.records(query),
|
|
856
871
|
},
|
|
857
872
|
},
|
|
@@ -970,7 +985,7 @@ new DurabilitySemaphore({
|
|
|
970
985
|
- **`under`** — a reserve inside the limit is kept for leases; grants may use at most `limit - reserve`. Still never exceeds the limit, and leases have guaranteed headroom. The reserve never takes the last permit: with a reserve at or above the limit (including after lowering the limit), grants keep one, so groups excluded from leases and clients without lease support still make progress.
|
|
971
986
|
- **`over`** — leases come from a separate budget above the limit (the original gatekeeper behaviour); the effective ceiling is `limit + extra`.
|
|
972
987
|
|
|
973
|
-
Leases are issued only to clients that report `leases` (both default and named queues do), to groups without their own limit unless `limitedGroups: 'withinGroupLimit'`, and only to a client that holds no undrawn permits for that group. Each round shares the remaining lease budget equally among candidates, oldest demand first, up to `permitsPerLease`; with fewer permits left than candidates, the oldest get one each. When a round leaves demand unserved, the holder marks its replies `contended`, and each client returns the undrawn permits of leases in groups where it has nothing queued, so an idle lease cannot keep other clients waiting. A client that stops talking to the holder keeps its lease until `ttlMs`. Reports (`drawn`, `released` high-water marks) heartbeat a lease, and the queue reports a lease with permits in use at half its lifetime even when nothing else is happening, so long-running work keeps its permits counted; a lease unreported for `ttlMs` is retired rather than forgotten: its client may be cut off from the holder with work still running, so, like a claimed grant permit, its footprint stays counted until the client reports what it drew and released. The queue keeps reporting a stopped lease until the holder settles it. A report for a lease the holder never knew is answered retired and its live footprint charged against that round. On the client, a drawn permit is one-shot: releasing the item returns footprint to the holder but never makes the permit drawable again. Lowering a group's eligibility, or the limit, retires leases on the next round. A retired lease stops drawing at once but stays counted in full until its client confirms how much it drew, since it may have drawn after its last report.
|
|
988
|
+
Leases are issued only to clients that report `leases` (both default and named queues do), to groups without their own limit unless `limitedGroups: 'withinGroupLimit'`, and only to a client that holds no undrawn permits for that group. A group's limit counts its grants and the footprint of its leases together, in every provisioning mode and including retired leases not yet confirmed, so a leased group never admits past its limit. Each round shares the remaining lease budget equally among candidates, oldest demand first, up to `permitsPerLease`; with fewer permits left than candidates, the oldest get one each. When a round leaves demand unserved, the holder marks its replies `contended`, and each client returns the undrawn permits of leases in groups where it has nothing queued, so an idle lease cannot keep other clients waiting. A client that stops talking to the holder keeps its lease until `ttlMs`. Reports (`drawn`, `released` high-water marks) heartbeat a lease, and the queue reports a lease with permits in use at half its lifetime even when nothing else is happening, so long-running work keeps its permits counted; a lease unreported for `ttlMs` is retired rather than forgotten: its client may be cut off from the holder with work still running, so, like a claimed grant permit, its footprint stays counted until the client reports what it drew and released. The queue keeps reporting a stopped lease until the holder settles it. A report for a lease the holder never knew is answered retired and its live footprint charged against that round; the report does not name a group, so no group limit is charged. On the client, a drawn permit is one-shot: releasing the item returns footprint to the holder but never makes the permit drawable again. Lowering a group's eligibility, or the limit, retires leases on the next round. A retired lease stops drawing at once but stays counted in full until its client confirms how much it drew, since it may have drawn after its last report.
|
|
974
989
|
|
|
975
990
|
### Queueing work in a client Durable Object
|
|
976
991
|
|
package/dist/alarms/index.js
CHANGED
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _DurabilityAlarmsCore_instances, _DurabilityAlarmsCore_engine, _DurabilityAlarmsCore_channel, _DurabilityAlarmsCore_table, _DurabilityAlarmsCore_handlers, _DurabilityAlarmsCore_policy, _DurabilityAlarmsCore_lifecycle, _DurabilityAlarmsCore_emit, _DurabilityAlarmsCore_active, _DurabilityAlarmsCore_trackedSource, _DurabilityAlarmsCore_runDue, _DurabilityAlarmsCore_schedule, _DurabilityAlarmsCore_cancel, _DurabilityAlarmsCore_execute;
|
|
13
|
-
import
|
|
13
|
+
import * as v from 'valibot';
|
|
14
14
|
import { traceFunction, traceMethods } from '../dev.js';
|
|
15
15
|
import { DurableAlarmTimeoutError, DurableAttemptsExhaustedError, isErrorInstance, NonRetryableError, } from '../errors.js';
|
|
16
16
|
import { createLifecycleHooks, } from '../lifecycle.js';
|
|
@@ -22,23 +22,21 @@ import { assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js
|
|
|
22
22
|
import { createMemoize, deleteMemos, deleteMemosOfDroppedTable, memoizedKeys, } from '../memoize/index.js';
|
|
23
23
|
import { deleteAttempt, listCreated } from '../storage.js';
|
|
24
24
|
import { registerTrackedSource, } from '../tracker/source.js';
|
|
25
|
-
const alarmRowSchema =
|
|
26
|
-
name:
|
|
27
|
-
channel:
|
|
28
|
-
generation_id:
|
|
29
|
-
status:
|
|
30
|
-
scheduled_at:
|
|
31
|
-
next_attempt_at:
|
|
32
|
-
attempt:
|
|
33
|
-
last_error:
|
|
34
|
-
last_error_name:
|
|
35
|
-
created_at:
|
|
25
|
+
const alarmRowSchema = v.object({
|
|
26
|
+
name: v.string(),
|
|
27
|
+
channel: v.string(),
|
|
28
|
+
generation_id: v.string(),
|
|
29
|
+
status: v.picklist(['pending', 'failed']),
|
|
30
|
+
scheduled_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
31
|
+
next_attempt_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
32
|
+
attempt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
33
|
+
last_error: v.nullable(v.string()),
|
|
34
|
+
last_error_name: v.nullable(v.string()),
|
|
35
|
+
created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
36
36
|
});
|
|
37
37
|
const alarmsTable = { name: 'durability_alarms', key: 'name' };
|
|
38
|
-
const alarmHandlerSchema =
|
|
39
|
-
const readAlarm = (sql, channel, name) => alarmRowSchema
|
|
40
|
-
.optional()
|
|
41
|
-
.parse(sql
|
|
38
|
+
const alarmHandlerSchema = v.custom((value) => typeof value === 'function');
|
|
39
|
+
const readAlarm = (sql, channel, name) => v.parse(v.optional(alarmRowSchema), sql
|
|
42
40
|
.exec('SELECT * FROM durability_alarms WHERE channel = ? AND name = ?', channel, name)
|
|
43
41
|
.toArray()[0]);
|
|
44
42
|
const alarmRecord = (sql, row) => ({
|
|
@@ -83,7 +81,7 @@ class DurabilityAlarmsCore {
|
|
|
83
81
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_channel, assertChannel('DurabilityAlarms', config.channel ?? defaultChannel), "f");
|
|
84
82
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_table, { ...alarmsTable, channel: __classPrivateFieldGet(this, _DurabilityAlarmsCore_channel, "f") }, "f");
|
|
85
83
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_engine, engineFor(config), "f");
|
|
86
|
-
__classPrivateFieldSet(this, _DurabilityAlarmsCore_handlers, new Map(Object.entries(config.handlers).map(([name, handler]) => [name,
|
|
84
|
+
__classPrivateFieldSet(this, _DurabilityAlarmsCore_handlers, new Map(Object.entries(config.handlers).map(([name, handler]) => [name, v.parse(alarmHandlerSchema, handler)])), "f");
|
|
87
85
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_policy, createPolicyResolver(config, config.methods ?? {}, alarmMethodOptionsSchema), "f");
|
|
88
86
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_lifecycle, createLifecycleHooks((hook) => __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").emitter(hook), config.onLifecycleEvent), "f");
|
|
89
87
|
__classPrivateFieldSet(this, _DurabilityAlarmsCore_emit, __classPrivateFieldGet(this, _DurabilityAlarmsCore_lifecycle, "f").emit, "f");
|
|
@@ -157,9 +155,8 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_channel = ne
|
|
|
157
155
|
status,
|
|
158
156
|
name,
|
|
159
157
|
});
|
|
160
|
-
const records =
|
|
161
|
-
.array()
|
|
162
|
-
.parse(page.rows)
|
|
158
|
+
const records = v
|
|
159
|
+
.parse(v.array(alarmRowSchema), page.rows)
|
|
163
160
|
.map((row) => alarmRecord(sql, row));
|
|
164
161
|
return page.next === undefined
|
|
165
162
|
? { records }
|
|
@@ -182,7 +179,7 @@ _DurabilityAlarmsCore_engine = new WeakMap(), _DurabilityAlarmsCore_channel = ne
|
|
|
182
179
|
},
|
|
183
180
|
};
|
|
184
181
|
}, _DurabilityAlarmsCore_runDue = async function _DurabilityAlarmsCore_runDue(now, info) {
|
|
185
|
-
const due =
|
|
182
|
+
const due = v.parse(v.array(alarmRowSchema), __classPrivateFieldGet(this, _DurabilityAlarmsCore_engine, "f").sql
|
|
186
183
|
.exec(`SELECT * FROM durability_alarms
|
|
187
184
|
WHERE channel = ? AND status = 'pending' AND next_attempt_at <= ?
|
|
188
185
|
ORDER BY next_attempt_at ASC
|
package/dist/fanout/index.js
CHANGED
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _DurabilityFanout_instances, _DurabilityFanout_engine, _DurabilityFanout_channel, _DurabilityFanout_table, _DurabilityFanout_routing, _DurabilityFanout_targets, _DurabilityFanout_policies, _DurabilityFanout_dlq, _DurabilityFanout_maxBatchSize, _DurabilityFanout_batchDelayMs, _DurabilityFanout_lifecycle, _DurabilityFanout_emit, _DurabilityFanout_running, _DurabilityFanout_pending, _DurabilityFanout_firstAttemptAt, _DurabilityFanout_runDue, _DurabilityFanout_runTarget, _DurabilityFanout_postpone, _DurabilityFanout_deliver, _DurabilityFanout_ackUnsettled, _DurabilityFanout_message, _DurabilityFanout_remove, _DurabilityFanout_removeDelivery, _DurabilityFanout_retry, _DurabilityFanout_deadLetter, _DurabilityFanout_emitTerminal, _DurabilityFanout_transaction, _DurabilityFanout_pendingCount, _DurabilityFanout_adjustPending, _DurabilityFanout_listDue, _DurabilityFanout_deadline;
|
|
13
|
-
import
|
|
13
|
+
import * as v from 'valibot';
|
|
14
14
|
import { traceFunction, traceMethods } from '../dev.js';
|
|
15
15
|
import { DurableRetryPolicyError, FanoutEnqueueError, FanoutSettlementError, FanoutTimeoutError, reportFailure, serializeError, } from '../errors.js';
|
|
16
16
|
import { createLifecycleHooks, } from '../lifecycle.js';
|
|
@@ -23,41 +23,43 @@ import { assertPositiveInteger, createPolicyResolver, methodOptionsSchema, } fro
|
|
|
23
23
|
import { assertChannel, defaultChannel, engineFor, } from '../scheduler/index.js';
|
|
24
24
|
import { deserialize, serialize } from '../serialization.js';
|
|
25
25
|
import { claimAttempt, deleteAttempt, updateAttempt, } from '../storage.js';
|
|
26
|
-
const fanoutMessageRowSchema =
|
|
27
|
-
key:
|
|
28
|
-
id:
|
|
29
|
-
payload:
|
|
30
|
-
targets:
|
|
31
|
-
remaining:
|
|
32
|
-
seq:
|
|
33
|
-
created_at:
|
|
34
|
-
generation_id:
|
|
26
|
+
const fanoutMessageRowSchema = v.object({
|
|
27
|
+
key: v.string(),
|
|
28
|
+
id: v.string(),
|
|
29
|
+
payload: v.string(),
|
|
30
|
+
targets: v.string(),
|
|
31
|
+
remaining: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
32
|
+
seq: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
33
|
+
created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
34
|
+
generation_id: v.string(),
|
|
35
35
|
});
|
|
36
|
-
const deliveryRowSchema =
|
|
37
|
-
id:
|
|
38
|
-
message_key:
|
|
39
|
-
target_id:
|
|
40
|
-
seq:
|
|
41
|
-
status:
|
|
42
|
-
phase:
|
|
43
|
-
attempt:
|
|
44
|
-
next_attempt_at:
|
|
45
|
-
last_error:
|
|
46
|
-
last_error_name:
|
|
47
|
-
created_at:
|
|
48
|
-
generation_id:
|
|
49
|
-
dead_lettered_at:
|
|
50
|
-
dead_letter_reason:
|
|
36
|
+
const deliveryRowSchema = v.object({
|
|
37
|
+
id: v.string(),
|
|
38
|
+
message_key: v.string(),
|
|
39
|
+
target_id: v.string(),
|
|
40
|
+
seq: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
41
|
+
status: v.literal('pending'),
|
|
42
|
+
phase: v.picklist(['delivery', 'dead_letter']),
|
|
43
|
+
attempt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
44
|
+
next_attempt_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
45
|
+
last_error: v.nullable(v.string()),
|
|
46
|
+
last_error_name: v.nullable(v.string()),
|
|
47
|
+
created_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
48
|
+
generation_id: v.string(),
|
|
49
|
+
dead_lettered_at: v.nullable(v.pipe(v.number(), v.safeInteger(), v.minValue(0))),
|
|
50
|
+
dead_letter_reason: v.nullable(v.picklist(['explicit', 'exhausted'])),
|
|
51
51
|
});
|
|
52
52
|
const deliveriesTable = {
|
|
53
53
|
name: 'durability_fanout_deliveries',
|
|
54
54
|
key: 'id',
|
|
55
55
|
};
|
|
56
|
-
const countRowSchema =
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
.
|
|
56
|
+
const countRowSchema = v.object({
|
|
57
|
+
count: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
58
|
+
});
|
|
59
|
+
const seqRowSchema = v.object({
|
|
60
|
+
seq: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
|
|
61
|
+
});
|
|
62
|
+
const readManifest = (sql, channel, key) => v.parse(v.optional(fanoutMessageRowSchema), sql
|
|
61
63
|
.exec('SELECT * FROM durability_fanout_messages WHERE channel = ? AND key = ?', channel, key)
|
|
62
64
|
.toArray()[0]);
|
|
63
65
|
const attemptOf = (row) => ({
|
|
@@ -204,7 +206,7 @@ export class DurabilityFanout {
|
|
|
204
206
|
}
|
|
205
207
|
continue;
|
|
206
208
|
}
|
|
207
|
-
const { seq } =
|
|
209
|
+
const { seq } = v.parse(seqRowSchema, sql
|
|
208
210
|
.exec(`UPDATE durability_fanout_seq
|
|
209
211
|
SET next_seq = next_seq + 1
|
|
210
212
|
WHERE id = 1
|
|
@@ -297,10 +299,8 @@ _DurabilityFanout_engine = new WeakMap(), _DurabilityFanout_channel = new WeakMa
|
|
|
297
299
|
}
|
|
298
300
|
return Math.ceil(createdAt / __classPrivateFieldGet(this, _DurabilityFanout_batchDelayMs, "f")) * __classPrivateFieldGet(this, _DurabilityFanout_batchDelayMs, "f");
|
|
299
301
|
}, _DurabilityFanout_runDue = async function _DurabilityFanout_runDue() {
|
|
300
|
-
const targets =
|
|
301
|
-
.object({ target_id:
|
|
302
|
-
.array()
|
|
303
|
-
.parse(__classPrivateFieldGet(this, _DurabilityFanout_engine, "f").sql
|
|
302
|
+
const targets = v
|
|
303
|
+
.parse(v.array(v.object({ target_id: v.string() })), __classPrivateFieldGet(this, _DurabilityFanout_engine, "f").sql
|
|
304
304
|
.exec(`SELECT DISTINCT target_id FROM durability_fanout_deliveries
|
|
305
305
|
WHERE channel = ?
|
|
306
306
|
ORDER BY target_id`, __classPrivateFieldGet(this, _DurabilityFanout_channel, "f"))
|
|
@@ -457,7 +457,7 @@ _DurabilityFanout_engine = new WeakMap(), _DurabilityFanout_channel = new WeakMa
|
|
|
457
457
|
if (!state.finished) {
|
|
458
458
|
continue;
|
|
459
459
|
}
|
|
460
|
-
const current =
|
|
460
|
+
const current = v.parse(v.optional(deliveryRowSchema), __classPrivateFieldGet(this, _DurabilityFanout_engine, "f").sql
|
|
461
461
|
.exec(`SELECT * FROM durability_fanout_deliveries
|
|
462
462
|
WHERE channel = ? AND id = ?`, __classPrivateFieldGet(this, _DurabilityFanout_channel, "f"), state.claim.row.id)
|
|
463
463
|
.toArray()[0]);
|
|
@@ -678,7 +678,7 @@ _DurabilityFanout_engine = new WeakMap(), _DurabilityFanout_channel = new WeakMa
|
|
|
678
678
|
throw error;
|
|
679
679
|
}
|
|
680
680
|
}, _DurabilityFanout_pendingCount = function _DurabilityFanout_pendingCount() {
|
|
681
|
-
__classPrivateFieldSet(this, _DurabilityFanout_pending, __classPrivateFieldGet(this, _DurabilityFanout_pending, "f") ??
|
|
681
|
+
__classPrivateFieldSet(this, _DurabilityFanout_pending, __classPrivateFieldGet(this, _DurabilityFanout_pending, "f") ?? v.parse(countRowSchema, __classPrivateFieldGet(this, _DurabilityFanout_engine, "f").sql
|
|
682
682
|
.exec(`SELECT COUNT(*) AS count FROM durability_fanout_deliveries
|
|
683
683
|
WHERE channel = ? AND status = 'pending'`, __classPrivateFieldGet(this, _DurabilityFanout_channel, "f"))
|
|
684
684
|
.toArray()[0]).count, "f");
|
|
@@ -688,7 +688,7 @@ _DurabilityFanout_engine = new WeakMap(), _DurabilityFanout_channel = new WeakMa
|
|
|
688
688
|
__classPrivateFieldSet(this, _DurabilityFanout_pending, __classPrivateFieldGet(this, _DurabilityFanout_pending, "f") + delta, "f");
|
|
689
689
|
}
|
|
690
690
|
}, _DurabilityFanout_listDue = function _DurabilityFanout_listDue(target, now) {
|
|
691
|
-
const first =
|
|
691
|
+
const first = v.parse(v.array(deliveryRowSchema), __classPrivateFieldGet(this, _DurabilityFanout_engine, "f").sql
|
|
692
692
|
.exec(`SELECT * FROM durability_fanout_deliveries
|
|
693
693
|
WHERE channel = ? AND target_id = ? AND phase = 'delivery'
|
|
694
694
|
AND status = 'pending' AND attempt = 0 AND next_attempt_at <= ?
|
|
@@ -698,7 +698,7 @@ _DurabilityFanout_engine = new WeakMap(), _DurabilityFanout_channel = new WeakMa
|
|
|
698
698
|
if (first.length >= __classPrivateFieldGet(this, _DurabilityFanout_maxBatchSize, "f")) {
|
|
699
699
|
return first;
|
|
700
700
|
}
|
|
701
|
-
const retries =
|
|
701
|
+
const retries = v.parse(v.array(deliveryRowSchema), __classPrivateFieldGet(this, _DurabilityFanout_engine, "f").sql
|
|
702
702
|
.exec(`SELECT * FROM durability_fanout_deliveries
|
|
703
703
|
WHERE channel = ? AND target_id = ? AND status = 'pending'
|
|
704
704
|
AND attempt > 0 AND next_attempt_at <= ?
|
|
@@ -707,19 +707,6 @@ _DurabilityFanout_engine = new WeakMap(), _DurabilityFanout_channel = new WeakMa
|
|
|
707
707
|
.toArray());
|
|
708
708
|
return [...first, ...retries];
|
|
709
709
|
}, _DurabilityFanout_deadline = async function _DurabilityFanout_deadline(run, timeoutMs) {
|
|
710
|
-
|
|
711
|
-
try {
|
|
712
|
-
await Promise.race([
|
|
713
|
-
Promise.resolve().then(run),
|
|
714
|
-
new Promise((_resolve, reject) => {
|
|
715
|
-
timer = setTimeout(() => reject(new FanoutTimeoutError(timeoutMs)), timeoutMs);
|
|
716
|
-
}),
|
|
717
|
-
]);
|
|
718
|
-
}
|
|
719
|
-
finally {
|
|
720
|
-
if (timer !== undefined) {
|
|
721
|
-
clearTimeout(timer);
|
|
722
|
-
}
|
|
723
|
-
}
|
|
710
|
+
await __classPrivateFieldGet(this, _DurabilityFanout_engine, "f").timer.withTimeout(Promise.resolve().then(run), timeoutMs, () => new FanoutTimeoutError(timeoutMs));
|
|
724
711
|
};
|
|
725
712
|
traceMethods(DurabilityFanout.prototype, 'DurabilityFanout');
|
package/dist/log/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as v from 'valibot';
|
|
2
2
|
import { RoutingLoad, type EnqueueResult, type LoadSnapshot } from '../routing/load.js';
|
|
3
3
|
import { type SchedulerAttachment } from '../scheduler/index.js';
|
|
4
4
|
/**
|
|
@@ -6,22 +6,17 @@ import { type SchedulerAttachment } from '../scheduler/index.js';
|
|
|
6
6
|
* redelivered under a fresh `batch_id`, so an ack from a previous holder is
|
|
7
7
|
* rejected rather than advancing a range someone else now owns.
|
|
8
8
|
*/
|
|
9
|
-
declare const logLeaseRowSchema:
|
|
10
|
-
batch_id:
|
|
11
|
-
consumer:
|
|
12
|
-
first_offset:
|
|
13
|
-
last_offset:
|
|
14
|
-
state:
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
attempt: z.ZodNumber;
|
|
21
|
-
expires_at: z.ZodNumber;
|
|
22
|
-
created_at: z.ZodNumber;
|
|
23
|
-
}, z.core.$strip>;
|
|
24
|
-
export type LogLeaseRow = z.infer<typeof logLeaseRowSchema>;
|
|
9
|
+
declare const logLeaseRowSchema: v.ObjectSchema<{
|
|
10
|
+
readonly batch_id: v.StringSchema<undefined>;
|
|
11
|
+
readonly consumer: v.StringSchema<undefined>;
|
|
12
|
+
readonly first_offset: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
13
|
+
readonly last_offset: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
14
|
+
readonly state: v.PicklistSchema<["active", "pending", "acked", "skipped"], undefined>;
|
|
15
|
+
readonly attempt: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.GtValueAction<number, 0, undefined>]>;
|
|
16
|
+
readonly expires_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
17
|
+
readonly created_at: v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>]>;
|
|
18
|
+
}, undefined>;
|
|
19
|
+
export type LogLeaseRow = v.InferOutput<typeof logLeaseRowSchema>;
|
|
25
20
|
export type LogBounds = {
|
|
26
21
|
/**
|
|
27
22
|
* Lowest readable offset, counting flushed segments that can be rehydrated.
|