From 7ff824a0583b7518820fa9c1b1a5e66bc6ca7e22 Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Tue, 11 Mar 2025 21:21:32 +0700 Subject: [PATCH] Fix keep alive consistency for account + front (#8198) Signed-off-by: Andrey Sobolev --- pods/authProviders/src/index.ts | 8 ++++++- pods/front/package.json | 2 +- server/account-service/src/index.ts | 17 ++++++++++++--- server/collaborator/package.json | 2 +- server/front/package.json | 4 ++-- server/front/src/index.ts | 29 ++++++++++++++----------- server/ws/package.json | 2 +- services/github/pod-github/package.json | 2 +- services/rekoni/package.json | 2 +- 9 files changed, 44 insertions(+), 24 deletions(-) diff --git a/pods/authProviders/src/index.ts b/pods/authProviders/src/index.ts index 1cc29a4e89..2b8bd73d4c 100644 --- a/pods/authProviders/src/index.ts +++ b/pods/authProviders/src/index.ts @@ -70,6 +70,12 @@ export function registerProviders ( } router.get('providers', '/providers', (ctx) => { - ctx.body = JSON.stringify(res) + const json = JSON.stringify(res) + ctx.res.writeHead(200, { + 'Content-Type': 'application/json', + 'keep-alive': 'timeout=5, max=1000', + connection: 'keep-alive' + }) + ctx.res.end(json) }) } diff --git a/pods/front/package.json b/pods/front/package.json index 2caa6b72bf..7cc6185735 100644 --- a/pods/front/package.json +++ b/pods/front/package.json @@ -67,7 +67,7 @@ "@hcengineering/server-core": "^0.6.1", "@hcengineering/server-token": "^0.6.11", "@hcengineering/attachment": "^0.6.14", - "body-parser": "^1.20.2", + "body-parser": "^1.20.3", "sharp": "~0.32.0", "@hcengineering/analytics": "^0.6.0", "@hcengineering/analytics-service": "^0.6.0" diff --git a/server/account-service/src/index.ts b/server/account-service/src/index.ts index ac2839b768..f4be75501c 100644 --- a/server/account-service/src/index.ts +++ b/server/account-service/src/index.ts @@ -29,6 +29,12 @@ import { migrateFromOldAccounts } from './migration/migration' const AUTH_TOKEN_COOKIE = 'account-metadata-Token' +const KEEP_ALIVE_HEADERS = { + 'Content-Type': 'application/json', + Connection: 'keep-alive', + 'Keep-Alive': 'timeout=5, max=1000' +} + /** * @public */ @@ -218,7 +224,7 @@ export function serveAccount (measureCtx: MeasureContext, brandings: BrandingMap data.statistics.freeMem = Math.round((os.freemem() / 1024 / 1024) * 100) / 100 data.statistics.totalMem = Math.round((os.totalmem() / 1024 / 1024) * 100) / 100 const json = JSON.stringify(data) - req.res.writeHead(200, { 'Content-Type': 'application/json' }) + req.res.writeHead(200, KEEP_ALIVE_HEADERS) req.res.end(json) } catch (err: any) { Analytics.handleError(err) @@ -317,7 +323,10 @@ export function serveAccount (measureCtx: MeasureContext, brandings: BrandingMap error: new Status(Severity.ERROR, platform.status.UnknownMethod, { method: request.method }) } - ctx.body = JSON.stringify(response) + const body = JSON.stringify(response) + ctx.res.writeHead(404, KEEP_ALIVE_HEADERS) + ctx.res.end(body) + return } const [db] = await accountsDb @@ -343,7 +352,9 @@ export function serveAccount (measureCtx: MeasureContext, brandings: BrandingMap return method(mctx, db, branding, request, token) }) - ctx.body = result + const body = JSON.stringify(result) + ctx.res.writeHead(200, KEEP_ALIVE_HEADERS) + ctx.res.end(body) }) app.use(router.routes()).use(router.allowedMethods()) diff --git a/server/collaborator/package.json b/server/collaborator/package.json index fe70d150ac..746e2713b3 100644 --- a/server/collaborator/package.json +++ b/server/collaborator/package.json @@ -71,7 +71,7 @@ "mongodb": "^6.12.0", "yjs": "^13.6.23", "express": "^4.21.2", - "body-parser": "^1.20.2", + "body-parser": "^1.20.3", "cors": "^2.8.5", "ws": "^8.18.0" } diff --git a/server/front/package.json b/server/front/package.json index 959fd036dd..c59c7ab17b 100644 --- a/server/front/package.json +++ b/server/front/package.json @@ -51,7 +51,7 @@ "@hcengineering/platform": "^0.6.11", "express": "^4.21.2", "express-fileupload": "^1.5.1", - "express-static-gzip": "^2.1.7", + "express-static-gzip": "^2.2.0", "uuid": "^8.3.2", "cors": "^2.8.5", "@hcengineering/account-client": "^0.6.0", @@ -60,7 +60,7 @@ "@hcengineering/server-storage": "^0.6.0", "@hcengineering/server-token": "^0.6.11", "@hcengineering/attachment": "^0.6.14", - "body-parser": "^1.20.2", + "body-parser": "^1.20.3", "sharp": "~0.32.0", "@hcengineering/minio": "^0.6.0", "@hcengineering/mongo": "^0.6.1", diff --git a/server/front/src/index.ts b/server/front/src/index.ts index 7555e7e054..82af991264 100644 --- a/server/front/src/index.ts +++ b/server/front/src/index.ts @@ -39,6 +39,13 @@ import { tmpdir } from 'os' const cacheControlValue = 'public, no-cache, must-revalidate, max-age=365d' const cacheControlNoCache = 'public, no-store, no-cache, must-revalidate, max-age=0' +const KEEP_ALIVE_TIMEOUT = 5 // seconds +const KEEP_ALIVE_MAX = 1000 +const KEEP_ALIVE_HEADERS = { + Connection: 'keep-alive', + 'Keep-Alive': `timeout=${KEEP_ALIVE_TIMEOUT}, max=${KEEP_ALIVE_MAX}` +} + async function storageUpload ( ctx: MeasureContext, storageAdapter: StorageAdapter, @@ -109,8 +116,7 @@ async function getFileRange ( {} ) res.writeHead(206, { - Connection: 'keep-alive', - 'Keep-Alive': 'timeout=5', + ...KEEP_ALIVE_HEADERS, 'Content-Range': `bytes ${start}-${end}/${size}`, 'Accept-Ranges': 'bytes', 'Content-Length': end - start + 1, @@ -175,12 +181,11 @@ async function getFile ( ) { // Matched, return not modified res.writeHead(304, { + ...KEEP_ALIVE_HEADERS, 'content-type': stat.contentType, etag: stat.etag, 'last-modified': new Date(stat.modifiedOn).toISOString(), - 'cache-control': cacheControlValue, - connection: 'keep-alive', - 'keep-alive': 'timeout=5, max=1000' + 'cache-control': cacheControlValue }) res.end() return @@ -188,12 +193,11 @@ async function getFile ( if (preConditions.IfUnmodifiedSince(req.headers, { lastModified: new Date(stat.modifiedOn) }) === 'failed') { // Send 412 (Precondition Failed) res.writeHead(412, { + ...KEEP_ALIVE_HEADERS, 'content-type': stat.contentType, etag: stat.etag, 'last-modified': new Date(stat.modifiedOn).toISOString(), - 'cache-control': cacheControlValue, - connection: 'keep-alive', - 'keep-alive': 'timeout=5, max=1000' + 'cache-control': cacheControlValue }) res.end() return @@ -347,7 +351,7 @@ export function start ( res.status(200) res.set('Cache-Control', cacheControlNoCache) res.set('Connection', 'keep-alive') - res.set('Keep-Alive', 'timeout=5') + res.set('Keep-Alive', `timeout=${KEEP_ALIVE_TIMEOUT}, max=${KEEP_ALIVE_MAX}`) res.json(data) }) @@ -410,7 +414,7 @@ export function start ( res.setHeader('Cache-Control', cacheControlNoCache) } res.setHeader('Connection', 'keep-alive') - res.setHeader('Keep-Alive', 'timeout=5') + res.setHeader('Keep-Alive', `timeout=${KEEP_ALIVE_TIMEOUT}, max=${KEEP_ALIVE_MAX}`) } } }) @@ -849,9 +853,8 @@ export function start ( }) const server = app.listen(port) - - server.keepAliveTimeout = 60 * 1000 + 1000 - server.headersTimeout = 60 * 1000 + 2000 + server.keepAliveTimeout = KEEP_ALIVE_TIMEOUT * 1000 + 1000 + server.headersTimeout = KEEP_ALIVE_TIMEOUT * 1000 + 2000 return () => { server.close() } diff --git a/server/ws/package.json b/server/ws/package.json index 1a5f57e62e..cb96808839 100644 --- a/server/ws/package.json +++ b/server/ws/package.json @@ -53,7 +53,7 @@ "express": "^4.21.2", "utf-8-validate": "^6.0.4", "ws": "^8.18.0", - "body-parser": "^1.20.2", + "body-parser": "^1.20.3", "snappy": "^7.2.2", "@hcengineering/api-client": "^0.6.0", "morgan": "^1.10.0" diff --git a/services/github/pod-github/package.json b/services/github/pod-github/package.json index 3f65d24b2b..0f24385b26 100644 --- a/services/github/pod-github/package.json +++ b/services/github/pod-github/package.json @@ -113,7 +113,7 @@ "@hcengineering/task": "^0.6.20", "express": "^4.21.2", "cors": "^2.8.5", - "body-parser": "^1.20.2", + "body-parser": "^1.20.3", "mongodb": "^6.12.0", "@hcengineering/setting": "^0.6.17", "@hcengineering/preference": "^0.6.13", diff --git a/services/rekoni/package.json b/services/rekoni/package.json index ef1e4012b6..f7e16c452e 100644 --- a/services/rekoni/package.json +++ b/services/rekoni/package.json @@ -71,7 +71,7 @@ "@anticrm/skillset": "^0.6.0", "@hcengineering/core": "^0.6.32", "@types/email-addresses": "^3.0.0", - "body-parser": "^1.20.2", + "body-parser": "^1.20.3", "cors": "^2.8.5", "dotenv": "~16.0.0", "email-addresses": "^5.0.0",