From 234403f7d5781ed7681dd50a8585bb7c5ccda857 Mon Sep 17 00:00:00 2001 From: Rory& Date: Tue, 23 Jun 2026 09:02:32 +0200 Subject: [PATCH] Gateway offload: gracefully fall back to TS code on failure --- src/gateway/opcodes/GuildSync.ts | 2 +- src/gateway/opcodes/LazyRequest.ts | 2 +- src/gateway/opcodes/RequestChannelInfo.ts | 2 +- src/gateway/opcodes/RequestChannelStatuses.ts | 2 +- src/gateway/opcodes/RequestGuildMembers.ts | 2 +- src/gateway/util/Utils.ts | 7 +++++-- 6 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/gateway/opcodes/GuildSync.ts b/src/gateway/opcodes/GuildSync.ts index f7c0dd0c9..ce7ea773c 100644 --- a/src/gateway/opcodes/GuildSync.ts +++ b/src/gateway/opcodes/GuildSync.ts @@ -32,7 +32,7 @@ export async function onGuildSync(this: WebSocket, { d }: Payload) { if (!Array.isArray(d)) throw new Error("Invalid payload for GUILD_SYNC"); if (Config.get().offload.gateway.guildSyncUrl !== null) { - return await handleOffloadedGatewayRequest(this, Config.get().offload.gateway.guildSyncUrl!, d); + if (await handleOffloadedGatewayRequest(this, Config.get().offload.gateway.guildSyncUrl!, d)) return; } const guild_ids = d as string[]; diff --git a/src/gateway/opcodes/LazyRequest.ts b/src/gateway/opcodes/LazyRequest.ts index 410c265f1..342401b4a 100644 --- a/src/gateway/opcodes/LazyRequest.ts +++ b/src/gateway/opcodes/LazyRequest.ts @@ -159,7 +159,7 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) { const { guild_id, typing, channels, activities, members } = d as LazyRequestSchema; if (Config.get().offload.gateway.lazyRequestUrl !== null) { - return await handleOffloadedGatewayRequest(this, Config.get().offload.gateway.lazyRequestUrl!, d); + if (await handleOffloadedGatewayRequest(this, Config.get().offload.gateway.lazyRequestUrl!, d)) return; } if (members) { diff --git a/src/gateway/opcodes/RequestChannelInfo.ts b/src/gateway/opcodes/RequestChannelInfo.ts index 8cb9098de..7ef91df2e 100644 --- a/src/gateway/opcodes/RequestChannelInfo.ts +++ b/src/gateway/opcodes/RequestChannelInfo.ts @@ -27,7 +27,7 @@ export async function onRequestChannelInfo(this: WebSocket, { d }: Payload) { if (!d.fields) throw new Error('"fields" is required'); if (Config.get().offload.gateway.channelInfoUrl !== null) { - return await handleOffloadedGatewayRequest(this, Config.get().offload.gateway.channelInfoUrl!, d); + if (await handleOffloadedGatewayRequest(this, Config.get().offload.gateway.channelInfoUrl!, d)) return; } const channels = ( diff --git a/src/gateway/opcodes/RequestChannelStatuses.ts b/src/gateway/opcodes/RequestChannelStatuses.ts index 1747faae3..b0b291e78 100644 --- a/src/gateway/opcodes/RequestChannelStatuses.ts +++ b/src/gateway/opcodes/RequestChannelStatuses.ts @@ -24,7 +24,7 @@ export async function onRequestChannelStatuses(this: WebSocket, { d }: Payload) if (!d.guild_id) throw new Error('"guild_id" is required'); if (Config.get().offload.gateway.channelStatusesUrl !== null) { - return await handleOffloadedGatewayRequest(this, Config.get().offload.gateway.channelStatusesUrl!, d); + if (await handleOffloadedGatewayRequest(this, Config.get().offload.gateway.channelStatusesUrl!, d)) return; } // TODO: implement diff --git a/src/gateway/opcodes/RequestGuildMembers.ts b/src/gateway/opcodes/RequestGuildMembers.ts index db13ea8c1..ab7f516d0 100644 --- a/src/gateway/opcodes/RequestGuildMembers.ts +++ b/src/gateway/opcodes/RequestGuildMembers.ts @@ -34,7 +34,7 @@ export async function onRequestGuildMembers(this: WebSocket, { d }: Payload) { if (Config.get().offload.gateway.guildMembersUrl !== null) { const guildIds: string[] = Array.isArray(d.guild_id) ? d.guild_id : [d.guild_id]; - return await handleOffloadedGatewayRequest(this, Config.get().offload.gateway.guildMembersUrl!, guildIds); + if (await handleOffloadedGatewayRequest(this, Config.get().offload.gateway.guildMembersUrl!, guildIds)) return; } check.call(this, RequestGuildMembersSchema, d); diff --git a/src/gateway/util/Utils.ts b/src/gateway/util/Utils.ts index bbb054363..adc7bbeca 100644 --- a/src/gateway/util/Utils.ts +++ b/src/gateway/util/Utils.ts @@ -97,7 +97,7 @@ async function expireOldPresenceStates() { } } -export async function handleOffloadedGatewayRequest(socket: WebSocket, url: string, body: unknown) { +export async function handleOffloadedGatewayRequest(socket: WebSocket, url: string, body: unknown): Promise { // TODO: async json object streaming const resp = await fetch(url, { body: JSON.stringify(body), @@ -114,7 +114,8 @@ export async function handleOffloadedGatewayRequest(socket: WebSocket, url: stri const text = await resp.text(); console.error(`[Gateway] Offloaded request to ${url} failed with status ${resp.status}: ${text}`); if (resp.status === 415 || resp.status === 400) console.log(typeof body, body); - throw new Error(`Offloaded request failed with status ${resp.status}: ${text}`); + // throw new Error(`Offloaded request failed with status ${resp.status}: ${text}`); + return false; } const data = ((await resp.json()) as Event[]).toReversed(); @@ -128,4 +129,6 @@ export async function handleOffloadedGatewayRequest(socket: WebSocket, url: stri d: event.data, }); } + + return true; }