Skip to content

Commit 441486f

Browse files
authored
add capability to configure server timeouts (#198)
* add capability to configure server timeouts --------- Co-authored-by: Mario Melcher <mario.melcher@seitenbau.com>
1 parent 9720342 commit 441486f

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ POSTGRES_DB=vrt_db_dev
1515
# optional
1616
#HTTPS_KEY_PATH='./secrets/ssl.key'
1717
#HTTPS_CERT_PATH='./secrets/ssl.cert'
18+
#SERVER_TIMEOUT=120000
19+
#SERVER_HEADERS_TIMEOUT=60000
20+
#SERVER_KEEP_ALIVE_TIMEOUT=5000

src/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CacheInterceptor, CacheModule, Module } from '@nestjs/common';
2+
import { AppService } from './app.service';
23
import { AuthModule } from './auth/auth.module';
34
import { UsersModule } from './users/users.module';
45
import { BuildsModule } from './builds/builds.module';
@@ -29,6 +30,7 @@ import { HealthController } from './health/health.controller';
2930
CompareModule,
3031
],
3132
providers: [
33+
AppService,
3234
PrismaService,
3335
{
3436
provide: APP_FILTER,

src/app.service.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { AppService } from './app.service';
3+
import { ConfigService } from '@nestjs/config';
4+
5+
describe('AppService', () => {
6+
let service: AppService;
7+
8+
beforeEach(async () => {
9+
const module: TestingModule = await Test.createTestingModule({
10+
providers: [
11+
AppService,
12+
{
13+
provide: ConfigService,
14+
useValue: {},
15+
},
16+
],
17+
}).compile();
18+
19+
service = module.get<AppService>(AppService);
20+
});
21+
22+
it('should be defined', () => {
23+
expect(service).toBeDefined();
24+
});
25+
});

src/app.service.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Inject, Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
2+
import { HttpAdapterHost } from '@nestjs/core';
3+
import { ConfigService } from '@nestjs/config';
4+
import { Server } from 'http';
5+
6+
@Injectable()
7+
export class AppService implements OnApplicationBootstrap {
8+
private readonly logger: Logger = new Logger(AppService.name);
9+
10+
constructor(
11+
private readonly refHost: HttpAdapterHost<any>,
12+
@Inject(ConfigService) private configService: ConfigService
13+
) {}
14+
15+
onApplicationBootstrap() {
16+
const server: Server = this.refHost.httpAdapter.getHttpServer();
17+
const { timeout, headersTimeout, keepAliveTimeout } = server;
18+
server.timeout = parseInt(this.configService.get('SERVER_TIMEOUT', timeout.toString()));
19+
this.logger.log(`server.timeout is ${server.timeout}`);
20+
server.headersTimeout = parseInt(this.configService.get('SERVER_HEADERS_TIMEOUT', headersTimeout.toString()));
21+
this.logger.log(`server.headersTimeout is ${server.headersTimeout}`);
22+
server.keepAliveTimeout = parseInt(this.configService.get('SERVER_KEEP_ALIVE_TIMEOUT', keepAliveTimeout.toString()));
23+
this.logger.log(`server.keepAliveTimeout is ${server.keepAliveTimeout}`);
24+
}
25+
}

0 commit comments

Comments
 (0)