@yaks/durable-object 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +554 -2
- package/_dist/alarm.d.ts +35 -0
- package/_dist/alarm.d.ts.map +1 -0
- package/_dist/ceiling.d.ts +167 -0
- package/_dist/ceiling.d.ts.map +1 -0
- package/_dist/compose.d.ts +79 -0
- package/_dist/compose.d.ts.map +1 -0
- package/_dist/copy.d.ts +67 -0
- package/_dist/copy.d.ts.map +1 -0
- package/_dist/door.d.ts +172 -0
- package/_dist/door.d.ts.map +1 -0
- package/_dist/job.d.ts +26 -0
- package/_dist/job.d.ts.map +1 -0
- package/_dist/keys.d.ts +14 -0
- package/_dist/keys.d.ts.map +1 -0
- package/_dist/mod.d.ts +65 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/object.d.ts +75 -0
- package/_dist/object.d.ts.map +1 -0
- package/_dist/profile.d.ts +32 -0
- package/_dist/profile.d.ts.map +1 -0
- package/_dist/query-memory.d.ts +27 -0
- package/_dist/query-memory.d.ts.map +1 -0
- package/_dist/resets.d.ts +208 -0
- package/_dist/resets.d.ts.map +1 -0
- package/_dist/rewind.d.ts +21 -0
- package/_dist/rewind.d.ts.map +1 -0
- package/_dist/sockets.d.ts +90 -0
- package/_dist/sockets.d.ts.map +1 -0
- package/_dist/sql.d.ts +72 -0
- package/_dist/sql.d.ts.map +1 -0
- package/_dist/store.d.ts +8 -0
- package/_dist/store.d.ts.map +1 -0
- package/alarm.js +126 -0
- package/alarm.js.map +1 -0
- package/alarm.ts +188 -0
- package/ceiling.js +536 -0
- package/ceiling.js.map +1 -0
- package/ceiling.ts +681 -0
- package/compose.js +201 -0
- package/compose.js.map +1 -0
- package/compose.ts +249 -0
- package/copy.js +394 -0
- package/copy.js.map +1 -0
- package/copy.ts +413 -0
- package/deno.json +1 -0
- package/door.js +364 -0
- package/door.js.map +1 -0
- package/door.ts +542 -0
- package/example.ts +108 -0
- package/holds.js +85 -0
- package/holds.js.map +1 -0
- package/holds.ts +72 -0
- package/job.js +151 -0
- package/job.js.map +1 -0
- package/job.ts +166 -0
- package/keys.js +59 -0
- package/keys.js.map +1 -0
- package/keys.ts +85 -0
- package/mod.js +65 -0
- package/mod.js.map +1 -0
- package/mod.ts +167 -0
- package/object.js +247 -0
- package/object.js.map +1 -0
- package/object.ts +385 -0
- package/package.json +68 -4
- package/profile.js +131 -0
- package/profile.js.map +1 -0
- package/profile.ts +150 -0
- package/query-memory.js +157 -0
- package/query-memory.js.map +1 -0
- package/query-memory.ts +240 -0
- package/resets.js +302 -0
- package/resets.js.map +1 -0
- package/resets.ts +353 -0
- package/rewind.js +68 -0
- package/rewind.js.map +1 -0
- package/rewind.ts +91 -0
- package/sockets.js +749 -0
- package/sockets.js.map +1 -0
- package/sockets.ts +991 -0
- package/sql.js +221 -0
- package/sql.js.map +1 -0
- package/sql.ts +330 -0
- package/store.js +58 -0
- package/store.js.map +1 -0
- package/store.ts +80 -0
- package/watches.js +8 -0
- package/watches.js.map +1 -0
- package/watches.ts +23 -0
package/resets.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
// The words the runtime fails a Durable Object's work with when it resets the
|
|
2
|
+
// object, and which of them are ours to fix.
|
|
3
|
+
//
|
|
4
|
+
// The runtime moves an object to a new instance whenever it likes: a runtime
|
|
5
|
+
// update, another machine, a deploy, a failure inside its own storage or in
|
|
6
|
+
// starting that storage up, a lost connection to it. The instance that was
|
|
7
|
+
// moved fails what it had in flight, and whoever owns that work runs it again:
|
|
8
|
+
// a caller fetches again on a fresh stub, the runtime retries an alarm that
|
|
9
|
+
// failed. Cloudflare documents the move as part of an object's life, so what
|
|
10
|
+
// the moved instance meets is no defect. A live object that meets the words from an object it called has run
|
|
11
|
+
// out of retries, and that is a failure; so is a reset our own code caused: a
|
|
12
|
+
// storage operation over its timeout, or a kill for the CPU or memory limit.
|
|
13
|
+
import { sleep, system } from "@yaks/fp";
|
|
14
|
+
let MOVED = [
|
|
15
|
+
'Durable Object instance is no longer active',
|
|
16
|
+
'Durable Object reset because',
|
|
17
|
+
'Internal error in Durable Object storage',
|
|
18
|
+
'Internal error while starting up Durable Object storage',
|
|
19
|
+
// A connection to the object dropped. The runtime names it in words alone,
|
|
20
|
+
// with no `retryable` flag: a caller meets it when the call it sent is cut
|
|
21
|
+
// off, and an instance whose own storage lost its connection refuses every
|
|
22
|
+
// statement in them until it is replaced.
|
|
23
|
+
'Network connection lost'
|
|
24
|
+
];
|
|
25
|
+
let RESETS = [
|
|
26
|
+
...MOVED,
|
|
27
|
+
'Durable Object storage operation exceeded timeout which caused object to be reset'
|
|
28
|
+
];
|
|
29
|
+
let LIMITS = /exceeded its (CPU time|memory) limit/;
|
|
30
|
+
let STARVED = /\bSQLITE_NOMEM\b/;
|
|
31
|
+
let OPAQUE = /^internal error; reference = [a-z0-9]+$/i;
|
|
32
|
+
let says = (e, words)=>e instanceof Error && words.some((said)=>e.message.includes(said));
|
|
33
|
+
/**
|
|
34
|
+
* The runtime moved this object to a new instance, and the work is run again
|
|
35
|
+
* by whoever owns it.
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* import { assertEquals } from '@std/assert'
|
|
39
|
+
* let gone = new Error('Connection closed: this Durable Object instance is ' +
|
|
40
|
+
* 'no longer active. Reconnect or retry the request.')
|
|
41
|
+
* assertEquals(moved(gone), true)
|
|
42
|
+
* assertEquals(moved(new Error('Durable Object reset because its code was updated.')), true)
|
|
43
|
+
* assertEquals(moved(new Error('Internal error in Durable Object storage ' +
|
|
44
|
+
* 'caused object to be reset; reference = 48q6hau1fptrm6g69pk2lqqh')), true)
|
|
45
|
+
* assertEquals(moved(new Error('Internal error while starting up Durable ' +
|
|
46
|
+
* 'Object storage caused object to be reset; reference = k6485n9h')), true)
|
|
47
|
+
* assertEquals(moved(new Error('Network connection lost.')), true)
|
|
48
|
+
* assertEquals(moved(new Error('Durable Object storage operation exceeded ' +
|
|
49
|
+
* 'timeout which caused object to be reset.')), false)
|
|
50
|
+
* assertEquals(moved(null), false)
|
|
51
|
+
* ```
|
|
52
|
+
*/ export let moved = (e)=>says(e, MOVED);
|
|
53
|
+
/**
|
|
54
|
+
* Whether the runtime has moved the object off the instance this storage
|
|
55
|
+
* belongs to. A moved instance refuses its own storage in the words `moved`
|
|
56
|
+
* knows, a lost connection among them, but not all of it: one reset for a
|
|
57
|
+
* deploy still answers a read and refuses every write (workerd's
|
|
58
|
+
* `ActorSqlite::requireNotBroken`). A transaction counts as a write even when
|
|
59
|
+
* its body only reads, so it asks both; the live instance answers. An
|
|
60
|
+
* instance the runtime killed for its CPU or memory limit is replaced too:
|
|
61
|
+
* its transactions say the kill (`killed`), which whoever met it first
|
|
62
|
+
* reports, and its reads go on failing as a lost connection.
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { assertEquals } from '@std/assert'
|
|
66
|
+
* let reset = () => {
|
|
67
|
+
* throw new Error('Durable Object reset because its code was updated.')
|
|
68
|
+
* }
|
|
69
|
+
* let sql = { exec: () => [] }
|
|
70
|
+
* let live = { sql, transactionSync: <T>(body: () => T) => body() }
|
|
71
|
+
* assertEquals(replaced(live), false)
|
|
72
|
+
* assertEquals(replaced({ sql, transactionSync: reset }), true)
|
|
73
|
+
* assertEquals(replaced({ ...live, sql: { exec: reset } }), true)
|
|
74
|
+
* let cut = () => {
|
|
75
|
+
* throw new Error('Network connection lost.')
|
|
76
|
+
* }
|
|
77
|
+
* assertEquals(replaced({ ...live, sql: { exec: cut } }), true)
|
|
78
|
+
* let kill = () => {
|
|
79
|
+
* throw new Error("Durable Object's isolate exceeded its memory limit and was reset.")
|
|
80
|
+
* }
|
|
81
|
+
* assertEquals(replaced({ sql, transactionSync: kill }), true)
|
|
82
|
+
* ```
|
|
83
|
+
*/ export let replaced = (storage)=>vacated(storage) != null;
|
|
84
|
+
// The runtime's words for the move, as this instance's storage refuses in
|
|
85
|
+
// them, or null while the instance is the live one.
|
|
86
|
+
let vacated = (storage)=>{
|
|
87
|
+
try {
|
|
88
|
+
storage.transactionSync(()=>storage.sql.exec('SELECT 1'));
|
|
89
|
+
return null;
|
|
90
|
+
} catch (e) {
|
|
91
|
+
return moved(e) || killed(e) ? e : null;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
/** How often `vacating` asks whether the instance was replaced. */ export let LOOK = 2_000;
|
|
95
|
+
/**
|
|
96
|
+
* `run`'s answer, or the runtime's words once it moves the object off this
|
|
97
|
+
* instance while `run` has not answered. An instance reset under a request
|
|
98
|
+
* still runs it, and what the request waits on can stay unsettled after the
|
|
99
|
+
* reset, so the request never answers (T-160126). Failed in the words
|
|
100
|
+
* `evicted` knows, it is sent again by its caller to the instance that took
|
|
101
|
+
* its place, and the work left behind here can write nothing. While `run`
|
|
102
|
+
* waits, `replaced` is asked every `every` ms on `clock`.
|
|
103
|
+
*
|
|
104
|
+
* ```ts
|
|
105
|
+
* import { assertEquals, assertRejects } from '@std/assert'
|
|
106
|
+
* import { clock } from '@yaks/testing'
|
|
107
|
+
* let reset = false
|
|
108
|
+
* let storage = {
|
|
109
|
+
* sql: { exec: () => [] },
|
|
110
|
+
* transactionSync: <T>(body: () => T) => {
|
|
111
|
+
* if (reset) throw new Error('Durable Object reset because its code was updated.')
|
|
112
|
+
* return body()
|
|
113
|
+
* },
|
|
114
|
+
* }
|
|
115
|
+
* let time = clock()
|
|
116
|
+
* assertEquals(await vacating(() => Promise.resolve('ok'), storage, time), 'ok')
|
|
117
|
+
* let stuck = vacating(() => new Promise<never>(() => {}), storage, time)
|
|
118
|
+
* await time.advance(LOOK)
|
|
119
|
+
* reset = true
|
|
120
|
+
* await time.advance(LOOK)
|
|
121
|
+
* await assertRejects(() => stuck, Error, 'reset because its code was updated')
|
|
122
|
+
* ```
|
|
123
|
+
*/ export let vacating = (run, storage, clock = system, every = LOOK, signal)=>{
|
|
124
|
+
let answer = run();
|
|
125
|
+
if (!(answer instanceof Promise)) return answer;
|
|
126
|
+
return new Promise((resolve, reject)=>{
|
|
127
|
+
let stop = ()=>{};
|
|
128
|
+
let look = ()=>{
|
|
129
|
+
stop = clock.after(every, ()=>{
|
|
130
|
+
let gone = vacated(storage);
|
|
131
|
+
if (gone) reject(gone);
|
|
132
|
+
else look();
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
look();
|
|
136
|
+
let done = ()=>{
|
|
137
|
+
stop();
|
|
138
|
+
signal?.removeEventListener('abort', done);
|
|
139
|
+
};
|
|
140
|
+
signal?.addEventListener('abort', done, {
|
|
141
|
+
once: true
|
|
142
|
+
});
|
|
143
|
+
void answer.then(resolve, reject).finally(done);
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* The object was reset under a request, and the runtime's guidance is to fetch
|
|
148
|
+
* again: its own `retryable` flag on the error, or, on an older runtime, its
|
|
149
|
+
* words.
|
|
150
|
+
*
|
|
151
|
+
* ```ts
|
|
152
|
+
* import { assertEquals } from '@std/assert'
|
|
153
|
+
* assertEquals(evicted(Object.assign(new Error('reset'), { retryable: true })), true)
|
|
154
|
+
* assertEquals(evicted(new Error('Durable Object storage operation exceeded ' +
|
|
155
|
+
* 'timeout which caused object to be reset.')), true)
|
|
156
|
+
* assertEquals(evicted(new Error('boot failed')), false)
|
|
157
|
+
* assertEquals(evicted(null), false)
|
|
158
|
+
* ```
|
|
159
|
+
*/ export let evicted = (e)=>e instanceof Error && ('retryable' in e && e.retryable === true || says(e, RESETS));
|
|
160
|
+
/** A request dropped by an overloaded object before it could serve it. */ export let overloaded = (e)=>e instanceof Error && ('overloaded' in e && e.overloaded === true || e.message.includes('Durable Object is overloaded. Requests queued for too long.'));
|
|
161
|
+
/**
|
|
162
|
+
* The runtime reset the object under the call (`evicted`), or dropped the call
|
|
163
|
+
* from its queue (`overloaded`), in its flags or its words. A read, or a call
|
|
164
|
+
* carrying one idempotency key the object spends (door.ts `resending`), is
|
|
165
|
+
* sent again on it; a write with no key only on `untouched`. `retried` paces
|
|
166
|
+
* an overloaded object's resends apart (`backoff.crowded`).
|
|
167
|
+
*
|
|
168
|
+
* ```ts
|
|
169
|
+
* import { assertEquals } from '@std/assert'
|
|
170
|
+
* assertEquals(unserved(new Error('Network connection lost.')), true)
|
|
171
|
+
* assertEquals(unserved(Object.assign(new Error('x'), { overloaded: true })), true)
|
|
172
|
+
* assertEquals(unserved(new Error('boot failed')), false)
|
|
173
|
+
* ```
|
|
174
|
+
*/ export let unserved = (e)=>evicted(e) || overloaded(e);
|
|
175
|
+
let flag = (e, name)=>name in e && e[name] === true;
|
|
176
|
+
/**
|
|
177
|
+
* The runtime itself says the object never ran the call: its own `overloaded`
|
|
178
|
+
* or `retryable` flag, on an error the object's code did not throw. The words
|
|
179
|
+
* show nothing, and neither does a flag on an error marked `remote`: an
|
|
180
|
+
* object that met an overloaded or reset object it called, and rethrew what
|
|
181
|
+
* it met, ran the call up to there, and may have written. The runtime gives
|
|
182
|
+
* an object's own uncaught error to its caller with the message intact and
|
|
183
|
+
* `remote` set. So this, never `unserved`, is what sends a write with no
|
|
184
|
+
* idempotency key again.
|
|
185
|
+
*
|
|
186
|
+
* ```ts
|
|
187
|
+
* import { assertEquals } from '@std/assert'
|
|
188
|
+
* let words = 'Durable Object is overloaded. Requests queued for too long.'
|
|
189
|
+
* assertEquals(untouched(Object.assign(new Error(words), { overloaded: true })), true)
|
|
190
|
+
* assertEquals(untouched(Object.assign(new Error('reset'), { retryable: true })), true)
|
|
191
|
+
* assertEquals(untouched(new Error(words)), false)
|
|
192
|
+
* assertEquals(untouched(Object.assign(new Error(words), { overloaded: true, remote: true })), false)
|
|
193
|
+
* assertEquals(untouched(new Error('Network connection lost.')), false)
|
|
194
|
+
* ```
|
|
195
|
+
*/ export let untouched = (e)=>e instanceof Error && !flag(e, 'remote') && (flag(e, 'overloaded') || flag(e, 'retryable'));
|
|
196
|
+
/**
|
|
197
|
+
* The runtime killed the object for its limits. Its own code never sees the
|
|
198
|
+
* kill, and the retry may well answer, so whoever sees these words reports
|
|
199
|
+
* them before anything retries, or nothing ever would (M-37965).
|
|
200
|
+
*
|
|
201
|
+
* ```ts
|
|
202
|
+
* import { assertEquals } from '@std/assert'
|
|
203
|
+
* assertEquals(killed(new Error('Durable Object exceeded its CPU time limit and was reset.')), true)
|
|
204
|
+
* assertEquals(killed(new Error('boot failed')), false)
|
|
205
|
+
* ```
|
|
206
|
+
*/ export let killed = (e)=>e instanceof Error && LIMITS.test(e.message);
|
|
207
|
+
/**
|
|
208
|
+
* The object's SQLite refused a statement for memory. The engine also says
|
|
209
|
+
* this when a statement's program passes its step limit, which no wait cures,
|
|
210
|
+
* so it is reported and never sent again.
|
|
211
|
+
*
|
|
212
|
+
* ```ts
|
|
213
|
+
* import { assertEquals } from '@std/assert'
|
|
214
|
+
* assertEquals(starved(new Error('out of memory: SQLITE_NOMEM')), true)
|
|
215
|
+
* assertEquals(starved(new Error('boot failed')), false)
|
|
216
|
+
* ```
|
|
217
|
+
*/ export let starved = (e)=>e instanceof Error && STARVED.test(e.message);
|
|
218
|
+
/**
|
|
219
|
+
* The runtime failed inside itself and names no cause, only a reference for
|
|
220
|
+
* Cloudflare to look up. The work is tried again; a failure that persists
|
|
221
|
+
* through the retries is reported.
|
|
222
|
+
*
|
|
223
|
+
* ```ts
|
|
224
|
+
* import { assertEquals } from '@std/assert'
|
|
225
|
+
* assertEquals(opaque(new Error('internal error; reference = h1h5n5h0ltksfc1hb4h8qkit')), true)
|
|
226
|
+
* assertEquals(opaque(new Error('get: internal error; reference = abc')), false)
|
|
227
|
+
* assertEquals(opaque('internal error; reference = abc'), false)
|
|
228
|
+
* ```
|
|
229
|
+
*/ export let opaque = (e)=>e instanceof Error && OPAQUE.test(e.message);
|
|
230
|
+
// The pauses before a replayable call is sent again, one per resend, backing
|
|
231
|
+
// off as Cloudflare's guidance for a Durable Object's retryable errors has
|
|
232
|
+
// it. An object restarts for each newer deploy that reaches it, and each
|
|
233
|
+
// restart fails what it had in flight: a call cut off by one deploy's restart
|
|
234
|
+
// can have its retry cut off by the next one's when two land seconds apart.
|
|
235
|
+
// While the runtime moves an object it can refuse calls into it with its own
|
|
236
|
+
// internal error for ten to twenty seconds, so the pauses together outlast
|
|
237
|
+
// that: a read waits for the object rather than failing a person's page or
|
|
238
|
+
// leaving a store out of an answer. Only a call that keeps failing spends
|
|
239
|
+
// them.
|
|
240
|
+
//
|
|
241
|
+
// An object that dropped a call from its queue (`overloaded`) is busy, or
|
|
242
|
+
// stalled for seconds with its queue filling (the directory froze for 14 s on
|
|
243
|
+
// 2026-11-26, bug dbe06394), so its resends start a second apart and spread
|
|
244
|
+
// out, `crowded`: each adds one call to the queue it refused, not five, and
|
|
245
|
+
// together they outlast such a stall.
|
|
246
|
+
export let backoff = {
|
|
247
|
+
pauses: [
|
|
248
|
+
100,
|
|
249
|
+
500,
|
|
250
|
+
2000,
|
|
251
|
+
5000,
|
|
252
|
+
15000
|
|
253
|
+
],
|
|
254
|
+
crowded: [
|
|
255
|
+
1000,
|
|
256
|
+
3000,
|
|
257
|
+
8000
|
|
258
|
+
]
|
|
259
|
+
};
|
|
260
|
+
/** How `retried` waits out a pause: half of it or more, so calls that failed
|
|
261
|
+
* together do not return together, on `clock` (the runtime's by default). */ export let jittered = (clock = system)=>(ms)=>sleep(ms / 2 + Math.random() * ms / 2, undefined, clock);
|
|
262
|
+
/**
|
|
263
|
+
* Send one replayable call again while it fails in words `retry` knows,
|
|
264
|
+
* pausing before each resend, once per `backoff` pause (`crowded` after an
|
|
265
|
+
* `overloaded` refusal), as `wait` waits one
|
|
266
|
+
* (`jittered` on the runtime's clock by default; a test hands its own). The
|
|
267
|
+
* last failure propagates unchanged.
|
|
268
|
+
*
|
|
269
|
+
* ```ts
|
|
270
|
+
* import { assertEquals } from '@std/assert'
|
|
271
|
+
* let sends = 0
|
|
272
|
+
* let send = () => {
|
|
273
|
+
* if (++sends < 3) throw new Error('Network connection lost.')
|
|
274
|
+
* return 'answered'
|
|
275
|
+
* }
|
|
276
|
+
* let paused: number[] = []
|
|
277
|
+
* let wait = (ms: number) => Promise.resolve(void paused.push(ms))
|
|
278
|
+
* assertEquals(await retried(send, true, evicted, wait), 'answered')
|
|
279
|
+
* assertEquals(paused, [100, 500])
|
|
280
|
+
* assertEquals(sends, 3)
|
|
281
|
+
* let crowded = Object.assign(new Error('queued too long'), { overloaded: true })
|
|
282
|
+
* let refused = 0
|
|
283
|
+
* let busy = () => {
|
|
284
|
+
* if (++refused < 3) throw crowded
|
|
285
|
+
* return 'answered'
|
|
286
|
+
* }
|
|
287
|
+
* paused = []
|
|
288
|
+
* assertEquals(await retried(busy, true, unserved, wait), 'answered')
|
|
289
|
+
* assertEquals(paused, [1000, 3000])
|
|
290
|
+
* ```
|
|
291
|
+
*/ export let retried = async (send, replayable = true, retry = evicted, wait = jittered())=>{
|
|
292
|
+
for(let sent = 0;; sent++){
|
|
293
|
+
try {
|
|
294
|
+
return await send();
|
|
295
|
+
} catch (e) {
|
|
296
|
+
let pause = (overloaded(e) ? backoff.crowded : backoff.pauses)[sent];
|
|
297
|
+
if (!retry(e) || !replayable || pause == null) throw e;
|
|
298
|
+
await wait(pause);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
//# sourceMappingURL=resets.js.map
|
package/resets.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resets.js","sources":["./resets.ts"],"names":[],"mappings":"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"}
|
package/resets.ts
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
// The words the runtime fails a Durable Object's work with when it resets the
|
|
2
|
+
// object, and which of them are ours to fix.
|
|
3
|
+
//
|
|
4
|
+
// The runtime moves an object to a new instance whenever it likes: a runtime
|
|
5
|
+
// update, another machine, a deploy, a failure inside its own storage or in
|
|
6
|
+
// starting that storage up, a lost connection to it. The instance that was
|
|
7
|
+
// moved fails what it had in flight, and whoever owns that work runs it again:
|
|
8
|
+
// a caller fetches again on a fresh stub, the runtime retries an alarm that
|
|
9
|
+
// failed. Cloudflare documents the move as part of an object's life, so what
|
|
10
|
+
// the moved instance meets is no defect. A live object that meets the words from an object it called has run
|
|
11
|
+
// out of retries, and that is a failure; so is a reset our own code caused: a
|
|
12
|
+
// storage operation over its timeout, or a kill for the CPU or memory limit.
|
|
13
|
+
|
|
14
|
+
import { type Clock, sleep, system } from '@yaks/fp'
|
|
15
|
+
|
|
16
|
+
let MOVED = [
|
|
17
|
+
'Durable Object instance is no longer active',
|
|
18
|
+
'Durable Object reset because',
|
|
19
|
+
'Internal error in Durable Object storage',
|
|
20
|
+
'Internal error while starting up Durable Object storage',
|
|
21
|
+
// A connection to the object dropped. The runtime names it in words alone,
|
|
22
|
+
// with no `retryable` flag: a caller meets it when the call it sent is cut
|
|
23
|
+
// off, and an instance whose own storage lost its connection refuses every
|
|
24
|
+
// statement in them until it is replaced.
|
|
25
|
+
'Network connection lost',
|
|
26
|
+
]
|
|
27
|
+
let RESETS = [
|
|
28
|
+
...MOVED,
|
|
29
|
+
'Durable Object storage operation exceeded timeout which caused object to be reset',
|
|
30
|
+
]
|
|
31
|
+
let LIMITS = /exceeded its (CPU time|memory) limit/
|
|
32
|
+
let STARVED = /\bSQLITE_NOMEM\b/
|
|
33
|
+
let OPAQUE = /^internal error; reference = [a-z0-9]+$/i
|
|
34
|
+
|
|
35
|
+
let says = (e: unknown, words: string[]) =>
|
|
36
|
+
e instanceof Error && words.some((said) => e.message.includes(said))
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The runtime moved this object to a new instance, and the work is run again
|
|
40
|
+
* by whoever owns it.
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { assertEquals } from '@std/assert'
|
|
44
|
+
* let gone = new Error('Connection closed: this Durable Object instance is ' +
|
|
45
|
+
* 'no longer active. Reconnect or retry the request.')
|
|
46
|
+
* assertEquals(moved(gone), true)
|
|
47
|
+
* assertEquals(moved(new Error('Durable Object reset because its code was updated.')), true)
|
|
48
|
+
* assertEquals(moved(new Error('Internal error in Durable Object storage ' +
|
|
49
|
+
* 'caused object to be reset; reference = 48q6hau1fptrm6g69pk2lqqh')), true)
|
|
50
|
+
* assertEquals(moved(new Error('Internal error while starting up Durable ' +
|
|
51
|
+
* 'Object storage caused object to be reset; reference = k6485n9h')), true)
|
|
52
|
+
* assertEquals(moved(new Error('Network connection lost.')), true)
|
|
53
|
+
* assertEquals(moved(new Error('Durable Object storage operation exceeded ' +
|
|
54
|
+
* 'timeout which caused object to be reset.')), false)
|
|
55
|
+
* assertEquals(moved(null), false)
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export let moved = (e: unknown): boolean => says(e, MOVED)
|
|
59
|
+
|
|
60
|
+
/** The slice of an object's storage that `replaced` asks. */
|
|
61
|
+
export type Own = {
|
|
62
|
+
sql: { exec(query: string): unknown }
|
|
63
|
+
transactionSync<T>(body: () => T): T
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Whether the runtime has moved the object off the instance this storage
|
|
68
|
+
* belongs to. A moved instance refuses its own storage in the words `moved`
|
|
69
|
+
* knows, a lost connection among them, but not all of it: one reset for a
|
|
70
|
+
* deploy still answers a read and refuses every write (workerd's
|
|
71
|
+
* `ActorSqlite::requireNotBroken`). A transaction counts as a write even when
|
|
72
|
+
* its body only reads, so it asks both; the live instance answers. An
|
|
73
|
+
* instance the runtime killed for its CPU or memory limit is replaced too:
|
|
74
|
+
* its transactions say the kill (`killed`), which whoever met it first
|
|
75
|
+
* reports, and its reads go on failing as a lost connection.
|
|
76
|
+
*
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { assertEquals } from '@std/assert'
|
|
79
|
+
* let reset = () => {
|
|
80
|
+
* throw new Error('Durable Object reset because its code was updated.')
|
|
81
|
+
* }
|
|
82
|
+
* let sql = { exec: () => [] }
|
|
83
|
+
* let live = { sql, transactionSync: <T>(body: () => T) => body() }
|
|
84
|
+
* assertEquals(replaced(live), false)
|
|
85
|
+
* assertEquals(replaced({ sql, transactionSync: reset }), true)
|
|
86
|
+
* assertEquals(replaced({ ...live, sql: { exec: reset } }), true)
|
|
87
|
+
* let cut = () => {
|
|
88
|
+
* throw new Error('Network connection lost.')
|
|
89
|
+
* }
|
|
90
|
+
* assertEquals(replaced({ ...live, sql: { exec: cut } }), true)
|
|
91
|
+
* let kill = () => {
|
|
92
|
+
* throw new Error("Durable Object's isolate exceeded its memory limit and was reset.")
|
|
93
|
+
* }
|
|
94
|
+
* assertEquals(replaced({ sql, transactionSync: kill }), true)
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export let replaced = (storage: Own): boolean => vacated(storage) != null
|
|
98
|
+
|
|
99
|
+
// The runtime's words for the move, as this instance's storage refuses in
|
|
100
|
+
// them, or null while the instance is the live one.
|
|
101
|
+
let vacated = (storage: Own): Error | null => {
|
|
102
|
+
try {
|
|
103
|
+
storage.transactionSync(() => storage.sql.exec('SELECT 1'))
|
|
104
|
+
return null
|
|
105
|
+
} catch (e) {
|
|
106
|
+
return moved(e) || killed(e) ? e as Error : null
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** How often `vacating` asks whether the instance was replaced. */
|
|
111
|
+
export let LOOK = 2_000
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* `run`'s answer, or the runtime's words once it moves the object off this
|
|
115
|
+
* instance while `run` has not answered. An instance reset under a request
|
|
116
|
+
* still runs it, and what the request waits on can stay unsettled after the
|
|
117
|
+
* reset, so the request never answers (T-160126). Failed in the words
|
|
118
|
+
* `evicted` knows, it is sent again by its caller to the instance that took
|
|
119
|
+
* its place, and the work left behind here can write nothing. While `run`
|
|
120
|
+
* waits, `replaced` is asked every `every` ms on `clock`.
|
|
121
|
+
*
|
|
122
|
+
* ```ts
|
|
123
|
+
* import { assertEquals, assertRejects } from '@std/assert'
|
|
124
|
+
* import { clock } from '@yaks/testing'
|
|
125
|
+
* let reset = false
|
|
126
|
+
* let storage = {
|
|
127
|
+
* sql: { exec: () => [] },
|
|
128
|
+
* transactionSync: <T>(body: () => T) => {
|
|
129
|
+
* if (reset) throw new Error('Durable Object reset because its code was updated.')
|
|
130
|
+
* return body()
|
|
131
|
+
* },
|
|
132
|
+
* }
|
|
133
|
+
* let time = clock()
|
|
134
|
+
* assertEquals(await vacating(() => Promise.resolve('ok'), storage, time), 'ok')
|
|
135
|
+
* let stuck = vacating(() => new Promise<never>(() => {}), storage, time)
|
|
136
|
+
* await time.advance(LOOK)
|
|
137
|
+
* reset = true
|
|
138
|
+
* await time.advance(LOOK)
|
|
139
|
+
* await assertRejects(() => stuck, Error, 'reset because its code was updated')
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
export let vacating = <T>(
|
|
143
|
+
run: () => T,
|
|
144
|
+
storage: Own,
|
|
145
|
+
clock: Clock = system,
|
|
146
|
+
every = LOOK,
|
|
147
|
+
signal?: AbortSignal,
|
|
148
|
+
): T => {
|
|
149
|
+
let answer = run()
|
|
150
|
+
if (!(answer instanceof Promise)) return answer
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
let stop = () => {}
|
|
153
|
+
let look = () => {
|
|
154
|
+
stop = clock.after(every, () => {
|
|
155
|
+
let gone = vacated(storage)
|
|
156
|
+
if (gone) reject(gone)
|
|
157
|
+
else look()
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
look()
|
|
161
|
+
let done = () => {
|
|
162
|
+
stop()
|
|
163
|
+
signal?.removeEventListener('abort', done)
|
|
164
|
+
}
|
|
165
|
+
signal?.addEventListener('abort', done, { once: true })
|
|
166
|
+
void answer.then(resolve, reject).finally(done)
|
|
167
|
+
}) as T
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* The object was reset under a request, and the runtime's guidance is to fetch
|
|
172
|
+
* again: its own `retryable` flag on the error, or, on an older runtime, its
|
|
173
|
+
* words.
|
|
174
|
+
*
|
|
175
|
+
* ```ts
|
|
176
|
+
* import { assertEquals } from '@std/assert'
|
|
177
|
+
* assertEquals(evicted(Object.assign(new Error('reset'), { retryable: true })), true)
|
|
178
|
+
* assertEquals(evicted(new Error('Durable Object storage operation exceeded ' +
|
|
179
|
+
* 'timeout which caused object to be reset.')), true)
|
|
180
|
+
* assertEquals(evicted(new Error('boot failed')), false)
|
|
181
|
+
* assertEquals(evicted(null), false)
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
export let evicted = (e: unknown): boolean =>
|
|
185
|
+
e instanceof Error &&
|
|
186
|
+
(('retryable' in e && e.retryable === true) || says(e, RESETS))
|
|
187
|
+
|
|
188
|
+
/** A request dropped by an overloaded object before it could serve it. */
|
|
189
|
+
export let overloaded = (e: unknown): e is Error =>
|
|
190
|
+
e instanceof Error &&
|
|
191
|
+
(('overloaded' in e && e.overloaded === true) ||
|
|
192
|
+
e.message.includes(
|
|
193
|
+
'Durable Object is overloaded. Requests queued for too long.',
|
|
194
|
+
))
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The runtime reset the object under the call (`evicted`), or dropped the call
|
|
198
|
+
* from its queue (`overloaded`), in its flags or its words. A read, or a call
|
|
199
|
+
* carrying one idempotency key the object spends (door.ts `resending`), is
|
|
200
|
+
* sent again on it; a write with no key only on `untouched`. `retried` paces
|
|
201
|
+
* an overloaded object's resends apart (`backoff.crowded`).
|
|
202
|
+
*
|
|
203
|
+
* ```ts
|
|
204
|
+
* import { assertEquals } from '@std/assert'
|
|
205
|
+
* assertEquals(unserved(new Error('Network connection lost.')), true)
|
|
206
|
+
* assertEquals(unserved(Object.assign(new Error('x'), { overloaded: true })), true)
|
|
207
|
+
* assertEquals(unserved(new Error('boot failed')), false)
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
export let unserved = (e: unknown): boolean => evicted(e) || overloaded(e)
|
|
211
|
+
|
|
212
|
+
let flag = (e: Error, name: string) =>
|
|
213
|
+
name in e && (e as unknown as Record<string, unknown>)[name] === true
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The runtime itself says the object never ran the call: its own `overloaded`
|
|
217
|
+
* or `retryable` flag, on an error the object's code did not throw. The words
|
|
218
|
+
* show nothing, and neither does a flag on an error marked `remote`: an
|
|
219
|
+
* object that met an overloaded or reset object it called, and rethrew what
|
|
220
|
+
* it met, ran the call up to there, and may have written. The runtime gives
|
|
221
|
+
* an object's own uncaught error to its caller with the message intact and
|
|
222
|
+
* `remote` set. So this, never `unserved`, is what sends a write with no
|
|
223
|
+
* idempotency key again.
|
|
224
|
+
*
|
|
225
|
+
* ```ts
|
|
226
|
+
* import { assertEquals } from '@std/assert'
|
|
227
|
+
* let words = 'Durable Object is overloaded. Requests queued for too long.'
|
|
228
|
+
* assertEquals(untouched(Object.assign(new Error(words), { overloaded: true })), true)
|
|
229
|
+
* assertEquals(untouched(Object.assign(new Error('reset'), { retryable: true })), true)
|
|
230
|
+
* assertEquals(untouched(new Error(words)), false)
|
|
231
|
+
* assertEquals(untouched(Object.assign(new Error(words), { overloaded: true, remote: true })), false)
|
|
232
|
+
* assertEquals(untouched(new Error('Network connection lost.')), false)
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
export let untouched = (e: unknown): boolean =>
|
|
236
|
+
e instanceof Error && !flag(e, 'remote') &&
|
|
237
|
+
(flag(e, 'overloaded') || flag(e, 'retryable'))
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The runtime killed the object for its limits. Its own code never sees the
|
|
241
|
+
* kill, and the retry may well answer, so whoever sees these words reports
|
|
242
|
+
* them before anything retries, or nothing ever would (M-37965).
|
|
243
|
+
*
|
|
244
|
+
* ```ts
|
|
245
|
+
* import { assertEquals } from '@std/assert'
|
|
246
|
+
* assertEquals(killed(new Error('Durable Object exceeded its CPU time limit and was reset.')), true)
|
|
247
|
+
* assertEquals(killed(new Error('boot failed')), false)
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
export let killed = (e: unknown): boolean =>
|
|
251
|
+
e instanceof Error && LIMITS.test(e.message)
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The object's SQLite refused a statement for memory. The engine also says
|
|
255
|
+
* this when a statement's program passes its step limit, which no wait cures,
|
|
256
|
+
* so it is reported and never sent again.
|
|
257
|
+
*
|
|
258
|
+
* ```ts
|
|
259
|
+
* import { assertEquals } from '@std/assert'
|
|
260
|
+
* assertEquals(starved(new Error('out of memory: SQLITE_NOMEM')), true)
|
|
261
|
+
* assertEquals(starved(new Error('boot failed')), false)
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
export let starved = (e: unknown): boolean =>
|
|
265
|
+
e instanceof Error && STARVED.test(e.message)
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* The runtime failed inside itself and names no cause, only a reference for
|
|
269
|
+
* Cloudflare to look up. The work is tried again; a failure that persists
|
|
270
|
+
* through the retries is reported.
|
|
271
|
+
*
|
|
272
|
+
* ```ts
|
|
273
|
+
* import { assertEquals } from '@std/assert'
|
|
274
|
+
* assertEquals(opaque(new Error('internal error; reference = h1h5n5h0ltksfc1hb4h8qkit')), true)
|
|
275
|
+
* assertEquals(opaque(new Error('get: internal error; reference = abc')), false)
|
|
276
|
+
* assertEquals(opaque('internal error; reference = abc'), false)
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
export let opaque = (e: unknown): e is Error =>
|
|
280
|
+
e instanceof Error && OPAQUE.test(e.message)
|
|
281
|
+
|
|
282
|
+
// The pauses before a replayable call is sent again, one per resend, backing
|
|
283
|
+
// off as Cloudflare's guidance for a Durable Object's retryable errors has
|
|
284
|
+
// it. An object restarts for each newer deploy that reaches it, and each
|
|
285
|
+
// restart fails what it had in flight: a call cut off by one deploy's restart
|
|
286
|
+
// can have its retry cut off by the next one's when two land seconds apart.
|
|
287
|
+
// While the runtime moves an object it can refuse calls into it with its own
|
|
288
|
+
// internal error for ten to twenty seconds, so the pauses together outlast
|
|
289
|
+
// that: a read waits for the object rather than failing a person's page or
|
|
290
|
+
// leaving a store out of an answer. Only a call that keeps failing spends
|
|
291
|
+
// them.
|
|
292
|
+
//
|
|
293
|
+
// An object that dropped a call from its queue (`overloaded`) is busy, or
|
|
294
|
+
// stalled for seconds with its queue filling (the directory froze for 14 s on
|
|
295
|
+
// 2026-11-26, bug dbe06394), so its resends start a second apart and spread
|
|
296
|
+
// out, `crowded`: each adds one call to the queue it refused, not five, and
|
|
297
|
+
// together they outlast such a stall.
|
|
298
|
+
export let backoff: { pauses: number[]; crowded: number[] } = {
|
|
299
|
+
pauses: [100, 500, 2000, 5000, 15000],
|
|
300
|
+
crowded: [1000, 3000, 8000],
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** How `retried` waits out a pause: half of it or more, so calls that failed
|
|
304
|
+
* together do not return together, on `clock` (the runtime's by default). */
|
|
305
|
+
export let jittered = (clock: Clock = system) => (ms: number): Promise<void> =>
|
|
306
|
+
sleep(ms / 2 + Math.random() * ms / 2, undefined, clock)
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Send one replayable call again while it fails in words `retry` knows,
|
|
310
|
+
* pausing before each resend, once per `backoff` pause (`crowded` after an
|
|
311
|
+
* `overloaded` refusal), as `wait` waits one
|
|
312
|
+
* (`jittered` on the runtime's clock by default; a test hands its own). The
|
|
313
|
+
* last failure propagates unchanged.
|
|
314
|
+
*
|
|
315
|
+
* ```ts
|
|
316
|
+
* import { assertEquals } from '@std/assert'
|
|
317
|
+
* let sends = 0
|
|
318
|
+
* let send = () => {
|
|
319
|
+
* if (++sends < 3) throw new Error('Network connection lost.')
|
|
320
|
+
* return 'answered'
|
|
321
|
+
* }
|
|
322
|
+
* let paused: number[] = []
|
|
323
|
+
* let wait = (ms: number) => Promise.resolve(void paused.push(ms))
|
|
324
|
+
* assertEquals(await retried(send, true, evicted, wait), 'answered')
|
|
325
|
+
* assertEquals(paused, [100, 500])
|
|
326
|
+
* assertEquals(sends, 3)
|
|
327
|
+
* let crowded = Object.assign(new Error('queued too long'), { overloaded: true })
|
|
328
|
+
* let refused = 0
|
|
329
|
+
* let busy = () => {
|
|
330
|
+
* if (++refused < 3) throw crowded
|
|
331
|
+
* return 'answered'
|
|
332
|
+
* }
|
|
333
|
+
* paused = []
|
|
334
|
+
* assertEquals(await retried(busy, true, unserved, wait), 'answered')
|
|
335
|
+
* assertEquals(paused, [1000, 3000])
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
338
|
+
export let retried = async <T>(
|
|
339
|
+
send: () => T | Promise<T>,
|
|
340
|
+
replayable = true,
|
|
341
|
+
retry: (error: unknown) => boolean = evicted,
|
|
342
|
+
wait: (ms: number) => Promise<void> = jittered(),
|
|
343
|
+
): Promise<T> => {
|
|
344
|
+
for (let sent = 0;; sent++) {
|
|
345
|
+
try {
|
|
346
|
+
return await send()
|
|
347
|
+
} catch (e) {
|
|
348
|
+
let pause = (overloaded(e) ? backoff.crowded : backoff.pauses)[sent]
|
|
349
|
+
if (!retry(e) || !replayable || pause == null) throw e
|
|
350
|
+
await wait(pause)
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|