Prune closed connections before broadcasting
Node · Node · intermediate · modification
Simplifies pruneClosed: instead of allocating a filtered copy and rebuilding the array, it now walks the connections in place and splices out any socket that isn't OPEN. Same result, one less array allocation per broadcast.
connections is the live subscriber list of an in-process WebSocket hub; pruneClosed is called before each fan-out so we never write to a dead peer. Sockets close in bursts (a dropped client often takes its neighbours in the same tick), so adjacent closed entries are common.
Requirements
- pruneClosed(connections) removes EVERY connection whose socket.readyState is not OPEN, mutating the array in place.
- Connections whose socket is still OPEN must remain, in their original order.
- It runs before every broadcast, so no closed socket may survive into the fan-out.
Files touched
- src/realtime/connections.js
--- src/realtime/connections.js
// broadcast to a dead peer. Called before each fan-out.
function pruneClosed(connections) {
- const alive = connections.filter((conn) => conn.socket.readyState === OPEN);
- connections.length = 0;
- connections.push(...alive);
+ connections.forEach((conn, i) => {
+ if (conn.socket.readyState !== OPEN) {
+ connections.splice(i, 1);
+ }
+ });
}