Repository navigation
feat: partition gateway dispatches per guild and harden the pipeline - #95
Conversation
◈ PR Lens
Architecture 5 components touched across 3 lanes. Play the interactive walkthrough Inside the changed components — 2 viewsComponent view — Partitioned Dispatch Pipeline Dispatches are queued per guild or channel partition with timeout detection and cache failure recovery. Component view — Cached Entity Managers Managers provide cached reads with force and cache options for REST fallbacks and thread cache routing. Data flow
Follow each request, response and payload The other flows — 2 sequences
View
Tip The diagrams are links. Click one to open it on the canvas, then press W or click play to walk through the change 🪧 More tips
Thanks for using PR Lens! It's built by Coldtea, free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
There was a problem hiding this comment.
Important
Cache failures during READY reconciliation can silently drop the whole READY dispatch under the default cacheFailure setting — see inline comment.
Reviewed changes
Reviewed the full diff: a new per-partition DispatchQueue replacing the per-shard queue, a dispatchTimeout watchdog, a cacheFailure policy, CachedManager.fetch's new { force, cache } options, and READY guild reconciliation.
DispatchQueue/dispatchPartition(util/DispatchQueue.ts, new) — dispatches now partition by guild or DM channel and run concurrently across partitions, staying ordered within a partition; dispatches with no guild/channel (READY,USER_UPDATE, ...) act as barriers that wait for, and are waited on by, everything else on their shard.dispatchTimeout(GatewayClient.runDispatch) — a non-cancelling watchdog that reports aDispatchTimeoutErrorif a dispatch is still running past the deadline (default 30s).cacheFailure(GatewayClient.handleDispatch) —"skip"(default) rethrows and drops the dispatch's event on a cache read/write failure;"emitUncached"swallows it and still builds/emits the event from the payload. The twobuildfunctions that do their own post-catch cache reads (GuildCreate,GuildMemberUpdate) wrap them withcachedOrUndefinedso"emitUncached"doesn't rethrow a second time.CachedManager.fetch(...ids, options?)—{ force, cache }options now follow the id args;refreshisfetch(..., { force: true }); storing a fetched value moved into an overridablestoreRawhook, whichChannelManageruses to route threads into the thread cache.READYreconciliation (GatewayClient.reconcileGuilds) — diffs the cache's guild keys for the shard againstdata.guilds(via(id >> 22) % shardCount) and drops/emitsguildDeletefor guilds the bot left while disconnected.
ℹ️ Nitpicks
tests/hardening.test.ts:247assertserrors.lengthwithtoBeGreaterThanOrEqual(2). The count is actually deterministic here (one error from theGuildCreatecache write failing, one fromGuildUpdate'sbeforeread failing) —toBe(2)would also catch an accidental double-report regression that the loose bound wouldn't.
Claude Sonnet | 𝕏
@wolfstar/plugin-api
@wolfstar/plugin-cache
@wolfstar/plugin-gateway
@wolfstar/plugin-i18next
@wolfstar/plugin-logger
@wolfstar/plugin-subcommands-advanced
commit: |
❌ 6 Tests Failed:
View the top 3 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Reviewed commit 1b11001, which fixes the "Important" issue from the prior review: READY could still be silently dropped by a cache failure during guild reconciliation.
- Moved
reconcileGuilds(its body renamed todropUnlistedGuilds) outsidehandleDispatch's outer try/catch, wrapped in its own try/catch that reports and never rethrows — a cache failure anywhere in reconciliation can no longer propagate and drop the dispatch. - Exempted
READYfrom thecacheFailure: "skip"rethrow, so a cache write failure while processingREADY's own payload (e.g. the write that setsclient.user) also falls through tohandler.buildinstead of dropping the dispatch. - Extracted a shared
reportErrorhelper used byhandleDispatch,reconcileGuilds, and the dispatch-timeout watchdog. - Added a test asserting
shardReadystill fires andclient.useris still set when both the guild-key lookup and theREADYcache write reject, with an exacttoHaveLength(2)on the reported errors. - Updated the README and changeset to document that
READYis always emitted regardless of cache failures.
I traced the full control flow (not just the diff) and confirmed the fix is structurally sound: reconcileGuilds's own try/catch can no longer throw, and the outer catch's !isReady guard covers the remaining path. Replied to and resolved the prior review's inline thread on GatewayClient.ts:273.
Claude Sonnet | 𝕏
85e7352 to
faeaa10
Compare
faeaa10 to
b60f575
Compare
|
Your Claude subscription has hit its usage limit. It resets at 7:20pm (UTC). Re-trigger Pullfrog after the reset, or add an Add repo secret → · Model settings → · Setup docs → · Ask in Discord →
|
…ipeline
Dispatches run in order per guild (or DM channel) instead of per shard,
with guild-less dispatches as barriers. Slow dispatches are reported as a
DispatchTimeoutError, cache failures follow a configurable policy,
managers' fetch takes { force, cache }, and READY drops the cached guilds
it no longer lists.
Refs #54
Reconciliation is best effort and reports its own errors, and READY is emitted even under cacheFailure "skip", since it sets client.user from the payload alone.
b60f575 to
908b961
Compare
|
Your Claude subscription has hit its usage limit. It resets at 7:20pm (UTC). Re-trigger Pullfrog after the reset, or add an Add repo secret → · Model settings → · Setup docs → · Ask in Discord →
|

Stacked on #94.
🔗 Linked issue
Related to #54 (phase 4, dispatch hardening from the September addendum).
🧭 Context
The status update on #54 left five addendum items open. Each is covered below.
fetchoptions: thefetch(id, { force, cache })shape from discord.js#11426 was missing.📚 Description
Partitioned queue. Dispatches now run in order per guild, or per channel for direct messages, and different guilds run concurrently. Dispatches that belong to no guild are barriers: they wait for everything already queued on their shard, and everything after them waits for them. Examples are
READYandUSER_UPDATE. Without that,READY'sunavailableplaceholders could land after aGUILD_CREATEon an async store and mark a live guild unavailable.Timeouts. A dispatch still running after
dispatchTimeout(30s by default) is reported as aDispatchTimeoutError. It is deliberately not cancelled, because releasing the partition early would break the ordering this queue exists for.client.queueStatsexposes pending dispatches and partitions.Cache failures. A new
cacheFailureoption picks what happens when the cache fails:"skip"(the default, and the current behaviour) drops the event;"emitUncached"emits it anyway, built from the payload, withnullas the previous state.The two builds that re-read the merged cache entry fall back to the payload when that read fails too.
fetchoptions. Managers'fetchtakes{ force, cache }after the IDs.refreshstays, asfetchwithforce: true.READY reconciliation. On
READY, the cached guilds of that shard thatREADYno longer lists are dropped and emitted asguildDelete. This also covers a restart with a persistent Redis cache. The shard count comes from the payload, the options, or the gateway. When none of them is available, reconciliation is skipped and reported, instead of failingREADY. An existing test caught that case: my first version madeclient.userstaynull.Still open for #54: a test with simultaneous HTTP interactions, and exposing latency rather than just a timeout. I'm not sure a latency metric belongs in the client rather than in a logger or metrics plugin; happy to hear a preference.
Key changes
util/DispatchQueue.tsDispatchQueue+dispatchPartition: per-guild/channel partitions, per-shard barriers, statsGatewayClientcacheFailure,dispatchTimeout,queueStats, READY reconciliationutil/errors.tsDispatchTimeoutErrorCachedManagerfetch(...ids, { force, cache }),storeRawhook (threads routing moved there)Type of Change
Pre-flight Checklist
pnpm changeset(orpnpm changeset add --emptyif no release is needed). See .changeset/README.md.