From 1256711bcc1a8e7b11b156d66a5444d47f2a06f6 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Thu, 16 Mar 2023 12:10:11 +0800 Subject: [PATCH] refactor(core): set server timeout --- packages/core/src/app/init.test.ts | 6 +++++- packages/core/src/app/init.ts | 10 ++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/core/src/app/init.test.ts b/packages/core/src/app/init.test.ts index d8400f475..83aeb06b6 100644 --- a/packages/core/src/app/init.test.ts +++ b/packages/core/src/app/init.test.ts @@ -1,3 +1,5 @@ +import { createServer } from 'http'; + import { pickDefault } from '@logto/shared/esm'; import Koa from 'koa'; @@ -7,7 +9,9 @@ const initI18n = await pickDefault(import('../i18n/init.js')); const initApp = await pickDefault(import('./init.js')); describe('App Init', () => { - const listenMock = jest.spyOn(Koa.prototype, 'listen').mockImplementation(jest.fn()); + const listenMock = jest + .spyOn(Koa.prototype, 'listen') + .mockImplementation(jest.fn(() => createServer())); it('app init properly with 404 not found route', async () => { const app = new Koa(); diff --git a/packages/core/src/app/init.ts b/packages/core/src/app/init.ts index 52c9878a7..7a9082bd8 100644 --- a/packages/core/src/app/init.ts +++ b/packages/core/src/app/init.ts @@ -17,6 +17,8 @@ const logListening = (type: 'core' | 'admin' = 'core') => { } }; +const serverTimeout = 120_000; + const getTenant = async (tenantId: string) => { try { return await tenantPool.get(tenantId); @@ -76,6 +78,7 @@ export default async function initApp(app: Koa): Promise { coreServer.listen(urlSet.port, () => { logListening(); }); + coreServer.setTimeout(serverTimeout); // Create another server if admin localhost enabled if (!adminUrlSet.isLocalhostDisabled) { @@ -83,20 +86,23 @@ export default async function initApp(app: Koa): Promise { adminServer.listen(adminUrlSet.port, () => { logListening('admin'); }); + adminServer.setTimeout(serverTimeout); } return; } // Chrome doesn't allow insecure HTTP/2 servers, stick with HTTP for localhost. - app.listen(urlSet.port, () => { + const coreServer = app.listen(urlSet.port, () => { logListening(); }); + coreServer.setTimeout(serverTimeout); // Create another server if admin localhost enabled if (!adminUrlSet.isLocalhostDisabled) { - app.listen(adminUrlSet.port, () => { + const adminServer = app.listen(adminUrlSet.port, () => { logListening('admin'); }); + adminServer.setTimeout(serverTimeout); } }