From a07c8381f0f31827b34507df53e54fc86cf76cff Mon Sep 17 00:00:00 2001 From: Sairam Charan Date: Mon, 3 Jun 2024 11:51:52 +0530 Subject: [PATCH 1/4] fix: WEB-2246 added pool size option mongoose to handle memory --- example.env | 1 + example.rep.env | 1 + src/app.module.ts | 3 ++- src/common/DatabaseService.ts | 13 +++++++++++++ src/config/config.service.ts | 3 +++ 5 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/common/DatabaseService.ts diff --git a/example.env b/example.env index edb14adec..a4e63005c 100644 --- a/example.env +++ b/example.env @@ -8,6 +8,7 @@ MONGOOSE_HOST = mongo1 MONGOOSE_PORT = 27017 MONGOOSE_USERNAME MONGOOSE_PASSWORD +MONGOOSE_POOL_SIZE = 10 REDIS_HOST = redis REDIS_PORT = 6379 diff --git a/example.rep.env b/example.rep.env index 5baf587b2..3f9de896d 100644 --- a/example.rep.env +++ b/example.rep.env @@ -11,6 +11,7 @@ MONGOOSE_HOST_READ = mongo2 MONGOOSE_PORT_READ = 27017 MONGOOSE_USERNAME MONGOOSE_PASSWORD +MONGOOSE_POOL_SIZE = 10 REDIS_HOST = redis REDIS_PORT = 6379 diff --git a/src/app.module.ts b/src/app.module.ts index 7b239e0d8..942bdda1d 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -23,6 +23,7 @@ import { TagModule } from './tag/tag.module'; import { HealthController } from './health/health.controller'; import { AuthModule } from './auth/auth.module'; import { GqlContext } from './auth/gql-context'; +import { DatabaseService } from './common/DatabaseService'; @Module({ imports: [ @@ -91,7 +92,7 @@ import { GqlContext } from './auth/gql-context'; WebhookModule, AuthModule, ], - providers: [AppService], + providers: [AppService, DatabaseService], controllers: [AppController, HealthController], }) export class AppModule {} diff --git a/src/common/DatabaseService.ts b/src/common/DatabaseService.ts new file mode 100644 index 000000000..0c27f435b --- /dev/null +++ b/src/common/DatabaseService.ts @@ -0,0 +1,13 @@ +import { Injectable, OnModuleDestroy } from '@nestjs/common'; +import { InjectConnection } from '@nestjs/mongoose'; +import { Connection } from 'mongoose'; + +@Injectable() +export class DatabaseService implements OnModuleDestroy { + constructor(@InjectConnection() private readonly connection: Connection) {} + + async onModuleDestroy() { + await this.connection.close(); + console.log('Mongoose connection closed'); + } +} \ No newline at end of file diff --git a/src/config/config.service.ts b/src/config/config.service.ts index d8fe3c853..4a8b96de1 100644 --- a/src/config/config.service.ts +++ b/src/config/config.service.ts @@ -73,6 +73,9 @@ export class ConfigService { readPreference: this.get('REPLICASET') !== undefined ? readPreference : null, user: this.get('MONGOOSE_USERNAME'), pass: this.get('MONGOOSE_PASSWORD'), + maxPoolSize: this.get('MONGOOSE_POOL_SIZE') || 20, + retryAttempts: 2, + connectTimeoutMS: 30000, }; if (this.get('SSL_VALIDATE') === true) { options = { From 314a33e4ff173866c736530e89a2fd1d3bb898a1 Mon Sep 17 00:00:00 2001 From: Sairam Charan Date: Mon, 3 Jun 2024 12:01:55 +0530 Subject: [PATCH 2/4] fix: WEB-2246 lint fix --- src/common/DatabaseService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/DatabaseService.ts b/src/common/DatabaseService.ts index 0c27f435b..ac2d81b14 100644 --- a/src/common/DatabaseService.ts +++ b/src/common/DatabaseService.ts @@ -10,4 +10,4 @@ export class DatabaseService implements OnModuleDestroy { await this.connection.close(); console.log('Mongoose connection closed'); } -} \ No newline at end of file +} From cce17b2a303c1994585e48e5b8d3796e829137b2 Mon Sep 17 00:00:00 2001 From: Sairam Charan Date: Mon, 3 Jun 2024 13:13:18 +0530 Subject: [PATCH 3/4] fix: WEB-2246 added pool size option mongoose to handle memory --- src/config/config.service.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/config/config.service.ts b/src/config/config.service.ts index 4a8b96de1..89623e3a3 100644 --- a/src/config/config.service.ts +++ b/src/config/config.service.ts @@ -76,6 +76,9 @@ export class ConfigService { maxPoolSize: this.get('MONGOOSE_POOL_SIZE') || 20, retryAttempts: 2, connectTimeoutMS: 30000, + useUnifiedTopology: true, + useNewUrlParser: true, + socketTimeoutMS: 30000, }; if (this.get('SSL_VALIDATE') === true) { options = { From 8681c66dd440cd79862bc50b6fd458ef320873dc Mon Sep 17 00:00:00 2001 From: Sairam Charan Date: Mon, 3 Jun 2024 13:47:54 +0530 Subject: [PATCH 4/4] fix: WEB-2246 added pool size option mongoose to handle memory --- src/config/config.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config/config.service.ts b/src/config/config.service.ts index 89623e3a3..4ce747de2 100644 --- a/src/config/config.service.ts +++ b/src/config/config.service.ts @@ -75,10 +75,10 @@ export class ConfigService { pass: this.get('MONGOOSE_PASSWORD'), maxPoolSize: this.get('MONGOOSE_POOL_SIZE') || 20, retryAttempts: 2, - connectTimeoutMS: 30000, + connectTimeoutMS: 60000, useUnifiedTopology: true, useNewUrlParser: true, - socketTimeoutMS: 30000, + socketTimeoutMS: 60000, }; if (this.get('SSL_VALIDATE') === true) { options = {