Repository navigation
feat(client): add stale cache fallback on disconnect - #3495
PavelPashov wants to merge 1 commit into
Conversation
Add an opt-in serve-stale disconnect policy with a required maximum stale age. Return cached values when reads fail with connection errors and clear stale entries on reconnect.
Cdineshreddy12
left a comment
There was a problem hiding this comment.
Nice feature — the test matrix (connection-error vs error-reply vs abort, joined readers, maxStaleAge) is thorough. I spent time in cache.ts recently (#3641) so I focused the review there; one correctness point and a couple of smaller notes.
#onDisconnect drops in-flight promise entries without invalidating them, which reintroduces the race #3641 just fixed in clear(). The serve-stale branch moves value entries to #staleEntries and then does:
this.#cacheKeyToEntryMap.clear();That drops any in-flight promise entries (loads that haven't resolved yet) without calling entry.invalidate() on them. handleCache() re-validates the promise entry before storing (step 3d), so a load that resolves in the narrow window where its reply and onError() interleave will pass validate() and re-insert its value into the now-cleared live map — a live entry appearing while the client is disconnected. That's exactly the case #3641 added the invalidation loop to clear() for:
// current master, clear():
for (const entry of this.#cacheKeyToEntryMap.values()) entry.invalidate();
this.#cacheKeyToEntryMap.clear();This PR predates #3641, so after a rebase the comment in #onDisconnect ("Promise entries are dropped exactly as clear() drops them") no longer holds — clear() now invalidates them first and #onDisconnect doesn't. Suggest invalidating the promise entries you drop (iterate and invalidate() the ones you're not moving to stale, or anything still in #cacheKeyToEntryMap after the value-entry move). A test: start a load, fire onError() before it resolves, then resolve it successfully, and assert size() stays 0.
Smaller:
isConnectionErrortreats anyErrorwith a'syscall' in erras a connection error. That's the right intent for rawnet.Socketerrors (ECONNRESET/EPIPE), but it's a structural check — worth a comment that it's deliberately matching Node system errors, since a future error type that happens to carrysyscallwould be served stale.- Stale entries past
maxStaleAgeare only removed when a read touches them (per the test). During a long outage, keys that aren't read again sit in#staleEntriespast their max age until reconnect. Probably fine givenmaxStaleAgebounds correctness, but worth noting it's a retained-memory tradeoff rather than a time-based sweep.
Haven't run it against a server (the cluster/standalone docker harness uses --network host, which doesn't map ports on Docker for Mac), but the unit tests read correctly and cover the branches well.
Description
Add an opt-in serve-stale disconnect policy with a required maximum stale age. Return cached values when reads fail with connection errors and clear stale entries on reconnect.
Checklist
npm testpass with this change (including linting)?