Log gateway handling time

This commit is contained in:
Rory& 2025-07-06 19:14:27 +02:00
parent 1ee84431e4
commit 02efa17a04
9 changed files with 47 additions and 3 deletions

View File

@ -7,6 +7,7 @@ export async function onGuildSubscriptionsBulk(
this: WebSocket,
payload: Payload,
) {
const startTime = Date.now();
check.call(this, GuildSubscriptionsBulkSchema, payload.d);
const body = payload.d as GuildSubscriptionsBulkSchema;
@ -21,4 +22,7 @@ export async function onGuildSubscriptionsBulk(
},
});
}
console.log(
`[Gateway] GuildSubscriptionsBulk processed ${Object.keys(body.subscriptions).length} subscriptions for user ${this.user_id} in ${Date.now() - startTime}ms`,
);
}

View File

@ -71,6 +71,7 @@ const tryGetUserFromToken = async (...args: Parameters<typeof checkToken>) => {
};
export async function onIdentify(this: WebSocket, data: Payload) {
const startTime = Date.now();
if (this.user_id) {
// we've already identified
return this.close(CLOSECODES.Already_authenticated);
@ -521,4 +522,8 @@ export async function onIdentify(this: WebSocket, data: Payload) {
//TODO send VOICE_STATE_UPDATE to let the client know if another device is already connected to a voice channel
await setupListener.call(this);
console.log(
`[Gateway] IDENTIFY ${this.user_id} in ${Date.now() - startTime}ms`,
);
}

View File

@ -212,6 +212,7 @@ async function subscribeToMemberEvents(this: WebSocket, user_id: string) {
}
export async function onLazyRequest(this: WebSocket, { d }: Payload) {
const startTime = Date.now();
// TODO: check data
check.call(this, LazyRequestSchema, d);
const { guild_id, typing, channels, activities, members } =
@ -308,7 +309,7 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
.flat()
.unique();
return await Send(this, {
await Send(this, {
op: OPCODES.Dispatch,
s: this.sequence++,
t: "GUILD_MEMBER_LIST_UPDATE",
@ -327,4 +328,8 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
groups,
},
});
console.log(
`[Gateway] LAZY_REQUEST ${guild_id} ${channel_id} took ${Date.now() - startTime}ms`,
);
}

View File

@ -27,6 +27,7 @@ import {
import { check } from "./instanceOf";
export async function onPresenceUpdate(this: WebSocket, { d }: Payload) {
const startTime = Date.now();
check.call(this, ActivitySchema, d);
const presence = d as ActivitySchema;
@ -50,4 +51,8 @@ export async function onPresenceUpdate(this: WebSocket, { d }: Payload) {
client_status: session.client_status,
},
} as PresenceUpdateEvent);
console.log(
`Presence update for user ${this.user_id} processed in ${Date.now() - startTime}ms`,
);
}

View File

@ -30,6 +30,7 @@ import { check } from "./instanceOf";
import { FindManyOptions, ILike, In } from "typeorm";
export async function onRequestGuildMembers(this: WebSocket, { d }: Payload) {
const startTime = Date.now();
// Schema validation can only accept either string or array, so transforming it here to support both
if (!d.guild_id) throw new Error('"guild_id" is required');
d.guild_id = Array.isArray(d.guild_id) ? d.guild_id[0] : d.guild_id;
@ -52,12 +53,12 @@ export async function onRequestGuildMembers(this: WebSocket, { d }: Payload) {
user_ids = user_ids as string[] | undefined;
if (d.query && (!limit || Number.isNaN(limit))) {
console.log("Query:", d)
console.log("Query:", d);
throw new Error('"query" requires "limit" to be set');
}
if (d.query && user_ids) {
console.log("Query:", d)
console.log("Query:", d);
throw new Error('"query" and "user_ids" are mutually exclusive');
}
@ -200,4 +201,8 @@ export async function onRequestGuildMembers(this: WebSocket, { d }: Payload) {
d: chunk,
});
});
console.log(
`[Gateway] REQUEST_GUILD_MEMBERS took ${Date.now() - startTime}ms for guild ${guild_id} with ${members.length} members`,
);
}

View File

@ -22,6 +22,7 @@ import {
import { check } from "./instanceOf";
export async function onStreamCreate(this: WebSocket, data: Payload) {
const startTime = Date.now();
check.call(this, StreamCreateSchema, data.d);
const body = data.d as StreamCreateSchema;
@ -124,6 +125,10 @@ export async function onStreamCreate(this: WebSocket, data: Payload) {
guild_id: voiceState.guild_id,
channel_id: voiceState.channel_id,
} as VoiceStateUpdateEvent);
console.log(
`[Gateway] STREAM_CREATE for user ${this.user_id} in channel ${body.channel_id} with stream key ${streamKey} in ${Date.now() - startTime}ms`,
);
}
//stream key:

View File

@ -10,6 +10,7 @@ import {
import { check } from "./instanceOf";
export async function onStreamDelete(this: WebSocket, data: Payload) {
const startTime = Date.now();
check.call(this, StreamDeleteSchema, data.d);
const body = data.d as StreamDeleteSchema;
@ -73,4 +74,8 @@ export async function onStreamDelete(this: WebSocket, data: Payload) {
guild_id: guildId,
channel_id: channelId,
} as StreamDeleteEvent);
console.log(
`[Gateway] STREAM_DELETE for user ${this.user_id} in channel ${channelId} with stream key ${body.stream_key} in ${Date.now() - startTime}ms`,
);
}

View File

@ -17,6 +17,7 @@ import { check } from "./instanceOf";
import { Not } from "typeorm";
export async function onStreamWatch(this: WebSocket, data: Payload) {
const startTime = Date.now();
check.call(this, StreamWatchSchema, data.d);
const body = data.d as StreamWatchSchema;
@ -95,4 +96,8 @@ export async function onStreamWatch(this: WebSocket, data: Payload) {
},
user_id: this.user_id,
} as StreamServerUpdateEvent);
console.log(
`[Gateway] STREAM_WATCH for user ${this.user_id} in channel ${channelId} with stream key ${body.stream_key} in ${Date.now() - startTime}ms`,
);
}

View File

@ -37,6 +37,7 @@ import { check } from "./instanceOf";
// Having MANAGE_CHANNELS permission bypasses this limit and allows you to join regardless of the channel being full or not.
export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
const startTime = Date.now();
check.call(this, VoiceStateUpdateSchema, data.d);
const body = data.d as VoiceStateUpdateSchema;
const isNew = body.channel_id === null && body.guild_id === null;
@ -167,4 +168,8 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
user_id: voiceState.user_id,
} as VoiceServerUpdateEvent);
}
console.log(
`[Gateway] VOICE_STATE_UPDATE for user ${this.user_id} in channel ${voiceState.channel_id} in guild ${voiceState.guild_id} in ${Date.now() - startTime}ms`,
);
}