From 0cefe81b5ed413a0ceba1a9f14a45b46e9510a5b Mon Sep 17 00:00:00 2001 From: Samuel Goulart Date: Wed, 3 Dec 2025 13:31:12 -0300 Subject: [PATCH 1/2] feat: add timeouts and pool settings to the Mongoose connection --- CHANGELOG.md | 147 +----------------- TEMPLATE-CHANGELOG.md | 6 + .../input-and-output-log-repository.ts | 11 +- src/infra/db/mongodb/log/log-repository.ts | 10 +- src/main/util/get-mongoose-connection.ts | 18 ++- src/util/constants/environment.ts | 4 + .../input-and-output-log-repository.spec.ts | 7 +- .../db/mongodb/log/log-repository.spec.ts | 7 +- 8 files changed, 53 insertions(+), 157 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db208ae1..0ee1b06d 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,151 +5,8 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [2.4.0] - 2025-05-08 +## [0.0.0] - 0000-00-00 ### Added -- Add the `CacheRequestAdapter` wrapper of cache functionalities inside request adapter structure and interfaces. - -### Changed - -- Update dependencies. -- Update Node.js version in Dockerfile. -- Add type corrections to the RabbitMQ server. - -### Fixed - -- Correct logic to enable offline logger. - -## [2.3.0] - 2025-05-06 - -### Added - -- New `normalize` and `normalizeOptions` utilities for payload normalization in the reprocessing module. -- APM decorators for handlers, enabling instrumentation. -- Functionality to extract RabbitMQ options from the `RABBIT_OPTIONS` environment variable. - -### Changed - -- Renamed non-standard folder from `utils` to `util`. -- Improved application startup and shutdown process. -- Enhanced MemCached connection process and added connection timeout. -- Updated the `.env.example` file. -- Adjusted when Elastic is started. -- Added protocol adaptation for new RabbitMQ implementations. -- Improved handler processing logic to prevent subsequent handlers from executing prematurely. -- Optimized APM decorators. - -### Fixed - -- Added normalization before message validation in the reprocessing module. -- Optimized `searchLabels` to prevent maximum call stack overflow. - -### Deprecated - -- _No deprecations._ - -### Removed - -- _No removals._ - -### Security - -- _No security fixes. - -## [2.2.0] - 2025-03-19 - -### Added - -- Adds the `apm-transaction-id` header to the response when APM is active. -- Adds the `CONSUMER_LIST` and `WORKER_LIST` environment variables to override the hard coded worker and consumer settings -- Introduces the `AtLeastOne` global type to enhance type utility. - -### Fixed - -- Prevents the context from being concatenated as undefined. -- Returns an empty array if no callbacks are provided in promises. -- Establishes a connection to memcached. - -### Changed - -- Improves the build process by simplifying the Dockerfile. - -## [2.1.3] - 2024-12-06 - -## Added - -- Add AtLeastOne global type. - -## [2.1.2] - 2024-11-26 - -### Fixed - -- Fixing the route loading management, add a mechanism to track loading state of WebServer class. - -## [2.1.1] - 2024-11-19 - -### Fixed - -- The display URL of Agendash was being shown incorrectly in the terminal, and this version fixes that URL. - -## [2.1.0] - 2024-11-05 - -### Added - -- Add CLI support. -- Add graceful shutdown for workers. - -## [2.0.0] - 2024-11-04 - -### Added - -- Add websocket support. -- Add graceful shutdown as default. -- Change internal http/https engine. - -## Changed - -- Remove support for server, worker and consumer commands and their derivatives. -- Remove the application file in /src/main, now import the server from web-server.ts. - -## [1.1.0] - 2024-06-03 - -### Added - -- Add virtual host connection on RabbitMq. -- Add reject message on RabbitMq server. - -## [1.0.0] - 2024-04-17 - -### Added - -- Directory structure matching Clean Architecture layers. -- Example snippets based on SOLID principles and project patterns, as Factory and Adapter. -- HTTP Server adapter equipped with uncoupling; input and output standardization; state sharing and utils resources. -- RabbitMq Server adapter equipped with uncoupling; input and output standardization; state sharing and utils resources. -- Scheduled tasks adapter equipped with uncoupling; management through database; input and output standardization; state sharing and utils resources. -- HTTP Requests calls adapter. -- Cryptography adapter. -- Cache server integration adapter. -- Input validator adapter (available to endpoints, queues and scheduled tasks) -- Extension of Knex functionalities: noLock hint, adjustment to time zone conversions and custom select. -- Support for unit and integration tests. -- Support for running HTTP Server, message broker and scheduled tasks simultaneously. -- Native integration with APM and ElasticSearch database. -- Utils for database mapping with Knex schemas. -- Utils for ElasticSearch integration. -- Utils for date handling. -- Utils for object handling (conversion to snake_case, camelCase...). -- Utils for promise handling. -- Utils for numbers and strings handling and formatting. -- Utils for text standardization (applicable to Http responses messages, for example). -- Utils for aggregate and organize consts. -- Utils for responses standardization. -- Utils for queued messages reprocessing. -- Utils for managing shared state dependencies. -- Utils for logging (file, MongoDb and Elastic). -- Utils for dynamic injection of data on Middlewares (ExtractValues). -- Utils for dynamic management of application flow (FlowManager). -- Utils for coordinating middlewares flow, with middleware chain pattern (Flow). -- Utils for run N middlewares in concurrency (parallelizer). +- Add example. \ No newline at end of file diff --git a/TEMPLATE-CHANGELOG.md b/TEMPLATE-CHANGELOG.md index e77d61c0..128f31b1 100755 --- a/TEMPLATE-CHANGELOG.md +++ b/TEMPLATE-CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.4.0] - 2025-12-03 + +### Added + +- Add timeouts and pool settings to the Mongoose connection. + ## [2.4.0] - 2025-05-08 ### Added diff --git a/src/infra/db/mongodb/input-and-output-log/input-and-output-log-repository.ts b/src/infra/db/mongodb/input-and-output-log/input-and-output-log-repository.ts index ba2159b9..4b1e1cef 100755 --- a/src/infra/db/mongodb/input-and-output-log/input-and-output-log-repository.ts +++ b/src/infra/db/mongodb/input-and-output-log/input-and-output-log-repository.ts @@ -1,5 +1,6 @@ import { CreateInputAndOutputLogRepositoryRepository } from '@/data/protocols/db'; import { convertCamelCaseKeysToSnakeCase } from '@/util/object'; +import { logger } from '@/util'; import { InputAndOutputLogModel } from './input-and-output-log-model'; @@ -9,8 +10,14 @@ export class InputAndOutputLogRepository public async create( params: CreateInputAndOutputLogRepositoryRepository.Params ): CreateInputAndOutputLogRepositoryRepository.Result { - const formattedParams = convertCamelCaseKeysToSnakeCase(params); + try { + const formattedParams = convertCamelCaseKeysToSnakeCase(params); - await InputAndOutputLogModel.create(formattedParams); + await InputAndOutputLogModel.create(formattedParams, { + writeConcern: { w: 0 } + }); + } catch (error) { + logger.log(error); + } } } diff --git a/src/infra/db/mongodb/log/log-repository.ts b/src/infra/db/mongodb/log/log-repository.ts index f98618aa..0f350879 100755 --- a/src/infra/db/mongodb/log/log-repository.ts +++ b/src/infra/db/mongodb/log/log-repository.ts @@ -1,5 +1,5 @@ import { CreateLogRepository } from '@/data/protocols/db'; -import { convertCamelCaseKeysToSnakeCase } from '@/util'; +import { convertCamelCaseKeysToSnakeCase, logger } from '@/util'; import { LogModel } from './log-model'; @@ -7,7 +7,11 @@ export class LogRepository implements CreateLogRepository { public async create( params: CreateLogRepository.Params ): CreateLogRepository.Result { - const formattedParams = convertCamelCaseKeysToSnakeCase(params); - await LogModel.create(formattedParams); + try { + const formattedParams = convertCamelCaseKeysToSnakeCase(params); + await LogModel.create(formattedParams, { writeConcern: { w: 0 } }); + } catch (error) { + logger.log(error); + } } } diff --git a/src/main/util/get-mongoose-connection.ts b/src/main/util/get-mongoose-connection.ts index f6b67c3c..608b88e9 100644 --- a/src/main/util/get-mongoose-connection.ts +++ b/src/main/util/get-mongoose-connection.ts @@ -3,9 +3,17 @@ import mongoose from 'mongoose'; import { MONGO } from '@/util'; export function getMongooseConnection() { - return mongoose.set('strictQuery', false).connect(MONGO.URL(), { - dbName: MONGO.NAME, - authSource: MONGO.AUTH_SOURCE, - authMechanism: 'SCRAM-SHA-1' - }); + return mongoose + .set('strictQuery', false) + .set('bufferTimeoutMS', MONGO.CONNECTION_TIMEOUT_MS) + .connect(MONGO.URL(), { + dbName: MONGO.NAME, + authSource: MONGO.AUTH_SOURCE, + authMechanism: 'SCRAM-SHA-1', + serverSelectionTimeoutMS: MONGO.CONNECTION_TIMEOUT_MS, + socketTimeoutMS: MONGO.CONNECTION_TIMEOUT_MS, + connectTimeoutMS: MONGO.CONNECTION_TIMEOUT_MS, + maxPoolSize: MONGO.MAX_POOL_SIZE, + minPoolSize: MONGO.MIN_POOL_SIZE + }); } diff --git a/src/util/constants/environment.ts b/src/util/constants/environment.ts index d78c62f4..31993e08 100755 --- a/src/util/constants/environment.ts +++ b/src/util/constants/environment.ts @@ -85,6 +85,10 @@ export const MONGO = { ENABLED: process.env.MONGO_ENABLED ? stringToBoolean(process.env.MONGO_ENABLED) : true, + CONNECTION_TIMEOUT_MS: +(() => + process.env.MONGO_CONNECTION_TIMEOUT_MS || 60000)(), + MAX_POOL_SIZE: +(() => process.env.MONGO_MAX_POOL_SIZE || 100)(), + MIN_POOL_SIZE: +(() => process.env.MONGO_MIN_POOL_SIZE || 1)(), USER: process.env.MONGO_USER || '', PASSWORD: process.env.MONGO_PASSWORD || '', HOST: process.env.MONGO_HOST || '', diff --git a/test/unit/infra/db/mongodb/input-and-output-log/input-and-output-log-repository.spec.ts b/test/unit/infra/db/mongodb/input-and-output-log/input-and-output-log-repository.spec.ts index 0f4226d3..63369d19 100644 --- a/test/unit/infra/db/mongodb/input-and-output-log/input-and-output-log-repository.spec.ts +++ b/test/unit/infra/db/mongodb/input-and-output-log/input-and-output-log-repository.spec.ts @@ -28,6 +28,11 @@ describe('InputAndOutputLog Repository', () => { }, type: 'any_type' }; - expect(createSpy).toHaveBeenCalledWith(expected); + + const writeConcern = { + w: 0 + }; + + expect(createSpy).toHaveBeenCalledWith(expected, { writeConcern }); }); }); diff --git a/test/unit/infra/db/mongodb/log/log-repository.spec.ts b/test/unit/infra/db/mongodb/log/log-repository.spec.ts index 1b04e14d..bd633e5e 100644 --- a/test/unit/infra/db/mongodb/log/log-repository.spec.ts +++ b/test/unit/infra/db/mongodb/log/log-repository.spec.ts @@ -28,6 +28,11 @@ describe('Log Repository', () => { log_message: 'my_item', type: 'any_type' }; - expect(createSpy).toHaveBeenCalledWith(expected); + + const writeConcern = { + w: 0 + }; + + expect(createSpy).toHaveBeenCalledWith(expected, { writeConcern }); }); }); From ea31097a5582bfe5c044f9595471a8c841e5f931 Mon Sep 17 00:00:00 2001 From: Samuel Goulart Date: Wed, 3 Dec 2025 14:01:55 -0300 Subject: [PATCH 2/2] feat: add timeouts and pool settings to the Knex connection --- .vscode/settings.json | 1 + TEMPLATE-CHANGELOG.md | 1 + src/infra/db/mssql/util/connection/index.ts | 11 +++++++++++ src/util/constants/environment.ts | 10 ++++++++++ .../db/mssql/util/connection/knext-connection.spec.ts | 10 +++++++++- 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index fa3a5dc8..66a6b945 100755 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,6 +11,7 @@ "hokify", "italog", "memjs", + "Millis", "msisdn", "mvno", "OLLEH", diff --git a/TEMPLATE-CHANGELOG.md b/TEMPLATE-CHANGELOG.md index 128f31b1..863a3733 100755 --- a/TEMPLATE-CHANGELOG.md +++ b/TEMPLATE-CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add timeouts and pool settings to the Mongoose connection. +- Add timeouts and pool settings to the Knex connection. ## [2.4.0] - 2025-05-08 diff --git a/src/infra/db/mssql/util/connection/index.ts b/src/infra/db/mssql/util/connection/index.ts index 81c9659d..8f6c3c9d 100755 --- a/src/infra/db/mssql/util/connection/index.ts +++ b/src/infra/db/mssql/util/connection/index.ts @@ -12,11 +12,22 @@ const configs = { port: +DB.PORT, user: DB.USERNAME, password: DB.PASSWORD, + requestTimeout: DB.CONNECTION_TIMEOUT_MS, options: { encrypt: false, enableArithAbort: false, appName: pkg.name } + }, + pool: { + max: DB.MAX_POOL, + min: DB.MIN_POOL, + acquireTimeoutMillis: DB.CONNECTION_TIMEOUT_MS, + createTimeoutMillis: DB.CONNECTION_TIMEOUT_MS, + destroyTimeoutMillis: DB.CONNECTION_TIMEOUT_MS, + idleTimeoutMillis: DB.IDLE_TIMEOUT_IN_MILLISECONDS, + reapIntervalMillis: DB.REPEAT_INTERVAL_IN_MS, + createRetryIntervalMillis: DB.CREATE_RETRY_INTERVAL_IN_MS } }, diff --git a/src/util/constants/environment.ts b/src/util/constants/environment.ts index 31993e08..678654a8 100755 --- a/src/util/constants/environment.ts +++ b/src/util/constants/environment.ts @@ -57,6 +57,16 @@ export const DB = { ? stringToBoolean(process.env.DB_ENABLED) : true, DB_TEST_CONNECTION_QUERY: process.env.DB_TEST_CONNECTION_QUERY || 'SELECT 1', + CONNECTION_TIMEOUT_MS: +(() => + process.env.DB_CONNECTION_TIMEOUT_MS || 60000)(), + MAX_POOL: +(() => process.env.DB_MAX_POOL || 10)(), + MIN_POOL: +(() => process.env.DB_MIN_POOL || 2)(), + IDLE_TIMEOUT_IN_MILLISECONDS: +(() => + process.env.DB_IDLE_TIMEOUT_IN_MILLISECONDS || 30000)(), + REPEAT_INTERVAL_IN_MS: +(() => + process.env.DB_REPEAT_INTERVAL_IN_MS || 1000)(), + CREATE_RETRY_INTERVAL_IN_MS: +(() => + process.env.DB_CREATE_RETRY_INTERVAL_IN_MS || 200)(), CONFIG: process.env.DB_CONFIG || 'default', DIALECT: process.env.DB_DIALECT || 'mssql', HOST: process.env.DB_HOST || '', diff --git a/test/unit/infra/db/mssql/util/connection/knext-connection.spec.ts b/test/unit/infra/db/mssql/util/connection/knext-connection.spec.ts index 5818b901..49216dc1 100644 --- a/test/unit/infra/db/mssql/util/connection/knext-connection.spec.ts +++ b/test/unit/infra/db/mssql/util/connection/knext-connection.spec.ts @@ -8,7 +8,15 @@ describe('KNEX connection config', () => { const config = getConfig(); const values = Object.keys(config.connection); - const expectedValues = ['host', 'port', 'user', 'password', 'options']; + const expectedValues = [ + 'host', + 'port', + 'user', + 'password', + 'requestTimeout', + 'options' + ]; + expect(values).toEqual(expectedValues); expect(config.client).toBeTruthy(); });