From 8ba5b742231c3330c78378868fc66959285e5683 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sun, 3 Dec 2023 23:04:26 +0000 Subject: [PATCH] use one shared log reader client --- svc/web.mjs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/svc/web.mjs b/svc/web.mjs index 031af4121..13768bbfe 100644 --- a/svc/web.mjs +++ b/svc/web.mjs @@ -13,6 +13,7 @@ import cors from 'cors'; import bodyParser from 'body-parser'; import stripeLib from 'stripe'; import Redis from 'ioredis'; +import uuid from 'uuid'; import keys from '../routes/keyManagement.mjs'; import api from '../routes/api.mjs'; import queries from '../store/queries.mjs'; @@ -337,27 +338,23 @@ app.route('/manageSub').post(async (req, res) => { return res.json(session); }); app.use('/api', api); -let openClients = 0; -app.get('/logs', (req, res, cb) => { - // limit the number of max clients - if (openClients >= 100) { - return cb('too many log clients'); - } - const logSub = new Redis(config.REDIS_URL); - logSub.subscribe(['api', 'parsed', 'gcdata']); - openClients += 1; - // Create a subscriber client - logSub.on('message', (channel, message) => { +const logReaders = {}; +const logSub = new Redis(config.REDIS_URL); +logSub.subscribe(['api', 'parsed', 'gcdata']); +logSub.on('message', (channel, message) => { + // Emit it to all the connected logs + Object.values(logReaders).forEach(([req, res]) => { const matched = Array.isArray(req.query.channel) && req.query.channel.includes(channel); if (!req.query.channel || matched) { res.write(message + '\n'); } }); - // Teardown the subscriber on request close +}); +app.get('/logs', (req, res) => { + const id = uuid.v4(); + logReaders[id] = [req, res]; req.on('close', () => { - console.log('closing log client'); - logSub.disconnect(); - openClients -= 1; + delete logReaders[id]; }); }); // CORS Preflight for API keys