summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-08-16 14:43:30 +0200
committerRory& <root@rory.gay>2026-08-16 14:43:30 +0200
commit3ca0677b2628338422e57aa42a2a3b9286c8a335 (patch)
tree82cd307615dfea028d6394d545cbb044f1b99a38 /src
parentNo-op metrics endpoints (diff)
downloadserver-ts-3ca0677b2628338422e57aa42a2a3b9286c8a335.tar.xz
Dont fail the gateway connection if unable to connect to an offload endpoint
Diffstat (limited to 'src')
-rw-r--r--src/gateway/util/Utils.ts65
1 files changed, 35 insertions, 30 deletions
diff --git a/src/gateway/util/Utils.ts b/src/gateway/util/Utils.ts

index adc7bbec..cc710e57 100644 --- a/src/gateway/util/Utils.ts +++ b/src/gateway/util/Utils.ts
@@ -98,37 +98,42 @@ async function expireOldPresenceStates() { } 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), - method: "POST", - headers: { - Authorization: `Bearer ${socket.accessToken}`, - // because the session may not have an id in the token! - "X-Session-Id": socket.session_id, - "Content-Type": "application/json", - }, - }); + try { + // TODO: async json object streaming + const resp = await fetch(url, { + body: JSON.stringify(body), + method: "POST", + headers: { + Authorization: `Bearer ${socket.accessToken}`, + // because the session may not have an id in the token! + "X-Session-Id": socket.session_id, + "Content-Type": "application/json", + }, + }); - if (!resp.ok) { - 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}`); - return false; - } + if (!resp.ok) { + 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}`); + return false; + } - const data = ((await resp.json()) as Event[]).toReversed(); - while (data.length > 0) { - const event = data.pop()!; - if (process.env.WS_VERBOSE) console.log(`[Gateway] Received offloaded event: ${JSON.stringify(event)}`); - await Send(socket, { - op: OPCODES.Dispatch, - s: socket.sequence++, - t: event.event, - d: event.data, - }); - } + const data = ((await resp.json()) as Event[]).toReversed(); + while (data.length > 0) { + const event = data.pop()!; + if (process.env.WS_VERBOSE) console.log(`[Gateway] Received offloaded event: ${JSON.stringify(event)}`); + await Send(socket, { + op: OPCODES.Dispatch, + s: socket.sequence++, + t: event.event, + d: event.data, + }); + } - return true; + return true; + } catch (e) { + console.error("Error while handling offloaded gateway request:", e); + return false; + } }