nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.97k stars 7.66k forks source link

Using keepAliveTimeout and headersTimeout properties with Nest.js #12183

Closed canbalkaya closed 1 year ago

canbalkaya commented 1 year ago

Is there an existing issue that is already proposing this?

Is your feature request related to a problem? Please describe it

I want to increase my keepAliveTimeout and headersTimeout properties of my Nest.js app.

Right now, I define these properties like that but I don't know it's a good approach or it's actually working:

import { NestFactory } from '@nestjs/core' import { AppModule } from './app.module' import { json, urlencoded } from "body-parser"

async function bootstrap() { const app = await NestFactory.create(AppModule); const port = process.env.PORT || 3000; app.enableCors(); app.use(json({ limit: "20mb" })); app.use(urlencoded({ extended: true, limit: "20mb" }));

const expressApp = app.getHttpAdapter().getInstance(); expressApp.keepAliveTimeout = 600 1000; expressApp.headersTimeout = 610 1000;

await app.listen(port); } bootstrap();

Describe the solution you'd like

We need to define these properties (keepAliveTimeout and headersTimeout) without using Express.

Teachability, documentation, adoption, migration strategy

You can learn more about keepAliveTimeout parameter here: https://www.geeksforgeeks.org/node-js-http-server-keepalivetimeout-property/

You can learn more about headersTimeout parameter here: https://www.geeksforgeeks.org/node-js-http-server-headerstimeout-method/

What is the motivation / use case for changing the behavior?

I'm having problem with my Elastic Beanstalk server. So, I'm searching solutions from backend and devops side.

At least, anyone can say "This is OK. You can keep using this code." This is OK for me too. Right now, I'm not confident the code that I provided.

micalevisk commented 1 year ago
const server = await app.listen(3000)
server.setTimeout(2 * 60 * 1000)
server.keepAliveTimeout = 30000
server.headersTimeout = 31000

should work.


If we would introduce some abstraction to this (which I don't think is needed as far as it works for any HTTP adapter), I'd suggest a options arg for app.listen)

micalevisk commented 7 months ago

I saw that the above approach didn't work as expected. Instead, you should do those changes before calling app.listen, so:

// ...
const server = app.getHttpServer()
server.setTimeout(2 * 60 * 1000)
server.keepAliveTimeout = 30000
server.headersTimeout = 31000
await app.listen(3000)