Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ POSTGRES_DB=vrt_db_dev
# optional
#HTTPS_KEY_PATH='./secrets/ssl.key'
#HTTPS_CERT_PATH='./secrets/ssl.cert'
#SERVER_TIMEOUT=120000
#SERVER_HEADERS_TIMEOUT=60000
#SERVER_KEEP_ALIVE_TIMEOUT=5000
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CacheInterceptor, CacheModule, Module } from '@nestjs/common';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { UsersModule } from './users/users.module';
import { BuildsModule } from './builds/builds.module';
Expand Down Expand Up @@ -29,6 +30,7 @@ import { HealthController } from './health/health.controller';
CompareModule,
],
providers: [
AppService,
PrismaService,
{
provide: APP_FILTER,
Expand Down
25 changes: 25 additions & 0 deletions src/app.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppService } from './app.service';
import { ConfigService } from '@nestjs/config';

describe('AppService', () => {
let service: AppService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
AppService,
{
provide: ConfigService,
useValue: {},
},
],
}).compile();

service = module.get<AppService>(AppService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
25 changes: 25 additions & 0 deletions src/app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Inject, Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
import { ConfigService } from '@nestjs/config';
import { Server } from 'http';

@Injectable()
export class AppService implements OnApplicationBootstrap {
private readonly logger: Logger = new Logger(AppService.name);

constructor(
private readonly refHost: HttpAdapterHost<any>,
@Inject(ConfigService) private configService: ConfigService
) {}

onApplicationBootstrap() {
const server: Server = this.refHost.httpAdapter.getHttpServer();
const { timeout, headersTimeout, keepAliveTimeout } = server;
server.timeout = parseInt(this.configService.get('SERVER_TIMEOUT', timeout.toString()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you check it's working when variable is not set?
might need default value for them of avoid assigning if it's undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure
I'm currently running this with an image i build myself.

line 17 is reading the defaults from node server and the second argument of this.configService.get assigns the default again, when the variable is not set

log from my server, after restart:

[Nest] 19  - 03/16/2023, 8:51:43 AM     LOG [RouterExplorer] Mapped {/projects/:id, DELETE} route +1ms
[Nest] 19  - 03/16/2023, 8:51:44 AM     LOG [AppService] timeout=120000
[Nest] 19  - 03/16/2023, 8:51:44 AM     LOG [AppService] headersTimeout=60000
[Nest] 19  - 03/16/2023, 8:51:44 AM     LOG [AppService] keepAliveTimeout=181000
[Nest] 19  - 03/16/2023, 8:51:44 AM     LOG [NestApplication] Nest application successfully started +0ms

timeout and headersTimeout is the default from node https://nodejs.org/docs/latest-v12.x/api/http.html#http_server_headerstimeout

keepAliveTimeout is overwritten by environment variable SERVER_KEEP_ALIVE_TIMEOUT=181000

this.logger.log(`server.timeout is ${server.timeout}`);
server.headersTimeout = parseInt(this.configService.get('SERVER_HEADERS_TIMEOUT', headersTimeout.toString()));
this.logger.log(`server.headersTimeout is ${server.headersTimeout}`);
server.keepAliveTimeout = parseInt(this.configService.get('SERVER_KEEP_ALIVE_TIMEOUT', keepAliveTimeout.toString()));
this.logger.log(`server.keepAliveTimeout is ${server.keepAliveTimeout}`);
}
}