refactor: improve socket session finding reliability
Add retry mechanism when finding session sockets to handle race conditions. The function now makes up to 3 attempts with 500ms delays before failing, which should improve reliability when sessions are connecting or reconnecting. Also includes code style reformatting for consistency. Signed-off-by: rjshrjndrn <rjshrjndrn@gmail.com>
This commit is contained in:
parent
f4bf1b8960
commit
508c87725f
2 changed files with 195 additions and 180 deletions
|
|
@ -7,6 +7,8 @@ ENV ENTERPRISE_BUILD=${envarg} \
|
|||
MAXMINDDB_FILE=/home/openreplay/geoip.mmdb \
|
||||
PRIVATE_ENDPOINTS=false \
|
||||
LISTEN_PORT=9001 \
|
||||
MAX_RETRIES=3 \
|
||||
RETRY_DELAY=500 \
|
||||
NODE_ENV=production
|
||||
WORKDIR /work
|
||||
COPY package.json .
|
||||
|
|
|
|||
|
|
@ -213,7 +213,20 @@ async function onAny(socket, eventName, ...args) {
|
|||
handleEvent(eventName, socket, args[0]);
|
||||
logger.debug(`received event:${eventName}, from:${socket.handshake.query.identity}, sending message to session of room:${socket.handshake.query.roomId}`);
|
||||
const io = getServer();
|
||||
let socketId = await findSessionSocketId(io, socket.handshake.query.roomId, args[0]?.meta?.tabId);
|
||||
const MAX_RETRIES = parseInt(process.env.MAX_RETRIES || 3);
|
||||
const RETRY_DELAY = parseInt(process.env.RETRY_DELAY || 500); // ms
|
||||
|
||||
async function findSessionWithRetry(io, roomId, tabId, retries = 0) {
|
||||
let socketId = await findSessionSocketId(io, socket.handshake.query.roomId, tabId);
|
||||
if (socketId === null && retries < MAX_RETRIES) {
|
||||
logger.debug(`Session not found, retry ${retries + 1}/${MAX_RETRIES}`);
|
||||
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
|
||||
return findSessionWithRetry(io, roomId, tabId, retries + 1);
|
||||
}
|
||||
return socketId;
|
||||
}
|
||||
|
||||
let socketId = await findSessionWithRetry(io, socket.handshake.query.roomId, args[0]?.meta?.tabId);
|
||||
if (socketId === null) {
|
||||
logger.debug(`session not found for:${socket.handshake.query.roomId}`);
|
||||
io.to(socket.id).emit(EVENTS_DEFINITION.emit.NO_SESSIONS);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue