durability 5.2.1 → 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 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 active handler in the same instance.
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. If a handler ignores abort, its in-memory lock and concurrency permit remain held until it actually settles, so a same-isolate retry cannot overlap it.
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`.
@@ -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
 
@@ -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
- let timer;
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');
@@ -78,7 +78,7 @@ const runDown = (sql, table, name, down) => {
78
78
  * scripts it stored. Refuses when the schema is further ahead than
79
79
  * {@link maxFutureMigrations}.
80
80
  */
81
- const revertFutureMigrations = (storage, scope, history, known) => {
81
+ const revertFutureMigrations = (sql, scope, history, known) => {
82
82
  const future = history.filter(({ name }) => !known.has(name)).reverse();
83
83
  if (future.length === 0) {
84
84
  return [];
@@ -86,11 +86,9 @@ const revertFutureMigrations = (storage, scope, history, known) => {
86
86
  if (future.length > maxFutureMigrations) {
87
87
  throw new Error(`${scope.label} schema is ${future.length} migrations ahead of this version; refusing to revert more than ${maxFutureMigrations}`);
88
88
  }
89
- storage.transactionSync(() => {
90
- for (const row of future) {
91
- runDown(storage.sql, scope.table, row.name, row.down);
92
- }
93
- });
89
+ for (const row of future) {
90
+ runDown(sql, scope.table, row.name, row.down);
91
+ }
94
92
  return future.map(({ name }) => name);
95
93
  };
96
94
  const currentRowSchema = v.object({
@@ -130,27 +128,29 @@ const migrateScope = (storage, scope, migrations, target) => {
130
128
  if (resolvedTarget !== null && targetIndex === -1) {
131
129
  throw new Error(`Unknown ${scope.label} migration target "${resolvedTarget}"`);
132
130
  }
133
- ensureHistoryTable(storage.sql, scope.table);
134
- if (scope.table === migrationTableName) {
135
- translateLegacyHistory(storage.sql);
136
- }
137
- const known = new Set(migrations.map(({ name }) => name));
138
- const history = readHistory(storage.sql, scope, migrations);
139
- const rolledBack = revertFutureMigrations(storage, scope, history, known);
140
- const applied = new Map(history
141
- .filter(({ name }) => known.has(name))
142
- .map((row) => [row.name, row]));
143
- const toRevert = migrations
144
- .slice(targetIndex + 1)
145
- .filter(({ name }) => applied.has(name))
146
- .reverse();
147
- const toApply = migrations
148
- .slice(0, targetIndex + 1)
149
- .filter(({ name }) => !applied.has(name));
150
- const toRefresh = migrations
151
- .slice(0, targetIndex + 1)
152
- .filter(({ name, down }) => applied.has(name) && applied.get(name)?.down !== down);
153
- storage.transactionSync(() => {
131
+ // Every step shares one transaction so a failure anywhere, including after
132
+ // future migrations were reverted, leaves the schema and history as they were.
133
+ return storage.transactionSync(() => {
134
+ ensureHistoryTable(storage.sql, scope.table);
135
+ if (scope.table === migrationTableName) {
136
+ translateLegacyHistory(storage.sql);
137
+ }
138
+ const known = new Set(migrations.map(({ name }) => name));
139
+ const history = readHistory(storage.sql, scope, migrations);
140
+ const rolledBack = revertFutureMigrations(storage.sql, scope, history, known);
141
+ const applied = new Map(history
142
+ .filter(({ name }) => known.has(name))
143
+ .map((row) => [row.name, row]));
144
+ const toRevert = migrations
145
+ .slice(targetIndex + 1)
146
+ .filter(({ name }) => applied.has(name))
147
+ .reverse();
148
+ const toApply = migrations
149
+ .slice(0, targetIndex + 1)
150
+ .filter(({ name }) => !applied.has(name));
151
+ const toRefresh = migrations
152
+ .slice(0, targetIndex + 1)
153
+ .filter(({ name, down }) => applied.has(name) && applied.get(name)?.down !== down);
154
154
  for (const migration of toRevert) {
155
155
  runDown(storage.sql, scope.table, migration.name, migration.down);
156
156
  }
@@ -162,17 +162,17 @@ const migrateScope = (storage, scope, migrations, target) => {
162
162
  for (const migration of toRefresh) {
163
163
  storage.sql.exec(`UPDATE ${scope.table} SET down = ? WHERE name = ?`, migration.down, migration.name);
164
164
  }
165
+ const remaining = v.parse(v.object({ count: v.pipe(v.number(), v.safeInteger()) }), storage.sql
166
+ .exec(`SELECT COUNT(*) AS count FROM ${scope.table}`)
167
+ .toArray()[0]).count;
168
+ if (remaining === 0) {
169
+ storage.sql.exec(`DROP TABLE IF EXISTS ${scope.table}`);
170
+ }
171
+ return {
172
+ applied: toApply.map(({ name }) => name),
173
+ rolledBack: [...rolledBack, ...toRevert.map(({ name }) => name)],
174
+ };
165
175
  });
166
- const remaining = v.parse(v.object({ count: v.pipe(v.number(), v.safeInteger()) }), storage.sql
167
- .exec(`SELECT COUNT(*) AS count FROM ${scope.table}`)
168
- .toArray()[0]).count;
169
- if (remaining === 0) {
170
- storage.sql.exec(`DROP TABLE IF EXISTS ${scope.table}`);
171
- }
172
- return {
173
- applied: toApply.map(({ name }) => name),
174
- rolledBack: [...rolledBack, ...toRevert.map(({ name }) => name)],
175
- };
176
176
  };
177
177
  /**
178
178
  * Moves one capability's schema to a target migration, applying or reverting as
@@ -361,20 +361,7 @@ _DurabilityOutbox_engine = new WeakMap(), _DurabilityOutbox_channel = new WeakMa
361
361
  }
362
362
  return infrastructureRetryMs;
363
363
  }, _DurabilityOutbox_deadline = async function _DurabilityOutbox_deadline(run) {
364
- let timer;
365
- try {
366
- await Promise.race([
367
- Promise.resolve().then(run),
368
- new Promise((_resolve, reject) => {
369
- timer = setTimeout(() => reject(new OutboxTimeoutError(__classPrivateFieldGet(this, _DurabilityOutbox_attemptTimeoutMs, "f"))), __classPrivateFieldGet(this, _DurabilityOutbox_attemptTimeoutMs, "f"));
370
- }),
371
- ]);
372
- }
373
- finally {
374
- if (timer !== undefined) {
375
- clearTimeout(timer);
376
- }
377
- }
364
+ await __classPrivateFieldGet(this, _DurabilityOutbox_engine, "f").timer.withTimeout(Promise.resolve().then(run), __classPrivateFieldGet(this, _DurabilityOutbox_attemptTimeoutMs, "f"), () => new OutboxTimeoutError(__classPrivateFieldGet(this, _DurabilityOutbox_attemptTimeoutMs, "f")));
378
365
  };
379
366
  (() => {
380
367
  stageWithin = (outbox, sql, key, value) => {
@@ -111,7 +111,8 @@ export type DurabilityScheduleConfig = SchedulerAttachment & {
111
111
  channel?: string;
112
112
  /**
113
113
  * Returns the first tick strictly after `after`, or `undefined` for none.
114
- * Must be synchronous and deterministic; durability ships no cron parser.
114
+ * Must be synchronous; durability ships no cron parser. When its answers
115
+ * change, call {@link DurabilitySchedule.reschedule}.
115
116
  */
116
117
  nextOccurrence(expression: string, after: number): number | undefined;
117
118
  /** The work of one tick. Throwing or timing out makes the run `errored`. */
@@ -154,6 +155,17 @@ export declare class DurabilitySchedule {
154
155
  * alarm if it was lost.
155
156
  */
156
157
  set(definition: ScheduleDefinition): Promise<boolean>;
158
+ /**
159
+ * Recomputes the stored next tick with `nextOccurrence`, for when its answers
160
+ * changed for a reason durability can't see, and resolves `true` when the
161
+ * stored next tick changed. Resolves `false` when it didn't, when no schedule
162
+ * is live, or when the stored tick is already due: the alarm fires it, and
163
+ * the advance after it asks `nextOccurrence` again. The search starts from
164
+ * `now - jitterMs`, so a tick whose firing time is still ahead is kept and
165
+ * one whose firing time passed is skipped, not run. The version, the stored
166
+ * jitter, cancellations, and runs are unchanged.
167
+ */
168
+ reschedule(): Promise<boolean>;
157
169
  /**
158
170
  * Stops future ticks and resolves `true` when a schedule was live. Runs
159
171
  * already due or running still finish and report. With `version`, the
@@ -121,22 +121,6 @@ const assertDelay = (name, value) => {
121
121
  throw new RangeError(`${name} must be at most ${maxDelayMs}`);
122
122
  }
123
123
  };
124
- const deadline = async (work, timeoutMs, onTimeout) => {
125
- let timer;
126
- try {
127
- await Promise.race([
128
- work,
129
- new Promise((_resolve, reject) => {
130
- timer = setTimeout(() => reject(onTimeout()), timeoutMs);
131
- }),
132
- ]);
133
- }
134
- finally {
135
- if (timer !== undefined) {
136
- clearTimeout(timer);
137
- }
138
- }
139
- };
140
124
  /**
141
125
  * Experimental. One recurring schedule per channel of a Durable Object whose
142
126
  * ticks start individually tracked, overlapping runs and report each run's
@@ -258,6 +242,39 @@ export class DurabilitySchedule {
258
242
  __classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_wake).call(this);
259
243
  return applied;
260
244
  }
245
+ /**
246
+ * Recomputes the stored next tick with `nextOccurrence`, for when its answers
247
+ * changed for a reason durability can't see, and resolves `true` when the
248
+ * stored next tick changed. Resolves `false` when it didn't, when no schedule
249
+ * is live, or when the stored tick is already due: the alarm fires it, and
250
+ * the advance after it asks `nextOccurrence` again. The search starts from
251
+ * `now - jitterMs`, so a tick whose firing time is still ahead is kept and
252
+ * one whose firing time passed is skipped, not run. The version, the stored
253
+ * jitter, cancellations, and runs are unchanged.
254
+ */
255
+ async reschedule() {
256
+ const changed = await __classPrivateFieldGet(this, _DurabilitySchedule_engine, "f").transaction((sql) => {
257
+ const schedule = readSchedule(sql, __classPrivateFieldGet(this, _DurabilitySchedule_channel, "f"));
258
+ if (schedule === undefined || schedule.expression === null) {
259
+ return false;
260
+ }
261
+ const now = Date.now();
262
+ const dueAt = firesAt(schedule);
263
+ if (dueAt !== undefined && dueAt <= now) {
264
+ return false;
265
+ }
266
+ const nextAt = __classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_nextAfter).call(this, schedule.expression, now - schedule.jitter_ms) ?? null;
267
+ if (nextAt === schedule.next_at) {
268
+ return false;
269
+ }
270
+ sql.exec('UPDATE durability_schedule SET next_at = ? WHERE channel = ? AND id = 1', nextAt, __classPrivateFieldGet(this, _DurabilitySchedule_channel, "f"));
271
+ return true;
272
+ });
273
+ if (changed) {
274
+ __classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_wake).call(this);
275
+ }
276
+ return changed;
277
+ }
261
278
  /**
262
279
  * Stops future ticks and resolves `true` when a schedule was live. Runs
263
280
  * already due or running still finish and report. With `version`, the
@@ -381,20 +398,17 @@ _DurabilitySchedule_engine = new WeakMap(), _DurabilitySchedule_channel = new We
381
398
  if (__classPrivateFieldGet(this, _DurabilitySchedule_woken, "f")) {
382
399
  return;
383
400
  }
384
- let timer;
401
+ let cancelTimeout;
385
402
  try {
386
403
  await new Promise((resolve) => {
387
404
  __classPrivateFieldSet(this, _DurabilitySchedule_wakeSleeper, resolve, "f");
388
405
  if (until !== undefined) {
389
- const delay = Math.min(Math.max(until - Date.now(), 0), maxDelayMs);
390
- timer = setTimeout(resolve, delay);
406
+ cancelTimeout = __classPrivateFieldGet(this, _DurabilitySchedule_engine, "f").timer.schedule(resolve, until - Date.now());
391
407
  }
392
408
  });
393
409
  }
394
410
  finally {
395
- if (timer !== undefined) {
396
- clearTimeout(timer);
397
- }
411
+ cancelTimeout?.();
398
412
  __classPrivateFieldSet(this, _DurabilitySchedule_wakeSleeper, undefined, "f");
399
413
  }
400
414
  }, _DurabilitySchedule_nextWakeAt = function _DurabilitySchedule_nextWakeAt() {
@@ -552,7 +566,8 @@ async function _DurabilitySchedule_reserveSlots(mark) {
552
566
  __classPrivateFieldGet(this, _DurabilitySchedule_emit, "f").call(this, { ...__classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_entity).call(this, scheduledTime), type: 'started' });
553
567
  const controller = new AbortController();
554
568
  const handler = Promise.resolve().then(() => __classPrivateFieldGet(this, _DurabilitySchedule_run, "f").call(this, { ...run, signal: controller.signal }));
555
- const recorded = deadline(handler, __classPrivateFieldGet(this, _DurabilitySchedule_runTimeoutMs, "f"), () => {
569
+ const recorded = __classPrivateFieldGet(this, _DurabilitySchedule_engine, "f").timer
570
+ .withTimeout(handler, __classPrivateFieldGet(this, _DurabilitySchedule_runTimeoutMs, "f"), () => {
556
571
  const error = new ScheduledRunTimeoutError(__classPrivateFieldGet(this, _DurabilitySchedule_runTimeoutMs, "f"));
557
572
  controller.abort(error);
558
573
  return error;
@@ -612,7 +627,7 @@ async function _DurabilitySchedule_reserveSlots(mark) {
612
627
  });
613
628
  }, _DurabilitySchedule_deliverOnce = async function _DurabilitySchedule_deliverOnce(run, outcome, attempt) {
614
629
  try {
615
- await deadline(Promise.resolve().then(() => __classPrivateFieldGet(this, _DurabilitySchedule_onOutcome, "f").call(this, { ...run, attempt }, outcome)), __classPrivateFieldGet(this, _DurabilitySchedule_outcomeTimeoutMs, "f"), () => new ScheduledOutcomeTimeoutError(__classPrivateFieldGet(this, _DurabilitySchedule_outcomeTimeoutMs, "f")));
630
+ await __classPrivateFieldGet(this, _DurabilitySchedule_engine, "f").timer.withTimeout(Promise.resolve().then(() => __classPrivateFieldGet(this, _DurabilitySchedule_onOutcome, "f").call(this, { ...run, attempt }, outcome)), __classPrivateFieldGet(this, _DurabilitySchedule_outcomeTimeoutMs, "f"), () => new ScheduledOutcomeTimeoutError(__classPrivateFieldGet(this, _DurabilitySchedule_outcomeTimeoutMs, "f")));
616
631
  }
617
632
  catch (failure) {
618
633
  await __classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_failDelivery).call(this, run, attempt, failure);
@@ -2,6 +2,7 @@ import { type SerializedError } from '../errors.js';
2
2
  import { type DurabilityLifecycleEvent, type Emit, type LifecycleEntity } from '../lifecycle.js';
3
3
  import { type MigrationCapability } from '../migrations.js';
4
4
  import { type AttemptPolicy } from '../policy.js';
5
+ import { SharedTimer } from '../shared-timer.js';
5
6
  import { type DurableTable } from '../storage.js';
6
7
  import type { DurableMigrations } from '@durability/storage';
7
8
  /** The Durable Object state durability needs: storage plus optional `waitUntil`. */
@@ -132,6 +133,8 @@ export type ExecutionSpec = {
132
133
  */
133
134
  export declare class Engine {
134
135
  readonly sql: SqlStorage;
136
+ /** Holds the timeouts of every helper on the object behind one native timer. */
137
+ readonly timer: SharedTimer;
135
138
  private context;
136
139
  private readonly storage;
137
140
  private alarmConcurrency;
@@ -250,7 +253,10 @@ export declare class Engine {
250
253
  /**
251
254
  * Registers the execution in its active map synchronously, then claims and
252
255
  * runs one attempt in the background. The returned promise settles when the
253
- * attempt's outcome has been persisted, not when a timed-out handler settles.
256
+ * attempt's outcome has been persisted. The key leaves the active map and
257
+ * its permit is released then too, whether or not a timed-out handler has
258
+ * settled: nothing can stop a handler, and one that never settles would
259
+ * otherwise keep its retry from ever starting.
254
260
  */
255
261
  startExecution(spec: ExecutionSpec, eagerPermit?: () => void): Promise<void>;
256
262
  private failExhausted;
@@ -289,6 +295,18 @@ export declare class DurabilityScheduler {
289
295
  constructor(config: DurabilitySchedulerConfig);
290
296
  /** Runs due work for every attached helper and re-arms the physical alarm. */
291
297
  alarm(info?: AlarmInvocationInfo): Promise<void>;
298
+ /**
299
+ * Runs `callback` once after `delayMs`, like the global `setTimeout`, and
300
+ * returns a function that cancels it. The timeout shares one native timer
301
+ * with every helper on the object, so any number of them can be pending
302
+ * without reaching the runtime's limit of 10,000 native timers.
303
+ *
304
+ * The timeout exists only in memory and an eviction drops it; schedule work
305
+ * that must survive one with `DurabilityAlarms`. Callbacks due at the same
306
+ * time run one after another in the same task, and one that throws is
307
+ * reported as `durability.timer.callback_failed` instead of being thrown.
308
+ */
309
+ setTimeout(callback: () => void, delayMs: number): () => void;
292
310
  }
293
311
  /** Resolves the engine a helper attaches to: the object's one scheduler. */
294
312
  export declare const engineFor: (attachment: SchedulerAttachment) => Engine;
@@ -5,6 +5,7 @@ import { createEmitter, } from '../lifecycle.js';
5
5
  import { isSchemaCurrent, migrateCapability, } from '../migrations.js';
6
6
  import { assertPositiveInteger } from '../policy.js';
7
7
  import { deleteMemos, deleteStaleMemos } from '../memoize/index.js';
8
+ import { SharedTimer } from '../shared-timer.js';
8
9
  import { channelBindings, channelClause, claimAttempt, exhaustAttempts, tableOwner, updateAttempt, } from '../storage.js';
9
10
  const minAlarmDelayMs = 1000;
10
11
  const failedRunRetryMs = 1000;
@@ -17,9 +18,6 @@ const schedulerOptionKeys = [
17
18
  'onAlarmRetry',
18
19
  ];
19
20
  const maxAlarmDelayMs = 15000;
20
- // setTimeout treats delays past this as zero, so anything further out can only
21
- // be served by the physical alarm fallback.
22
- const maxTimerMs = 2 ** 31 - 1;
23
21
  const pendingHeadRowSchema = v.object({
24
22
  pk: v.string(),
25
23
  channel: v.optional(v.string()),
@@ -85,6 +83,8 @@ const decideRetry = (spec, attempt, error) => {
85
83
  */
86
84
  export class Engine {
87
85
  constructor(context, options) {
86
+ /** Holds the timeouts of every helper on the object behind one native timer. */
87
+ this.timer = new SharedTimer();
88
88
  this.alarmConcurrency = defaultAlarmConcurrency;
89
89
  this.alarmMinDelayMs = 15000;
90
90
  this.alarmHandoffMs = 14 * 60000;
@@ -428,7 +428,7 @@ export class Engine {
428
428
  return;
429
429
  }
430
430
  if (this.earlyAlarm) {
431
- clearTimeout(this.earlyAlarm.timer);
431
+ this.earlyAlarm.cancel();
432
432
  this.earlyAlarm = undefined;
433
433
  }
434
434
  if (timestamp === undefined) {
@@ -437,16 +437,12 @@ export class Engine {
437
437
  const delay = timestamp - Date.now();
438
438
  // Without liveTimers the in-memory timer only covers the sub-min-delay
439
439
  // window; the physical alarm handles everything beyond it. With liveTimers
440
- // it fires at the exact due time no matter how far out, except past the
441
- // timer's range where only the physical fallback can serve it.
440
+ // it fires at the exact due time no matter how far out.
442
441
  if (!this.liveTimers && delay >= this.alarmMinDelayMs) {
443
442
  return;
444
443
  }
445
- if (delay > maxTimerMs) {
446
- return;
447
- }
448
- const timer = setTimeout(() => {
449
- if (this.earlyAlarm?.timer !== timer) {
444
+ const cancel = this.timer.schedule(() => {
445
+ if (this.earlyAlarm?.cancel !== cancel) {
450
446
  return;
451
447
  }
452
448
  this.earlyAlarm = undefined;
@@ -454,8 +450,8 @@ export class Engine {
454
450
  const onError = (error) => reportFailure('durability.background_execution.failed', { scheduledTime: timestamp }, error);
455
451
  const execution = this.alarm().catch(onError);
456
452
  this.waitUntil(execution, onError);
457
- }, Math.max(delay, 0));
458
- this.earlyAlarm = { timestamp, timer };
453
+ }, delay);
454
+ this.earlyAlarm = { timestamp, cancel };
459
455
  }
460
456
  /** Coalesces refreshes without dropping requests arriving after the pending-work read. */
461
457
  scheduleNextAlarm() {
@@ -647,20 +643,18 @@ export class Engine {
647
643
  void execution.catch(() => undefined);
648
644
  const handoff = Symbol('alarm handoff');
649
645
  const remaining = this.alarmHandoffMs - (Date.now() - startedAt);
650
- let timeout;
646
+ let cancelTimeout;
651
647
  let result;
652
648
  try {
653
649
  result = await Promise.race([
654
650
  execution,
655
651
  new Promise((resolve) => {
656
- timeout = setTimeout(() => resolve(handoff), Math.max(remaining, 0));
652
+ cancelTimeout = this.timer.schedule(() => resolve(handoff), remaining);
657
653
  }),
658
654
  ]);
659
655
  }
660
656
  finally {
661
- if (timeout !== undefined) {
662
- clearTimeout(timeout);
663
- }
657
+ cancelTimeout?.();
664
658
  }
665
659
  if (result === handoff) {
666
660
  await this.armForHandoff(Date.now());
@@ -690,7 +684,10 @@ export class Engine {
690
684
  /**
691
685
  * Registers the execution in its active map synchronously, then claims and
692
686
  * runs one attempt in the background. The returned promise settles when the
693
- * attempt's outcome has been persisted, not when a timed-out handler settles.
687
+ * attempt's outcome has been persisted. The key leaves the active map and
688
+ * its permit is released then too, whether or not a timed-out handler has
689
+ * settled: nothing can stop a handler, and one that never settles would
690
+ * otherwise keep its retry from ever starting.
694
691
  */
695
692
  startExecution(spec, eagerPermit) {
696
693
  const { key, row, policy } = spec;
@@ -706,15 +703,6 @@ export class Engine {
706
703
  spec.active.set(key, activeExecution);
707
704
  this.trackActive(spec.table.name, activeKey(spec.table.channel, key));
708
705
  let releasePermit = eagerPermit;
709
- let handlerStarted = false;
710
- let handlerSettled = false;
711
- let policySettled = false;
712
- const releaseIfSettled = () => {
713
- if (policySettled && (!handlerStarted || handlerSettled)) {
714
- releasePermit?.();
715
- settleActive();
716
- }
717
- };
718
706
  const execution = (async () => {
719
707
  releasePermit ?? (releasePermit = await this.acquirePermit());
720
708
  const attempt = claimAttempt(this.sql, spec.table, key, row.generation_id, policy.maxAttempts);
@@ -731,18 +719,13 @@ export class Engine {
731
719
  });
732
720
  const controller = new AbortController();
733
721
  activeExecution.controller = controller;
734
- let timeout;
722
+ let cancelTimeout;
735
723
  try {
736
- handlerStarted = true;
737
724
  const handlerResult = Promise.resolve().then(() => spec.invoke(attempt, controller.signal));
738
- void handlerResult
739
- .finally(() => {
740
- handlerSettled = true;
741
- releaseIfSettled();
742
- })
743
- .catch(() => undefined);
725
+ // After a timeout nothing awaits the handler, which may still reject.
726
+ void handlerResult.catch(() => undefined);
744
727
  const timeoutResult = new Promise((_resolve, reject) => {
745
- timeout = setTimeout(() => {
728
+ cancelTimeout = this.timer.schedule(() => {
746
729
  const error = spec.timeoutError();
747
730
  controller.abort(error);
748
731
  reject(error);
@@ -774,13 +757,11 @@ export class Engine {
774
757
  await this.settleFailure(spec, attempt, startedAt, caught);
775
758
  }
776
759
  finally {
777
- if (timeout !== undefined) {
778
- clearTimeout(timeout);
779
- }
760
+ cancelTimeout?.();
780
761
  }
781
762
  })().finally(() => {
782
- policySettled = true;
783
- releaseIfSettled();
763
+ releasePermit?.();
764
+ settleActive();
784
765
  });
785
766
  void settled.finally(() => {
786
767
  if (spec.active.get(key) === activeExecution) {
@@ -911,6 +892,23 @@ export class DurabilityScheduler {
911
892
  alarm(info) {
912
893
  return engineFor({ scheduler: this }).alarm(info);
913
894
  }
895
+ /**
896
+ * Runs `callback` once after `delayMs`, like the global `setTimeout`, and
897
+ * returns a function that cancels it. The timeout shares one native timer
898
+ * with every helper on the object, so any number of them can be pending
899
+ * without reaching the runtime's limit of 10,000 native timers.
900
+ *
901
+ * The timeout exists only in memory and an eviction drops it; schedule work
902
+ * that must survive one with `DurabilityAlarms`. Callbacks due at the same
903
+ * time run one after another in the same task, and one that throws is
904
+ * reported as `durability.timer.callback_failed` instead of being thrown.
905
+ */
906
+ setTimeout(callback, delayMs) {
907
+ if (typeof callback !== 'function') {
908
+ throw new TypeError('callback must be a function');
909
+ }
910
+ return engineFor({ scheduler: this }).timer.schedule(callback, delayMs);
911
+ }
914
912
  }
915
913
  traceMethods(DurabilityScheduler.prototype, 'DurabilityScheduler');
916
914
  /** Resolves the engine a helper attaches to: the object's one scheduler. */
@@ -903,20 +903,7 @@ async function _DurabilitySemaphoreQueue_notify() {
903
903
  reportFailure('durability.semaphore_queue.retry_policy_failed', {}, error);
904
904
  }
905
905
  return 60000;
906
- }, _DurabilitySemaphoreQueue_deadline = async function _DurabilitySemaphoreQueue_deadline(what, run) {
907
- let timer;
908
- try {
909
- return await Promise.race([
910
- Promise.resolve().then(run),
911
- new Promise((_resolve, reject) => {
912
- timer = setTimeout(() => reject(new Error(`Semaphore ${what} timed out after ${__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f")}ms`)), __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f"));
913
- }),
914
- ]);
915
- }
916
- finally {
917
- if (timer !== undefined) {
918
- clearTimeout(timer);
919
- }
920
- }
906
+ }, _DurabilitySemaphoreQueue_deadline = function _DurabilitySemaphoreQueue_deadline(what, run) {
907
+ return __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_engine, "f").timer.withTimeout(Promise.resolve().then(run), __classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f"), () => new Error(`Semaphore ${what} timed out after ${__classPrivateFieldGet(this, _DurabilitySemaphoreQueue_attemptTimeoutMs, "f")}ms`));
921
908
  };
922
909
  traceMethods(DurabilitySemaphoreQueue.prototype, 'DurabilitySemaphoreQueue');
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _DurabilitySemaphoreServer_instances, _DurabilitySemaphoreServer_engine, _DurabilitySemaphoreServer_store, _DurabilitySemaphoreServer_limit, _DurabilitySemaphoreServer_groupLimit, _DurabilitySemaphoreServer_canGrant, _DurabilitySemaphoreServer_roundWaitMs, _DurabilitySemaphoreServer_roundPeriodMs, _DurabilitySemaphoreServer_grantTimeoutMs, _DurabilitySemaphoreServer_leases, _DurabilitySemaphoreServer_emit, _DurabilitySemaphoreServer_pending, _DurabilitySemaphoreServer_roundTimer, _DurabilitySemaphoreServer_round, _DurabilitySemaphoreServer_lastRoundAt, _DurabilitySemaphoreServer_contended, _DurabilitySemaphoreServer_grantRoom, _DurabilitySemaphoreServer_scheduleRound, _DurabilitySemaphoreServer_runRound, _DurabilitySemaphoreServer_allocate, _DurabilitySemaphoreServer_sync, _DurabilitySemaphoreServer_applyReports, _DurabilitySemaphoreServer_reclaim, _DurabilitySemaphoreServer_reclaimExpired, _DurabilitySemaphoreServer_resolveLimit, _DurabilitySemaphoreServer_groupPolicy;
12
+ var _DurabilitySemaphoreServer_instances, _DurabilitySemaphoreServer_engine, _DurabilitySemaphoreServer_store, _DurabilitySemaphoreServer_limit, _DurabilitySemaphoreServer_groupLimit, _DurabilitySemaphoreServer_canGrant, _DurabilitySemaphoreServer_roundWaitMs, _DurabilitySemaphoreServer_roundPeriodMs, _DurabilitySemaphoreServer_grantTimeoutMs, _DurabilitySemaphoreServer_leases, _DurabilitySemaphoreServer_emit, _DurabilitySemaphoreServer_pending, _DurabilitySemaphoreServer_roundScheduled, _DurabilitySemaphoreServer_round, _DurabilitySemaphoreServer_lastRoundAt, _DurabilitySemaphoreServer_contended, _DurabilitySemaphoreServer_grantRoom, _DurabilitySemaphoreServer_scheduleRound, _DurabilitySemaphoreServer_runRound, _DurabilitySemaphoreServer_allocate, _DurabilitySemaphoreServer_sync, _DurabilitySemaphoreServer_applyReports, _DurabilitySemaphoreServer_reclaim, _DurabilitySemaphoreServer_reclaimExpired, _DurabilitySemaphoreServer_resolveLimit, _DurabilitySemaphoreServer_groupPolicy;
13
13
  import * as v from 'valibot';
14
14
  import { traceLifecycle, traceMethods } from '../dev.js';
15
15
  import { SemaphoreProtocolError, reportFailure } from '../errors.js';
@@ -86,7 +86,7 @@ export class DurabilitySemaphoreServer {
86
86
  _DurabilitySemaphoreServer_leases.set(this, void 0);
87
87
  _DurabilitySemaphoreServer_emit.set(this, void 0);
88
88
  _DurabilitySemaphoreServer_pending.set(this, []);
89
- _DurabilitySemaphoreServer_roundTimer.set(this, void 0);
89
+ _DurabilitySemaphoreServer_roundScheduled.set(this, false);
90
90
  _DurabilitySemaphoreServer_round.set(this, void 0);
91
91
  _DurabilitySemaphoreServer_lastRoundAt.set(this, -Infinity);
92
92
  // From the last round; tells clients in later syncs to return idle lease headroom.
@@ -191,20 +191,21 @@ export class DurabilitySemaphoreServer {
191
191
  };
192
192
  }
193
193
  }
194
- _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_store = new WeakMap(), _DurabilitySemaphoreServer_limit = new WeakMap(), _DurabilitySemaphoreServer_groupLimit = new WeakMap(), _DurabilitySemaphoreServer_canGrant = new WeakMap(), _DurabilitySemaphoreServer_roundWaitMs = new WeakMap(), _DurabilitySemaphoreServer_roundPeriodMs = new WeakMap(), _DurabilitySemaphoreServer_grantTimeoutMs = new WeakMap(), _DurabilitySemaphoreServer_leases = new WeakMap(), _DurabilitySemaphoreServer_emit = new WeakMap(), _DurabilitySemaphoreServer_pending = new WeakMap(), _DurabilitySemaphoreServer_roundTimer = new WeakMap(), _DurabilitySemaphoreServer_round = new WeakMap(), _DurabilitySemaphoreServer_lastRoundAt = new WeakMap(), _DurabilitySemaphoreServer_contended = new WeakMap(), _DurabilitySemaphoreServer_instances = new WeakSet(), _DurabilitySemaphoreServer_grantRoom = function _DurabilitySemaphoreServer_grantRoom(limit, granted, leased) {
194
+ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_store = new WeakMap(), _DurabilitySemaphoreServer_limit = new WeakMap(), _DurabilitySemaphoreServer_groupLimit = new WeakMap(), _DurabilitySemaphoreServer_canGrant = new WeakMap(), _DurabilitySemaphoreServer_roundWaitMs = new WeakMap(), _DurabilitySemaphoreServer_roundPeriodMs = new WeakMap(), _DurabilitySemaphoreServer_grantTimeoutMs = new WeakMap(), _DurabilitySemaphoreServer_leases = new WeakMap(), _DurabilitySemaphoreServer_emit = new WeakMap(), _DurabilitySemaphoreServer_pending = new WeakMap(), _DurabilitySemaphoreServer_roundScheduled = new WeakMap(), _DurabilitySemaphoreServer_round = new WeakMap(), _DurabilitySemaphoreServer_lastRoundAt = new WeakMap(), _DurabilitySemaphoreServer_contended = new WeakMap(), _DurabilitySemaphoreServer_instances = new WeakSet(), _DurabilitySemaphoreServer_grantRoom = function _DurabilitySemaphoreServer_grantRoom(limit, granted, leased) {
195
195
  if (!__classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f")) {
196
196
  return Math.max(0, limit - granted);
197
197
  }
198
198
  const budget = budgets(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f").provisioning, limit);
199
199
  return Math.max(0, budget.grants - granted - (budget.shared ? leased : 0));
200
200
  }, _DurabilitySemaphoreServer_scheduleRound = function _DurabilitySemaphoreServer_scheduleRound() {
201
- if (__classPrivateFieldGet(this, _DurabilitySemaphoreServer_roundTimer, "f") !== undefined || __classPrivateFieldGet(this, _DurabilitySemaphoreServer_round, "f") !== undefined) {
201
+ if (__classPrivateFieldGet(this, _DurabilitySemaphoreServer_roundScheduled, "f") || __classPrivateFieldGet(this, _DurabilitySemaphoreServer_round, "f") !== undefined) {
202
202
  return;
203
203
  }
204
204
  const earliest = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_lastRoundAt, "f") + __classPrivateFieldGet(this, _DurabilitySemaphoreServer_roundPeriodMs, "f");
205
205
  const delay = Math.max(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_roundWaitMs, "f"), earliest - Date.now());
206
- __classPrivateFieldSet(this, _DurabilitySemaphoreServer_roundTimer, setTimeout(() => {
207
- __classPrivateFieldSet(this, _DurabilitySemaphoreServer_roundTimer, undefined, "f");
206
+ __classPrivateFieldSet(this, _DurabilitySemaphoreServer_roundScheduled, true, "f");
207
+ __classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").timer.schedule(() => {
208
+ __classPrivateFieldSet(this, _DurabilitySemaphoreServer_roundScheduled, false, "f");
208
209
  const onError = (error) => reportFailure('durability.semaphore.round_failed', {}, error);
209
210
  __classPrivateFieldSet(this, _DurabilitySemaphoreServer_round, __classPrivateFieldGet(this, _DurabilitySemaphoreServer_instances, "m", _DurabilitySemaphoreServer_runRound).call(this)
210
211
  .catch(onError)
@@ -215,7 +216,7 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
215
216
  }
216
217
  }), "f");
217
218
  __classPrivateFieldGet(this, _DurabilitySemaphoreServer_engine, "f").waitUntil(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_round, "f"), onError);
218
- }, delay), "f");
219
+ }, delay);
219
220
  }, _DurabilitySemaphoreServer_runRound = async function _DurabilitySemaphoreServer_runRound() {
220
221
  const pending = __classPrivateFieldGet(this, _DurabilitySemaphoreServer_pending, "f");
221
222
  __classPrivateFieldSet(this, _DurabilitySemaphoreServer_pending, [], "f");
@@ -308,9 +309,15 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
308
309
  ? budgets(__classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f").provisioning, limit)
309
310
  : { grants: limit, leases: 0, shared: false };
310
311
  let leased = 0;
311
- if (__classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f") && budget.shared) {
312
+ // A group's limit counts its live lease footprint in every mode, retired
313
+ // leases included until their clients confirm. Kept apart from `used`,
314
+ // which the lease pass reads as grants only.
315
+ const leasedByGroup = new Map();
316
+ if (__classPrivateFieldGet(this, _DurabilitySemaphoreServer_leases, "f")) {
312
317
  for (const lease of store.listLeases()) {
313
- leased += leaseFootprint(lease);
318
+ const footprint = leaseFootprint(lease);
319
+ leased += footprint;
320
+ leasedByGroup.set(lease.grp, (leasedByGroup.get(lease.grp) ?? 0) + footprint);
314
321
  }
315
322
  }
316
323
  let remaining = budget.grants - total - (budget.shared ? leased : 0);
@@ -333,7 +340,9 @@ _DurabilitySemaphoreServer_engine = new WeakMap(), _DurabilitySemaphoreServer_st
333
340
  continue;
334
341
  }
335
342
  const groupUsed = used.get(demand.group) ?? 0;
336
- const groupRemaining = policy.limit === undefined ? Infinity : policy.limit - groupUsed;
343
+ const groupRemaining = policy.limit === undefined
344
+ ? Infinity
345
+ : policy.limit - groupUsed - (leasedByGroup.get(demand.group) ?? 0);
337
346
  const give = Math.max(0, Math.min(demand.count, groupRemaining, remaining));
338
347
  if (give > 0) {
339
348
  const grant = {
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Runs callbacks after a delay like `setTimeout`, but keeps every pending
3
+ * callback in a min-heap ordered by due time and sets one native timer for the
4
+ * earliest. workerd makes `setTimeout` throw once 10,000 native timers are
5
+ * pending, so an object whose open timeouts grow with its workload schedules
6
+ * them here instead of setting a native timer for each.
7
+ *
8
+ * Callbacks due together run one after another in the same task, so promise
9
+ * reactions queued by one do not run before the next as they would between
10
+ * separate native timers. A callback scheduled by one of them runs in a later
11
+ * task even with no delay.
12
+ *
13
+ * A native timer belongs to the request or Durable Object that set it, so an
14
+ * instance must not be shared between them.
15
+ */
16
+ export declare class SharedTimer {
17
+ #private;
18
+ /** Callbacks that have neither run nor been cancelled. */
19
+ get pendingCount(): number;
20
+ /** Runs `callback` once after `delayMs` and returns a function that cancels it. */
21
+ schedule(callback: () => void, delayMs: number): () => void;
22
+ /**
23
+ * Settles like `work`, or rejects with the error from `onTimeout` when
24
+ * `work` is still pending after `timeoutMs`.
25
+ */
26
+ withTimeout<T>(work: Promise<T>, timeoutMs: number, onTimeout: () => Error): Promise<T>;
27
+ }
@@ -0,0 +1,191 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
+ if (kind === "m") throw new TypeError("Private method is not writable");
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
+ };
12
+ var _SharedTimer_instances, _SharedTimer_heap, _SharedTimer_scheduledCount, _SharedTimer_nativeTimer, _SharedTimer_setNativeTimer, _SharedTimer_runDue, _SharedTimer_remove, _SharedTimer_place, _SharedTimer_moveUp, _SharedTimer_moveDown;
13
+ import { reportFailure } from './errors.js';
14
+ // setTimeout overflows past 2^31 - 1 ms and fires at once. A longer wait is
15
+ // set for this long, finds nothing due when it fires, and is set again.
16
+ const maxNativeDelayMs = 2 ** 31 - 1;
17
+ const isBefore = (firstEntry, secondEntry) => firstEntry.dueAt < secondEntry.dueAt ||
18
+ (firstEntry.dueAt === secondEntry.dueAt &&
19
+ firstEntry.order < secondEntry.order);
20
+ /**
21
+ * Runs callbacks after a delay like `setTimeout`, but keeps every pending
22
+ * callback in a min-heap ordered by due time and sets one native timer for the
23
+ * earliest. workerd makes `setTimeout` throw once 10,000 native timers are
24
+ * pending, so an object whose open timeouts grow with its workload schedules
25
+ * them here instead of setting a native timer for each.
26
+ *
27
+ * Callbacks due together run one after another in the same task, so promise
28
+ * reactions queued by one do not run before the next as they would between
29
+ * separate native timers. A callback scheduled by one of them runs in a later
30
+ * task even with no delay.
31
+ *
32
+ * A native timer belongs to the request or Durable Object that set it, so an
33
+ * instance must not be shared between them.
34
+ */
35
+ export class SharedTimer {
36
+ constructor() {
37
+ _SharedTimer_instances.add(this);
38
+ _SharedTimer_heap.set(this, []);
39
+ _SharedTimer_scheduledCount.set(this, 0);
40
+ _SharedTimer_nativeTimer.set(this, void 0);
41
+ }
42
+ /** Callbacks that have neither run nor been cancelled. */
43
+ get pendingCount() {
44
+ return __classPrivateFieldGet(this, _SharedTimer_heap, "f").length;
45
+ }
46
+ /** Runs `callback` once after `delayMs` and returns a function that cancels it. */
47
+ schedule(callback, delayMs) {
48
+ var _a, _b;
49
+ const entry = {
50
+ // performance.now() rather than Date.now(): a native timer counts
51
+ // elapsed time, which a change to the wall clock does not affect.
52
+ dueAt: performance.now() + (delayMs > 0 ? delayMs : 0),
53
+ order: (__classPrivateFieldSet(this, _SharedTimer_scheduledCount, (_b = __classPrivateFieldGet(this, _SharedTimer_scheduledCount, "f"), _a = _b++, _b), "f"), _a),
54
+ index: __classPrivateFieldGet(this, _SharedTimer_heap, "f").length,
55
+ callback,
56
+ };
57
+ __classPrivateFieldGet(this, _SharedTimer_heap, "f").push(entry);
58
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_moveUp).call(this, entry);
59
+ // The native timer is also missing if setting it failed in #runDue; the
60
+ // entries left without one get it back here.
61
+ if (entry.index === 0 || __classPrivateFieldGet(this, _SharedTimer_nativeTimer, "f") === undefined) {
62
+ try {
63
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_setNativeTimer).call(this);
64
+ }
65
+ catch (error) {
66
+ // The caller gets the error instead of a cancel function, so the
67
+ // callback must not stay behind and run later.
68
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_remove).call(this, entry);
69
+ throw error;
70
+ }
71
+ }
72
+ return () => {
73
+ entry.callback = undefined;
74
+ if (entry.index === -1) {
75
+ return;
76
+ }
77
+ const wasEarliest = entry.index === 0;
78
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_remove).call(this, entry);
79
+ if (wasEarliest) {
80
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_setNativeTimer).call(this);
81
+ }
82
+ };
83
+ }
84
+ /**
85
+ * Settles like `work`, or rejects with the error from `onTimeout` when
86
+ * `work` is still pending after `timeoutMs`.
87
+ */
88
+ async withTimeout(work, timeoutMs, onTimeout) {
89
+ let cancelTimeout;
90
+ try {
91
+ return await Promise.race([
92
+ work,
93
+ new Promise((_resolve, reject) => {
94
+ cancelTimeout = this.schedule(() => reject(onTimeout()), timeoutMs);
95
+ }),
96
+ ]);
97
+ }
98
+ finally {
99
+ cancelTimeout?.();
100
+ }
101
+ }
102
+ }
103
+ _SharedTimer_heap = new WeakMap(), _SharedTimer_scheduledCount = new WeakMap(), _SharedTimer_nativeTimer = new WeakMap(), _SharedTimer_instances = new WeakSet(), _SharedTimer_setNativeTimer = function _SharedTimer_setNativeTimer() {
104
+ clearTimeout(__classPrivateFieldGet(this, _SharedTimer_nativeTimer, "f"));
105
+ __classPrivateFieldSet(this, _SharedTimer_nativeTimer, undefined, "f");
106
+ const earliest = __classPrivateFieldGet(this, _SharedTimer_heap, "f")[0];
107
+ if (earliest === undefined) {
108
+ return;
109
+ }
110
+ const now = performance.now();
111
+ const wakeAt = Math.min(earliest.dueAt, now + maxNativeDelayMs);
112
+ __classPrivateFieldSet(this, _SharedTimer_nativeTimer, setTimeout(() => __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_runDue).call(this, wakeAt), Math.max(wakeAt - now, 0)), "f");
113
+ }, _SharedTimer_runDue = function _SharedTimer_runDue(wakeAt) {
114
+ // setTimeout drops the fraction of a delay, so it can fire just before the
115
+ // clock reaches `wakeAt`. Counting that as reached runs the callback now
116
+ // instead of setting another timer for the remaining fraction.
117
+ const now = Math.max(performance.now(), wakeAt);
118
+ const due = [];
119
+ let earliest = __classPrivateFieldGet(this, _SharedTimer_heap, "f")[0];
120
+ while (earliest !== undefined && earliest.dueAt <= now) {
121
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_remove).call(this, earliest);
122
+ due.push(earliest);
123
+ earliest = __classPrivateFieldGet(this, _SharedTimer_heap, "f")[0];
124
+ }
125
+ // Set before the callbacks run, and by clearing the native timer that is
126
+ // firing: workerd counts it as pending until this function returns, and a
127
+ // callback could take the last one the runtime allows. Clearing one and
128
+ // setting one cannot fail at that limit, so the entries still in the heap
129
+ // always keep a native timer.
130
+ try {
131
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_setNativeTimer).call(this);
132
+ }
133
+ catch (error) {
134
+ reportFailure('durability.timer.set_failed', { pendingCount: __classPrivateFieldGet(this, _SharedTimer_heap, "f").length }, error);
135
+ }
136
+ for (const entry of due) {
137
+ try {
138
+ entry.callback?.();
139
+ }
140
+ catch (error) {
141
+ // A throw must not keep the callbacks behind it from running.
142
+ reportFailure('durability.timer.callback_failed', { dueAt: entry.dueAt, scheduledOrder: entry.order }, error);
143
+ }
144
+ }
145
+ }, _SharedTimer_remove = function _SharedTimer_remove(entry) {
146
+ const { index } = entry;
147
+ entry.index = -1;
148
+ const last = __classPrivateFieldGet(this, _SharedTimer_heap, "f").pop();
149
+ if (last === undefined || last === entry) {
150
+ return;
151
+ }
152
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_place).call(this, last, index);
153
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_moveUp).call(this, last);
154
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_moveDown).call(this, last);
155
+ }, _SharedTimer_place = function _SharedTimer_place(entry, index) {
156
+ __classPrivateFieldGet(this, _SharedTimer_heap, "f")[index] = entry;
157
+ entry.index = index;
158
+ }, _SharedTimer_moveUp = function _SharedTimer_moveUp(entry) {
159
+ let { index } = entry;
160
+ while (index > 0) {
161
+ const parentIndex = (index - 1) >> 1;
162
+ const parent = __classPrivateFieldGet(this, _SharedTimer_heap, "f")[parentIndex];
163
+ if (parent === undefined || !isBefore(entry, parent)) {
164
+ break;
165
+ }
166
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_place).call(this, parent, index);
167
+ index = parentIndex;
168
+ }
169
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_place).call(this, entry, index);
170
+ }, _SharedTimer_moveDown = function _SharedTimer_moveDown(entry) {
171
+ let { index } = entry;
172
+ for (;;) {
173
+ const leftIndex = 2 * index + 1;
174
+ const left = __classPrivateFieldGet(this, _SharedTimer_heap, "f")[leftIndex];
175
+ if (left === undefined) {
176
+ break;
177
+ }
178
+ const right = __classPrivateFieldGet(this, _SharedTimer_heap, "f")[leftIndex + 1];
179
+ let child = left;
180
+ if (right !== undefined && isBefore(right, left)) {
181
+ child = right;
182
+ }
183
+ if (!isBefore(child, entry)) {
184
+ break;
185
+ }
186
+ const childIndex = child.index;
187
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_place).call(this, child, index);
188
+ index = childIndex;
189
+ }
190
+ __classPrivateFieldGet(this, _SharedTimer_instances, "m", _SharedTimer_place).call(this, entry, index);
191
+ };
@@ -9,9 +9,10 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _Connection_instances, _Connection_config, _Connection_url, _Connection_transforms, _Connection_pending, _Connection_handlers, _Connection_statusHandlers, _Connection_resyncHandlers, _Connection_subscribes, _Connection_acknowledged, _Connection_status, _Connection_ws, _Connection_generation, _Connection_nextId, _Connection_lastSeq, _Connection_attempt, _Connection_retryTimer, _Connection_pingTimer, _Connection_lastReceivedAt, _Connection_send, _Connection_subscribe, _Connection_subscribed, _Connection_flush, _Connection_connect, _Connection_receive, _Connection_settle, _Connection_dispatch, _Connection_invoke, _Connection_disconnected, _Connection_abandon, _Connection_startPing, _Connection_stopTimers, _Connection_setStatus, _DurabilitySocketClient_config, _DurabilitySocketClient_transforms;
12
+ var _Connection_instances, _Connection_config, _Connection_url, _Connection_transforms, _Connection_pending, _Connection_handlers, _Connection_statusHandlers, _Connection_resyncHandlers, _Connection_subscribes, _Connection_acknowledged, _Connection_status, _Connection_ws, _Connection_generation, _Connection_nextId, _Connection_lastSeq, _Connection_attempt, _Connection_timer, _Connection_cancelRetry, _Connection_cancelPing, _Connection_lastReceivedAt, _Connection_send, _Connection_subscribe, _Connection_subscribed, _Connection_flush, _Connection_connect, _Connection_receive, _Connection_settle, _Connection_dispatch, _Connection_invoke, _Connection_disconnected, _Connection_abandon, _Connection_startPing, _Connection_stopTimers, _Connection_setStatus, _DurabilitySocketClient_config, _DurabilitySocketClient_transforms;
13
13
  import { reportFailure } from '../errors.js';
14
14
  import { parseServerFrame, socketProtocolErrorCode, socketProtocolVersion, } from './protocol.js';
15
+ import { SharedTimer } from '../shared-timer.js';
15
16
  import { exponential, jitter } from '../utils.js';
16
17
  /** Rejection for calls still waiting when `close()` was called. */
17
18
  export class SocketClosedError extends Error {
@@ -71,8 +72,10 @@ class Connection {
71
72
  _Connection_nextId.set(this, 0);
72
73
  _Connection_lastSeq.set(this, void 0);
73
74
  _Connection_attempt.set(this, 0);
74
- _Connection_retryTimer.set(this, void 0);
75
- _Connection_pingTimer.set(this, void 0);
75
+ // Every pending call holds a timeout, and nothing limits how many are pending.
76
+ _Connection_timer.set(this, new SharedTimer());
77
+ _Connection_cancelRetry.set(this, void 0);
78
+ _Connection_cancelPing.set(this, void 0);
76
79
  _Connection_lastReceivedAt.set(this, 0);
77
80
  __classPrivateFieldSet(this, _Connection_config, config, "f");
78
81
  __classPrivateFieldSet(this, _Connection_url, config.url(id), "f");
@@ -132,7 +135,7 @@ class Connection {
132
135
  __classPrivateFieldGet(this, _Connection_instances, "m", _Connection_stopTimers).call(this);
133
136
  __classPrivateFieldSet(this, _Connection_generation, __classPrivateFieldGet(this, _Connection_generation, "f") + 1, "f");
134
137
  for (const pending of __classPrivateFieldGet(this, _Connection_pending, "f").values()) {
135
- clearTimeout(pending.timer);
138
+ pending.cancelTimeout();
136
139
  pending.reject(new SocketClosedError());
137
140
  }
138
141
  __classPrivateFieldGet(this, _Connection_pending, "f").clear();
@@ -143,7 +146,7 @@ class Connection {
143
146
  __classPrivateFieldSet(this, _Connection_ws, undefined, "f");
144
147
  }
145
148
  }
146
- _Connection_config = new WeakMap(), _Connection_url = new WeakMap(), _Connection_transforms = new WeakMap(), _Connection_pending = new WeakMap(), _Connection_handlers = new WeakMap(), _Connection_statusHandlers = new WeakMap(), _Connection_resyncHandlers = new WeakMap(), _Connection_subscribes = new WeakMap(), _Connection_acknowledged = new WeakMap(), _Connection_status = new WeakMap(), _Connection_ws = new WeakMap(), _Connection_generation = new WeakMap(), _Connection_nextId = new WeakMap(), _Connection_lastSeq = new WeakMap(), _Connection_attempt = new WeakMap(), _Connection_retryTimer = new WeakMap(), _Connection_pingTimer = new WeakMap(), _Connection_lastReceivedAt = new WeakMap(), _Connection_instances = new WeakSet(), _Connection_send = function _Connection_send(method, input) {
149
+ _Connection_config = new WeakMap(), _Connection_url = new WeakMap(), _Connection_transforms = new WeakMap(), _Connection_pending = new WeakMap(), _Connection_handlers = new WeakMap(), _Connection_statusHandlers = new WeakMap(), _Connection_resyncHandlers = new WeakMap(), _Connection_subscribes = new WeakMap(), _Connection_acknowledged = new WeakMap(), _Connection_status = new WeakMap(), _Connection_ws = new WeakMap(), _Connection_generation = new WeakMap(), _Connection_nextId = new WeakMap(), _Connection_lastSeq = new WeakMap(), _Connection_attempt = new WeakMap(), _Connection_timer = new WeakMap(), _Connection_cancelRetry = new WeakMap(), _Connection_cancelPing = new WeakMap(), _Connection_lastReceivedAt = new WeakMap(), _Connection_instances = new WeakSet(), _Connection_send = function _Connection_send(method, input) {
147
150
  var _a;
148
151
  if (__classPrivateFieldGet(this, _Connection_status, "f") === 'closed') {
149
152
  return Promise.reject(new SocketClosedError());
@@ -163,7 +166,7 @@ _Connection_config = new WeakMap(), _Connection_url = new WeakMap(), _Connection
163
166
  sent: false,
164
167
  resolve,
165
168
  reject,
166
- timer: setTimeout(() => {
169
+ cancelTimeout: __classPrivateFieldGet(this, _Connection_timer, "f").schedule(() => {
167
170
  __classPrivateFieldGet(this, _Connection_pending, "f").delete(id);
168
171
  reject(new SocketTimeoutError(method, __classPrivateFieldGet(this, _Connection_config, "f").timeoutMs));
169
172
  }, __classPrivateFieldGet(this, _Connection_config, "f").timeoutMs),
@@ -215,7 +218,7 @@ _Connection_config = new WeakMap(), _Connection_url = new WeakMap(), _Connection
215
218
  }
216
219
  }, _Connection_connect = function _Connection_connect() {
217
220
  var _a;
218
- if (__classPrivateFieldGet(this, _Connection_status, "f") !== 'disconnected' || __classPrivateFieldGet(this, _Connection_retryTimer, "f") !== undefined) {
221
+ if (__classPrivateFieldGet(this, _Connection_status, "f") !== 'disconnected' || __classPrivateFieldGet(this, _Connection_cancelRetry, "f") !== undefined) {
219
222
  return;
220
223
  }
221
224
  __classPrivateFieldGet(this, _Connection_instances, "m", _Connection_setStatus).call(this, 'connecting');
@@ -288,7 +291,7 @@ _Connection_config = new WeakMap(), _Connection_url = new WeakMap(), _Connection
288
291
  return;
289
292
  }
290
293
  __classPrivateFieldGet(this, _Connection_pending, "f").delete(frame.id);
291
- clearTimeout(pending.timer);
294
+ pending.cancelTimeout();
292
295
  settle(pending);
293
296
  }, _Connection_dispatch = function _Connection_dispatch(name, payload) {
294
297
  for (const handler of __classPrivateFieldGet(this, _Connection_handlers, "f").get(name) ?? []) {
@@ -310,7 +313,7 @@ _Connection_config = new WeakMap(), _Connection_url = new WeakMap(), _Connection
310
313
  continue;
311
314
  }
312
315
  __classPrivateFieldGet(this, _Connection_pending, "f").delete(id);
313
- clearTimeout(pending.timer);
316
+ pending.cancelTimeout();
314
317
  pending.reject(new SocketConnectionLostError(pending.method));
315
318
  }
316
319
  if (code === socketProtocolErrorCode) {
@@ -319,36 +322,36 @@ _Connection_config = new WeakMap(), _Connection_url = new WeakMap(), _Connection
319
322
  }
320
323
  __classPrivateFieldGet(this, _Connection_instances, "m", _Connection_setStatus).call(this, 'disconnected');
321
324
  __classPrivateFieldSet(this, _Connection_attempt, __classPrivateFieldGet(this, _Connection_attempt, "f") + 1, "f");
322
- __classPrivateFieldSet(this, _Connection_retryTimer, setTimeout(() => {
323
- __classPrivateFieldSet(this, _Connection_retryTimer, undefined, "f");
325
+ __classPrivateFieldSet(this, _Connection_cancelRetry, __classPrivateFieldGet(this, _Connection_timer, "f").schedule(() => {
326
+ __classPrivateFieldSet(this, _Connection_cancelRetry, undefined, "f");
324
327
  __classPrivateFieldGet(this, _Connection_instances, "m", _Connection_connect).call(this);
325
328
  }, jitter(exponential(__classPrivateFieldGet(this, _Connection_attempt, "f"), 250, 60000))), "f");
326
329
  }, _Connection_abandon = function _Connection_abandon(error) {
327
330
  reportFailure('durability.socket.protocol_error', {}, error);
328
331
  for (const pending of __classPrivateFieldGet(this, _Connection_pending, "f").values()) {
329
- clearTimeout(pending.timer);
332
+ pending.cancelTimeout();
330
333
  pending.reject(error);
331
334
  }
332
335
  __classPrivateFieldGet(this, _Connection_pending, "f").clear();
333
336
  this.close();
334
337
  }, _Connection_startPing = function _Connection_startPing() {
335
338
  const interval = __classPrivateFieldGet(this, _Connection_config, "f").pingIntervalMs;
336
- __classPrivateFieldSet(this, _Connection_pingTimer, setInterval(() => {
339
+ const ping = () => {
340
+ // Scheduled before the socket is used, so a close that stops the timers
341
+ // also stops the next ping.
342
+ __classPrivateFieldSet(this, _Connection_cancelPing, __classPrivateFieldGet(this, _Connection_timer, "f").schedule(ping, interval), "f");
337
343
  if (Date.now() - __classPrivateFieldGet(this, _Connection_lastReceivedAt, "f") > interval * 2) {
338
344
  __classPrivateFieldGet(this, _Connection_ws, "f")?.close(4000, 'Keep-alive timeout');
339
345
  return;
340
346
  }
341
347
  __classPrivateFieldGet(this, _Connection_ws, "f")?.send(pingText);
342
- }, interval), "f");
348
+ };
349
+ __classPrivateFieldSet(this, _Connection_cancelPing, __classPrivateFieldGet(this, _Connection_timer, "f").schedule(ping, interval), "f");
343
350
  }, _Connection_stopTimers = function _Connection_stopTimers() {
344
- if (__classPrivateFieldGet(this, _Connection_pingTimer, "f") !== undefined) {
345
- clearInterval(__classPrivateFieldGet(this, _Connection_pingTimer, "f"));
346
- __classPrivateFieldSet(this, _Connection_pingTimer, undefined, "f");
347
- }
348
- if (__classPrivateFieldGet(this, _Connection_retryTimer, "f") !== undefined) {
349
- clearTimeout(__classPrivateFieldGet(this, _Connection_retryTimer, "f"));
350
- __classPrivateFieldSet(this, _Connection_retryTimer, undefined, "f");
351
- }
351
+ __classPrivateFieldGet(this, _Connection_cancelPing, "f")?.call(this);
352
+ __classPrivateFieldSet(this, _Connection_cancelPing, undefined, "f");
353
+ __classPrivateFieldGet(this, _Connection_cancelRetry, "f")?.call(this);
354
+ __classPrivateFieldSet(this, _Connection_cancelRetry, undefined, "f");
352
355
  }, _Connection_setStatus = function _Connection_setStatus(status) {
353
356
  if (__classPrivateFieldGet(this, _Connection_status, "f") === status) {
354
357
  return;
@@ -74,32 +74,46 @@ _TopologyClient_config = new WeakMap(), _TopologyClient_readTimeoutMs = new Weak
74
74
  const previous = __classPrivateFieldGet(this, _TopologyClient_cache, "f").get(group);
75
75
  const result = await __classPrivateFieldGet(this, _TopologyClient_instances, "m", _TopologyClient_read).call(this, group);
76
76
  const now = Date.now();
77
+ let failure;
77
78
  if ('snapshot' in result) {
78
79
  const { snapshot } = result;
79
80
  // The store is eventually consistent; a lagging replica must not move readers backwards.
80
81
  // Versions only compare within one coordinator: a group moved to another starts over.
81
- const keep = previous?.view !== undefined &&
82
- previous.view.coordinator === snapshot.coordinator &&
83
- previous.view.version > snapshot.version;
84
- const view = keep
85
- ? previous.view
86
- : { ...snapshot, source: 'published', fetchedAt: now };
87
- const expiresAt = now + jittered(__classPrivateFieldGet(this, _TopologyClient_ttlMs, "f"));
88
- __classPrivateFieldGet(this, _TopologyClient_instances, "m", _TopologyClient_remember).call(this, group, {
89
- view,
90
- expiresAt,
91
- staleUntil: expiresAt + (__classPrivateFieldGet(this, _TopologyClient_config, "f").staleIfErrorMs ?? 0),
92
- });
93
- return view;
82
+ const outdated = previous?.newestSeen !== undefined &&
83
+ previous.newestSeen.coordinator === snapshot.coordinator &&
84
+ previous.newestSeen.version > snapshot.version;
85
+ if (!outdated || previous.view !== undefined) {
86
+ const view = outdated
87
+ ? previous.view
88
+ : { ...snapshot, source: 'published', fetchedAt: now };
89
+ const expiresAt = now + jittered(__classPrivateFieldGet(this, _TopologyClient_ttlMs, "f"));
90
+ __classPrivateFieldGet(this, _TopologyClient_instances, "m", _TopologyClient_remember).call(this, group, {
91
+ view,
92
+ newestSeen: {
93
+ coordinator: view.coordinator,
94
+ version: view.version,
95
+ },
96
+ expiresAt,
97
+ staleUntil: expiresAt + (__classPrivateFieldGet(this, _TopologyClient_config, "f").staleIfErrorMs ?? 0),
98
+ });
99
+ return view;
100
+ }
101
+ // Only a failed read leaves a newer version seen without a view, so the
102
+ // older snapshot answers the same way that read did.
103
+ failure = previous.failure;
104
+ }
105
+ else {
106
+ failure = result.failure;
94
107
  }
95
108
  const entry = {
96
109
  // A missing group is not an error, so nothing stale may be served for it.
97
- ...(result.failure.reason === 'missing' || !previous?.view
110
+ ...(failure.reason === 'missing' || !previous?.view
98
111
  ? {}
99
112
  : { view: previous.view }),
113
+ ...(previous?.newestSeen ? { newestSeen: previous.newestSeen } : {}),
100
114
  staleUntil: previous?.staleUntil ?? 0,
101
115
  expiresAt: now + jittered(__classPrivateFieldGet(this, _TopologyClient_negativeTtlMs, "f")),
102
- failure: result.failure,
116
+ failure,
103
117
  };
104
118
  __classPrivateFieldGet(this, _TopologyClient_instances, "m", _TopologyClient_remember).call(this, group, entry);
105
119
  return __classPrivateFieldGet(this, _TopologyClient_instances, "m", _TopologyClient_fallback).call(this, group, entry);
@@ -45,20 +45,6 @@ class TopologyTimeoutError extends Error {
45
45
  this.name = 'TopologyTimeoutError';
46
46
  }
47
47
  }
48
- const withTimeout = async (work, timeoutMs, label) => {
49
- let timer;
50
- try {
51
- return await Promise.race([
52
- work,
53
- new Promise((_resolve, reject) => {
54
- timer = setTimeout(() => reject(new TopologyTimeoutError(label, timeoutMs)), timeoutMs);
55
- }),
56
- ]);
57
- }
58
- finally {
59
- clearTimeout(timer);
60
- }
61
- };
62
48
  // setTimeout overflows past this, and the outbox enforces the same ceiling.
63
49
  const maxTimeoutMs = 2 ** 31 - 1;
64
50
  const timeoutOf = (options, fallback) => {
@@ -335,7 +321,7 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
335
321
  ? publisher.delete(publication.deleted.group, publication.deleted.version)
336
322
  : publisher.write(publication.snapshot, encodeSnapshot(publication.snapshot)));
337
323
  try {
338
- await withTimeout(write, timeoutMs, 'publish');
324
+ await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").timer.withTimeout(write, timeoutMs, () => new TopologyTimeoutError('publish', timeoutMs));
339
325
  }
340
326
  catch (error) {
341
327
  if (error instanceof TopologyTimeoutError) {
@@ -394,7 +380,7 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
394
380
  };
395
381
  try {
396
382
  // Members ignore versions they have passed, so an abandoned delivery landing late is harmless.
397
- await withTimeout(Promise.resolve().then(() => channel.deliver(member, update)), timeoutMs, 'delivery');
383
+ await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").timer.withTimeout(Promise.resolve().then(() => channel.deliver(member, update)), timeoutMs, () => new TopologyTimeoutError('delivery', timeoutMs));
398
384
  }
399
385
  catch (error) {
400
386
  reportFailure('durability.topology.delivery_failed', { group, channel: name, member: member.id, attempt: entry.attempt }, error);
@@ -9,9 +9,10 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _DurabilityTracker_instances, _DurabilityTracker_sources, _DurabilityTracker_collect, _DurabilityTracker_source, _DurabilityTracker_handle, _DurabilityTracker_wait;
12
+ var _DurabilityTracker_instances, _DurabilityTracker_sources, _DurabilityTracker_timer, _DurabilityTracker_collect, _DurabilityTracker_source, _DurabilityTracker_handle, _DurabilityTracker_wait;
13
13
  import * as v from 'valibot';
14
14
  import { traceMethods } from '../dev.js';
15
+ import { SharedTimer } from '../shared-timer.js';
15
16
  import { trackedSourceOf, } from './source.js';
16
17
  const limitSchema = v.pipe(v.number(), v.safeInteger(), v.minValue(1), v.maxValue(100));
17
18
  const cursorSchema = v.strictTuple([
@@ -54,6 +55,8 @@ export class DurabilityTracker {
54
55
  constructor(config) {
55
56
  _DurabilityTracker_instances.add(this);
56
57
  _DurabilityTracker_sources.set(this, void 0);
58
+ // Every wait holds a timeout, and nothing limits how many waits are open.
59
+ _DurabilityTracker_timer.set(this, new SharedTimer());
57
60
  __classPrivateFieldSet(this, _DurabilityTracker_sources, Object.entries(config.sources).map(([key, input]) => [
58
61
  key,
59
62
  trackedSourceOf(input) ?? input,
@@ -86,7 +89,7 @@ export class DurabilityTracker {
86
89
  return __classPrivateFieldGet(this, _DurabilityTracker_instances, "m", _DurabilityTracker_handle).call(this, resolved, itemFor(key, record));
87
90
  }
88
91
  }
89
- _DurabilityTracker_sources = new WeakMap(), _DurabilityTracker_instances = new WeakSet(), _DurabilityTracker_collect = async function _DurabilityTracker_collect(query) {
92
+ _DurabilityTracker_sources = new WeakMap(), _DurabilityTracker_timer = new WeakMap(), _DurabilityTracker_instances = new WeakSet(), _DurabilityTracker_collect = async function _DurabilityTracker_collect(query) {
90
93
  const limit = v.parse(limitSchema, query.limit ?? 50);
91
94
  const candidates = query.source === undefined ? __classPrivateFieldGet(this, _DurabilityTracker_sources, "f") : [__classPrivateFieldGet(this, _DurabilityTracker_instances, "m", _DurabilityTracker_source).call(this, query.source)];
92
95
  const [start, startCursor] = query.cursor === undefined
@@ -135,13 +138,13 @@ _DurabilityTracker_sources = new WeakMap(), _DurabilityTracker_instances = new W
135
138
  return new Promise((resolve, reject) => {
136
139
  let done = false;
137
140
  let unwatch;
138
- let timer;
141
+ let cancelTimeout;
139
142
  const finish = (settle) => {
140
143
  if (done) {
141
144
  return;
142
145
  }
143
146
  done = true;
144
- clearTimeout(timer);
147
+ cancelTimeout?.();
145
148
  unwatch?.();
146
149
  settle();
147
150
  };
@@ -164,7 +167,7 @@ _DurabilityTracker_sources = new WeakMap(), _DurabilityTracker_instances = new W
164
167
  unwatch?.();
165
168
  return;
166
169
  }
167
- timer = setTimeout(() => {
170
+ cancelTimeout = __classPrivateFieldGet(this, _DurabilityTracker_timer, "f").schedule(() => {
168
171
  read().then((result) => finish(() => resolve(result)), fail);
169
172
  }, timeoutMs);
170
173
  check();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "durability",
3
- "version": "5.2.1",
3
+ "version": "5.3.0",
4
4
  "description": "Alarm-backed durable operations and typed WebSocket sync for Cloudflare Durable Objects",
5
5
  "type": "module",
6
6
  "license": "ISC",
@@ -65,7 +65,7 @@
65
65
  "@standard-schema/spec": "^1.1.0",
66
66
  "valibot": "^1.5.0",
67
67
  "@durability/storage": "^0.1.1",
68
- "@durability/transforms": "^0.6.1"
68
+ "@durability/transforms": "^0.6.2"
69
69
  },
70
70
  "peerDependencies": {
71
71
  "@cloudflare/workers-types": "^4.20260702.1"