Gateway offload: gracefully fall back to TS code on failure

This commit is contained in:
Rory&
2026-06-23 09:02:32 +02:00
parent 1b45de55fd
commit 234403f7d5
6 changed files with 10 additions and 7 deletions
+1 -1
View File
@@ -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[];
+1 -1
View File
@@ -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) {
+1 -1
View File
@@ -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 = (
@@ -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
+1 -1
View File
@@ -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);
+5 -2
View File
@@ -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<boolean> {
// 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;
}