Skip to content

Commit

Permalink
Tweak guest counting
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Dec 7, 2024
1 parent 3dd100b commit 335c520
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
35 changes: 19 additions & 16 deletions src/SocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import lodash from 'lodash';
import sjson from 'secure-json-parse';
import { WebSocketServer } from 'ws';
import Ajv from 'ajv';
import ms from 'ms';
import { stdSerializers } from 'pino';
import { socketVote } from './controllers/booth.js';
import { disconnectUser } from './controllers/users.js';
Expand All @@ -13,10 +12,13 @@ import AuthedConnection from './sockets/AuthedConnection.js';
import LostConnection from './sockets/LostConnection.js';
import { serializeUser } from './utils/serialize.js';

const { debounce, isEmpty } = lodash;
const { isEmpty } = lodash;

export const REDIS_ACTIVE_SESSIONS = 'users';

const PING_INTERVAL = 10_000;
const GUEST_COUNT_INTERVAL = 2_000;

/**
* @typedef {import('./schema.js').User} User
*/
Expand Down Expand Up @@ -102,10 +104,10 @@ class SocketServer {

#pinger;

/**
* Update online guests count and broadcast an update if necessary.
*/
#recountGuests;
/** Update online guests count and broadcast an update if necessary. */
#guestCountInterval;

#guestCountDirty = true;

/**
* Handlers for commands that come in from clients.
Expand Down Expand Up @@ -187,16 +189,17 @@ class SocketServer {

this.#pinger = setInterval(() => {
this.ping();
}, ms('10 seconds'));
}, PING_INTERVAL);

this.#recountGuests = debounce(() => {
if (this.#closing) {
this.#guestCountInterval = setInterval(() => {
if (!this.#guestCountDirty) {
return;
}
this.#recountGuestsInternal().catch((error) => {

this.#recountGuests().catch((error) => {
this.#logger.error({ err: error }, 'counting guests failed');
});
}, ms('2 seconds'));
}, GUEST_COUNT_INTERVAL);

this.#clientActions = {
sendChat: (user, message) => {
Expand Down Expand Up @@ -629,7 +632,7 @@ class SocketServer {
this.#logger.trace({ type: connection.constructor.name, userID, sessionID }, 'add connection');

this.#connections.push(connection);
this.#recountGuests();
this.#guestCountDirty = true;
}

/**
Expand All @@ -647,7 +650,7 @@ class SocketServer {
this.#connections.splice(i, 1);

connection.removed();
this.#recountGuests();
this.#guestCountDirty = true;
}

/**
Expand Down Expand Up @@ -703,12 +706,12 @@ class SocketServer {
clearInterval(this.#pinger);

this.#closing = true;
clearInterval(this.#guestCountInterval);

for (const connection of this.#connections) {
connection.close();
}

this.#recountGuests.cancel();

const closeWsServer = promisify(this.#wss.close.bind(this.#wss));
await closeWsServer();
await this.#redisSubscription.quit();
Expand Down Expand Up @@ -779,7 +782,7 @@ class SocketServer {
return parseInt(rawCount, 10);
}

async #recountGuestsInternal() {
async #recountGuests() {
const { redis } = this.#uw;
const guests = this.#connections
.filter((connection) => connection instanceof GuestConnection)
Expand Down
27 changes: 11 additions & 16 deletions src/sockets/LostConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import EventEmitter from 'node:events';
class LostConnection extends EventEmitter {
#logger;

#removeTimer;

/**
* @param {import('../Uwave.js').default} uw
* @param {import('../schema.js').User} user
Expand All @@ -19,7 +21,11 @@ class LostConnection extends EventEmitter {
});

this.initQueued();
this.setTimeout(timeout);

this.#removeTimer = setTimeout(() => {
this.close();
this.uw.redis.del(this.key, this.messagesKey);
}, timeout * 1000);
}

/**
Expand Down Expand Up @@ -52,17 +58,6 @@ class LostConnection extends EventEmitter {
.exec();
}

/**
* @param {number} timeout
* @private
*/
setTimeout(timeout) {
this.removeTimer = setTimeout(() => {
this.close();
this.uw.redis.del(this.key, this.messagesKey);
}, timeout * 1000);
}

/**
* @param {string} command
* @param {import('type-fest').JsonValue} data
Expand All @@ -78,13 +73,13 @@ class LostConnection extends EventEmitter {

close() {
this.#logger.info('close');
this.emit('close');
queueMicrotask(() => {
this.emit('close');
});
}

removed() {
if (this.removeTimer) {
clearTimeout(this.removeTimer);
}
clearTimeout(this.#removeTimer);
}

toString() {
Expand Down

0 comments on commit 335c520

Please sign in to comment.