Skip to content

Commit

Permalink
add saverooms logging
Browse files Browse the repository at this point in the history
  • Loading branch information
howardchung committed Feb 7, 2025
1 parent 5c611af commit dd3183d
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,27 +630,39 @@ app.use('/*splat', (_req, res) => {
);
});

function saveRooms() {
async function saveRooms() {
// Unload rooms that are empty and idle
// Frees up some JS memory space when process is long-running
// On reconnect, we'll attempt to reload the room
rooms.forEach(async (room, key) => {
if (
room.roster.length === 0 &&
!room.vBrowser &&
Number(room.lastUpdateTime) < Date.now() - 24 * 60 * 60 * 1000
) {
console.log('freeing room %s from memory on shard %s', key, config.SHARD);
await room.saveRoom();
room.destroy();
rooms.delete(key);
// Unregister the namespace to avoid dupes on reload
io._nsps.delete(key);
} else if (room.roster.length) {
room.lastUpdateTime = new Date();
await room.saveRoom();
}
});
let saveCount = 0;
const start = Date.now();
await Promise.all(
Array.from(rooms.entries()).map(async ([key, room]) => {
if (
room.roster.length === 0 &&
!room.vBrowser &&
Number(room.lastUpdateTime) < Date.now() - 24 * 60 * 60 * 1000
) {
console.log(
'freeing room %s from memory on shard %s',
key,
config.SHARD,
);
await room.saveRoom();
room.destroy();
rooms.delete(key);
saveCount += 1;
// Unregister the namespace to avoid dupes on reload
io._nsps.delete(key);
} else if (room.roster.length) {
room.lastUpdateTime = new Date();
await room.saveRoom();
saveCount += 1;
}
}),
);
const end = Date.now();
console.log('[SAVEROOMS] %s saved in %sms', saveCount, end - start);
}

async function release() {
Expand Down

0 comments on commit dd3183d

Please sign in to comment.