From cc1383d87692dcf274a2471f83f73788479d1ad6 Mon Sep 17 00:00:00 2001 From: tudor <7089284+tudddorrr@users.noreply.github.com> Date: Mon, 1 Dec 2025 01:03:32 +0000 Subject: [PATCH 1/3] reduce db calls and serialisation overheads for socket messages --- src/entities/player.ts | 4 ++-- src/socket/listeners/gameChannelListeners.ts | 2 +- src/socket/messages/socketMessage.ts | 4 +++- src/socket/socketConnection.ts | 7 ++----- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/entities/player.ts b/src/entities/player.ts index 3c3baf8a..26592efb 100644 --- a/src/entities/player.ts +++ b/src/entities/player.ts @@ -156,11 +156,11 @@ export default class Player { await em.flush() - const conns = await socket.findConnectionsAsync(async (conn) => { + const conns = socket.findConnections((conn) => { return ( conn.hasScope(APIKeyScope.READ_PLAYER_PRESENCE) && !!conn.playerAliasId && - this.game.id === (await conn.getPlayerAlias())?.player.game.id + this.game.id === conn.game.id ) }) await sendMessages(conns, 'v1.players.presence.updated', { diff --git a/src/socket/listeners/gameChannelListeners.ts b/src/socket/listeners/gameChannelListeners.ts index 5b72cc4b..506494a4 100644 --- a/src/socket/listeners/gameChannelListeners.ts +++ b/src/socket/listeners/gameChannelListeners.ts @@ -23,7 +23,7 @@ const gameChannelListeners = [ id: data.channel.id, game: conn.game }, { - populate: ['members'] + populate: ['members:ref'] }) if (!channel) { diff --git a/src/socket/messages/socketMessage.ts b/src/socket/messages/socketMessage.ts index 12aeffef..b133ab64 100644 --- a/src/socket/messages/socketMessage.ts +++ b/src/socket/messages/socketMessage.ts @@ -31,7 +31,9 @@ export async function sendMessage(conn: SocketConnection, res: export async function sendMessages(conns: SocketConnection[], type: SocketMessageResponse, data: T) { await getSocketTracer().startActiveSpan('socket.send_many_messages', async (span) => { - await Promise.all(conns.map((conn) => conn.sendMessage(type, data))) + // serialise once instead of per-connection to reduce memory usage + const message = JSON.stringify({ res: type, data }) + await Promise.all(conns.map((conn) => conn.sendMessage(type, data, message))) span.end() }) } diff --git a/src/socket/socketConnection.ts b/src/socket/socketConnection.ts index fafd6a40..548a31f1 100644 --- a/src/socket/socketConnection.ts +++ b/src/socket/socketConnection.ts @@ -81,14 +81,11 @@ export default class SocketConnection { return this.ticket.devBuild } - async sendMessage(res: SocketMessageResponse, data: T): Promise { + async sendMessage(res: SocketMessageResponse, data: T, serialisedMessage?: string): Promise { await getSocketTracer().startActiveSpan('socket.send_message', async (span) => { if (this.ws.readyState === this.ws.OPEN) { const devBuild = this.isDevBuild() - const message = JSON.stringify({ - res, - data - }) + const message = serialisedMessage ?? JSON.stringify({ res, data }) setTraceAttributes({ 'socket.message_receiver.alias_id': this.playerAliasId, From 550f86dc846dc9c9e91a533c1958a375f082f73e Mon Sep 17 00:00:00 2001 From: tudor <7089284+tudddorrr@users.noreply.github.com> Date: Mon, 1 Dec 2025 01:11:39 +0000 Subject: [PATCH 2/3] remove unused findConnectionsAsync --- src/socket/index.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/socket/index.ts b/src/socket/index.ts index 9a76b9f8..3a7d5b2f 100644 --- a/src/socket/index.ts +++ b/src/socket/index.ts @@ -182,17 +182,6 @@ export default class Socket { return Array.from(this.connections.values()).filter(filter) } - async findConnectionsAsync(filter: (conn: SocketConnection) => Promise): Promise { - const connections = Array.from(this.connections.values()) - const results = await Promise.all( - connections.map(async (conn) => ({ - conn, - matches: await filter(conn) - })) - ) - return results.filter((r) => r.matches).map((r) => r.conn) - } - async trackEvent(data: Omit): Promise { if (process.env.DISABLE_SOCKET_EVENTS === '1') { return From 8be90f32ab6b77506e92e683e234fff4b10dea67 Mon Sep 17 00:00:00 2001 From: tudor <7089284+tudddorrr@users.noreply.github.com> Date: Mon, 1 Dec 2025 01:13:54 +0000 Subject: [PATCH 3/3] ensure span is always ended --- src/socket/messages/socketMessage.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/socket/messages/socketMessage.ts b/src/socket/messages/socketMessage.ts index b133ab64..2ad88c4b 100644 --- a/src/socket/messages/socketMessage.ts +++ b/src/socket/messages/socketMessage.ts @@ -31,9 +31,11 @@ export async function sendMessage(conn: SocketConnection, res: export async function sendMessages(conns: SocketConnection[], type: SocketMessageResponse, data: T) { await getSocketTracer().startActiveSpan('socket.send_many_messages', async (span) => { - // serialise once instead of per-connection to reduce memory usage - const message = JSON.stringify({ res: type, data }) - await Promise.all(conns.map((conn) => conn.sendMessage(type, data, message))) - span.end() + try { + const message = JSON.stringify({ res: type, data }) + await Promise.all(conns.map((conn) => conn.sendMessage(type, data, message))) + } finally { + span.end() + } }) }