admin/monitor: limit number of updates to avoid clogging

Massimo Melina committed Aug 5, 2022 at 20:44 UTC 8627e4744bff0e1fecac50d30072a8da4510fd78
1 file changed +12 -6
server/src/api.monitor.ts
+12 -6
@@ -19,20 +19,26 @@ const apis: ApiHandlers = {
19
20 get_connections({}, ctx) {
21 const list = new SendListReadable( getConnections().map(c => serializeConnection(c)) )
22 + type Change = Partial<Omit<Connection,'ip'>>
23 + const throttledUpdate = _.throttle(update, 1000/20) // try to avoid clogging with updates
24 return list.events(ctx, {
25 connection: conn => list.add(serializeConnection(conn)),
26 connectionClosed(conn: Connection) {
27 list.remove(serializeConnection(conn, true))
28 },
27 - connectionUpdated(conn: Connection, change: Partial<Omit<Connection,'ip'>>) {
28 - if (change.ctx) {
29 - Object.assign(change, fromCtx(change.ctx))
30 - delete change.ctx
31 - }
32 - list.update(serializeConnection(conn, true), change)
29 + connectionUpdated(conn: Connection, change: Change) {
30 + if (!change.ctx)
31 + return throttledUpdate(conn, change)
32 + Object.assign(change, fromCtx(change.ctx))
33 + delete change.ctx
34 + throttledUpdate(conn, change)
35 },
36 })
37
38 + function update(conn: Connection, change: Change) {
39 + list.update(serializeConnection(conn, true), change)
40 + }
41 +
42 function serializeConnection(conn: Connection, minimal?:true) {
43 const { socket, started, secure, got } = conn
44 return Object.assign(getConnAddress(conn), !minimal && {