mirror of
https://github.com/spacebarchat/server.git
synced 2026-08-21 22:59:52 +00:00
Gateway offload: gracefully fall back to TS code on failure
This commit is contained in:
@@ -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[];
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user