From 4429c98215a3e3956edd41eb445b7fa0aa565a0a Mon Sep 17 00:00:00 2001 From: Val Packett Date: Wed, 9 Jul 2025 04:00:23 -0300 Subject: [PATCH 1/2] bridge: support systemd socket activation Instead of opening a new port, inherit a socket from the parent process when told to, so that systemd could spawn us only when someone actually connects. --- src/bridge.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/bridge.js b/src/bridge.js index 55a9e77..274a2c6 100644 --- a/src/bridge.js +++ b/src/bridge.js @@ -17,6 +17,13 @@ if (process.env.ARRPC_BRIDGE_PORT) { throw new Error('invalid port'); } } +if (process.env.LISTEN_FDS) { // Socket activation + const fds = parseInt(process.env.LISTEN_FDS); + if (fds !== 1) { + throw new Error(`got ${fds} LISTEN_FDS, expecting 1`); + } + port = { fd: 3 }; +} const wss = new WebSocketServer({ port }); wss.on('connection', socket => { From e9d994995480babf3b91791775577618706bd82e Mon Sep 17 00:00:00 2001 From: Val Packett Date: Wed, 9 Jul 2025 04:10:09 -0300 Subject: [PATCH 2/2] bridge: automatically deactivate on a timeout with socket activation Allow saving memory by not running when Discord is not open. --- src/bridge.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/bridge.js b/src/bridge.js index 274a2c6..7999d05 100644 --- a/src/bridge.js +++ b/src/bridge.js @@ -25,9 +25,15 @@ if (process.env.LISTEN_FDS) { // Socket activation port = { fd: 3 }; } const wss = new WebSocketServer({ port }); +let quitTimer; wss.on('connection', socket => { log('web connected'); + if (quitTimer) { + log('client reconnected, not deactivating') + clearTimeout(quitTimer); + quitTimer = undefined; + } for (const id in lastMsg) { // catch up newly connected if (lastMsg[id].activity != null) send(lastMsg[id]); @@ -35,6 +41,10 @@ wss.on('connection', socket => { socket.on('close', () => { log('web disconnected'); + if (process.env.LISTEN_FDS && wss.clients.size === 0) { + log('last client disconnected, will deactivate in 30s'); + quitTimer = setTimeout(() => process.exit(0), 30000); + } }); });