NestJS - Too many MongoDB connections opened

Viewed 287

I'm using NestJS for our microservice and here's how we connect to MongoDB.

import { Inject, Injectable, Scope } from '@nestjs/common';
import {
  MongooseOptionsFactory,
  MongooseModuleOptions,
} from '@nestjs/mongoose';
import { REQUEST } from '@nestjs/core';
import { Request } from '@nestjs/common';
import * as fs from 'fs';

@Injectable()
export class MongoDBService implements MongooseOptionsFactory {
  constructor(@Inject(REQUEST) private readonly request: Request) {}

  createMongooseOptions(): MongooseModuleOptions {
    if (process.env.ENVIRONMENT === 'local.dev') {
      return {
        uri: process.env.MONGO_URL,
      };
    }
    return {
      uri: process.env.MONGO_URL,
      ssl: Boolean(process.env.MONGO_SSL),
      poolSize: 10,
      maxPoolSize: 25,
      sslCA: [
        fs.readFileSync(
          __dirname + '/../../config/certificates/EnterpriseRootCA.cer',
        ),
      ],
    };
  }
}

This is included in app module like below

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: `config/${process.env.ENVIRONMENT}.env`,
    }),
    TerminusModule,
    // EventEmitterModule.forRoot({ wildcard: true, delimiter: '.' }),
    MongooseModule.forRootAsync({ useClass: MongoDBService }),
    ScorecardModule,
    // DriverlicenseModule,
    PersistenceModule,
    MigrateModule,
    PurgeModule,
  ],
  providers: [MongoDBService, RedisPersistService, MongoDBHealthIndicator],
  controllers: [HealthController],
})
export class AppModule {}

After some insert and select operation, MongoDB errors out with "Too many connections opened". Is there anything I need to do to connect or close connection, when we use MongooseModule? Here are the logs from MongoDB

{"remote":"X.X.X.X:41412","connectionId":15945,"connectionCount":2015}}
{"t":{"$date":"2021-05-18T20:37:16.615+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"X.X.X.X:41412:41414","connectionId":15946,"connectionCount":2016}}
{"t":{"$date":"2021-05-18T20:37:16.616+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"X.X.X.X:41412:41416","connectionId":15947,"connectionCount":2017}}
{"t":{"$date":"2021-05-18T20:37:16.621+00:00"},"s":"I",  "c":"-",        "id":22948,   "ctx":"listener","msg":"pthread_create failed","attr":{"error":"Resource temporarily unavailable"}}
{"t":{"$date":"2021-05-18T20:37:16.621+00:00"},"s":"W",  "c":"EXECUTOR", "id":22993,   "ctx":"conn15947","msg":"Terminating session due to error","attr":{"error":{"code":1,"codeName":"InternalError","errmsg":"failed to create service entry worker thread"}}}
{"t":{"$date":"2021-05-18T20:37:16.621+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"listener","msg":"Connection ended","attr":{"remote":"X.X.X.X:41412:41416","connectionId":15947,"connectionCount":2016}}

As you can see in the logs, connectionCount is well over 2000, even though my connection pool is set at 25. Any help/advice will be much appreciated

0 Answers
Related