admin/monitor: fewer updates by sending new connections when they are fully set
Massimo Melina committed
Aug 5, 2022 at 21:27 UTC
83dce4203b5bc70dea7af92799bc91ea9fbf56d2
4 files changed
+3
-9
server/src/connections.ts
+2
-1
@@ -21,7 +21,8 @@ export class Connection {
21
this.got += data.length )
22
socket.on('close', () => {
23
all.splice(all.indexOf(this), 1)
24
- events.emit('connectionClosed', this)
24
+ if (this.alreadyEmitted)
25
+ events.emit('connectionClosed', this)
26
})
27
events.emit('socket', socket)
28
}
server/src/middlewares.ts
+1
-1
@@ -127,9 +127,9 @@ export const prepareState: Koa.Middleware = async (ctx, next) => {
127
// calculate these once and for all
128
ctx.state.account = await getHttpAccount(ctx) ?? getAccount(getCurrentUsername(ctx))
129
const conn = ctx.state.connection = socket2connection(ctx.socket)
130
+ await next()
131
if (conn)
132
updateConnection(conn, { ctx })
132
- await next()
133
}
134
135
async function getHttpAccount(ctx: Koa.Context) {
server/src/serveFile.ts
-4
@@ -65,10 +65,6 @@ export function serveFile(source:string, mime?:string, content?: string | Buffer
65
ctx.status = 200
66
if (ctx.fresh)
67
return ctx.status = 304
68
-
69
- const conn = ctx.state.connection
70
- if (conn)
71
- updateConnection(conn, { ctx }) // fileSource is affecting connection's outputted data, so we request an update
68
if (content !== undefined)
69
return ctx.body = content
70
const range = getRange(ctx, stats.size)
server/src/zip.ts
-3
@@ -62,9 +62,6 @@ export async function zipStreamFromFolder(node: VfsNode, ctx: Koa.Context) {
62
ctx.body = zip
63
ctx.req.on('close', ()=> zip.destroy())
64
ctx.state.archive = 'zip'
65
- const conn = ctx.state.connection
66
- if (conn)
67
- updateConnection(conn, { ctx }) // update connection data
65
}
66
67
const zipSeconds = defineConfig('zip_calculate_size_for_seconds', 1)