Skip to content

Commit e71287e

Browse files
authored
chore: merge pull request #60 from SamuelGoulart/feat/add-timeouts
Feat: add timeouts and pool settings to the Mongoose connection and knex
2 parents c6328a2 + ea31097 commit e71287e

11 files changed

Lines changed: 85 additions & 158 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"hokify",
1212
"italog",
1313
"memjs",
14+
"Millis",
1415
"msisdn",
1516
"mvno",
1617
"OLLEH",

CHANGELOG.md

Lines changed: 2 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -5,151 +5,8 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [2.4.0] - 2025-05-08
8+
## [0.0.0] - 0000-00-00
99

1010
### Added
1111

12-
- Add the `CacheRequestAdapter` wrapper of cache functionalities inside request adapter structure and interfaces.
13-
14-
### Changed
15-
16-
- Update dependencies.
17-
- Update Node.js version in Dockerfile.
18-
- Add type corrections to the RabbitMQ server.
19-
20-
### Fixed
21-
22-
- Correct logic to enable offline logger.
23-
24-
## [2.3.0] - 2025-05-06
25-
26-
### Added
27-
28-
- New `normalize` and `normalizeOptions` utilities for payload normalization in the reprocessing module.
29-
- APM decorators for handlers, enabling instrumentation.
30-
- Functionality to extract RabbitMQ options from the `RABBIT_OPTIONS` environment variable.
31-
32-
### Changed
33-
34-
- Renamed non-standard folder from `utils` to `util`.
35-
- Improved application startup and shutdown process.
36-
- Enhanced MemCached connection process and added connection timeout.
37-
- Updated the `.env.example` file.
38-
- Adjusted when Elastic is started.
39-
- Added protocol adaptation for new RabbitMQ implementations.
40-
- Improved handler processing logic to prevent subsequent handlers from executing prematurely.
41-
- Optimized APM decorators.
42-
43-
### Fixed
44-
45-
- Added normalization before message validation in the reprocessing module.
46-
- Optimized `searchLabels` to prevent maximum call stack overflow.
47-
48-
### Deprecated
49-
50-
- _No deprecations._
51-
52-
### Removed
53-
54-
- _No removals._
55-
56-
### Security
57-
58-
- _No security fixes.
59-
60-
## [2.2.0] - 2025-03-19
61-
62-
### Added
63-
64-
- Adds the `apm-transaction-id` header to the response when APM is active.
65-
- Adds the `CONSUMER_LIST` and `WORKER_LIST` environment variables to override the hard coded worker and consumer settings
66-
- Introduces the `AtLeastOne` global type to enhance type utility.
67-
68-
### Fixed
69-
70-
- Prevents the context from being concatenated as undefined.
71-
- Returns an empty array if no callbacks are provided in promises.
72-
- Establishes a connection to memcached.
73-
74-
### Changed
75-
76-
- Improves the build process by simplifying the Dockerfile.
77-
78-
## [2.1.3] - 2024-12-06
79-
80-
## Added
81-
82-
- Add AtLeastOne global type.
83-
84-
## [2.1.2] - 2024-11-26
85-
86-
### Fixed
87-
88-
- Fixing the route loading management, add a mechanism to track loading state of WebServer class.
89-
90-
## [2.1.1] - 2024-11-19
91-
92-
### Fixed
93-
94-
- The display URL of Agendash was being shown incorrectly in the terminal, and this version fixes that URL.
95-
96-
## [2.1.0] - 2024-11-05
97-
98-
### Added
99-
100-
- Add CLI support.
101-
- Add graceful shutdown for workers.
102-
103-
## [2.0.0] - 2024-11-04
104-
105-
### Added
106-
107-
- Add websocket support.
108-
- Add graceful shutdown as default.
109-
- Change internal http/https engine.
110-
111-
## Changed
112-
113-
- Remove support for server, worker and consumer commands and their derivatives.
114-
- Remove the application file in /src/main, now import the server from web-server.ts.
115-
116-
## [1.1.0] - 2024-06-03
117-
118-
### Added
119-
120-
- Add virtual host connection on RabbitMq.
121-
- Add reject message on RabbitMq server.
122-
123-
## [1.0.0] - 2024-04-17
124-
125-
### Added
126-
127-
- Directory structure matching Clean Architecture layers.
128-
- Example snippets based on SOLID principles and project patterns, as Factory and Adapter.
129-
- HTTP Server adapter equipped with uncoupling; input and output standardization; state sharing and utils resources.
130-
- RabbitMq Server adapter equipped with uncoupling; input and output standardization; state sharing and utils resources.
131-
- Scheduled tasks adapter equipped with uncoupling; management through database; input and output standardization; state sharing and utils resources.
132-
- HTTP Requests calls adapter.
133-
- Cryptography adapter.
134-
- Cache server integration adapter.
135-
- Input validator adapter (available to endpoints, queues and scheduled tasks)
136-
- Extension of Knex functionalities: noLock hint, adjustment to time zone conversions and custom select.
137-
- Support for unit and integration tests.
138-
- Support for running HTTP Server, message broker and scheduled tasks simultaneously.
139-
- Native integration with APM and ElasticSearch database.
140-
- Utils for database mapping with Knex schemas.
141-
- Utils for ElasticSearch integration.
142-
- Utils for date handling.
143-
- Utils for object handling (conversion to snake_case, camelCase...).
144-
- Utils for promise handling.
145-
- Utils for numbers and strings handling and formatting.
146-
- Utils for text standardization (applicable to Http responses messages, for example).
147-
- Utils for aggregate and organize consts.
148-
- Utils for responses standardization.
149-
- Utils for queued messages reprocessing.
150-
- Utils for managing shared state dependencies.
151-
- Utils for logging (file, MongoDb and Elastic).
152-
- Utils for dynamic injection of data on Middlewares (ExtractValues).
153-
- Utils for dynamic management of application flow (FlowManager).
154-
- Utils for coordinating middlewares flow, with middleware chain pattern (Flow).
155-
- Utils for run N middlewares in concurrency (parallelizer).
12+
- Add example.

TEMPLATE-CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.4.0] - 2025-12-03
9+
10+
### Added
11+
12+
- Add timeouts and pool settings to the Mongoose connection.
13+
- Add timeouts and pool settings to the Knex connection.
14+
815
## [2.4.0] - 2025-05-08
916

1017
### Added
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CreateInputAndOutputLogRepositoryRepository } from '@/data/protocols/db';
22
import { convertCamelCaseKeysToSnakeCase } from '@/util/object';
3+
import { logger } from '@/util';
34

45
import { InputAndOutputLogModel } from './input-and-output-log-model';
56

@@ -9,8 +10,14 @@ export class InputAndOutputLogRepository
910
public async create(
1011
params: CreateInputAndOutputLogRepositoryRepository.Params
1112
): CreateInputAndOutputLogRepositoryRepository.Result {
12-
const formattedParams = convertCamelCaseKeysToSnakeCase(params);
13+
try {
14+
const formattedParams = convertCamelCaseKeysToSnakeCase(params);
1315

14-
await InputAndOutputLogModel.create(formattedParams);
16+
await InputAndOutputLogModel.create(formattedParams, {
17+
writeConcern: { w: 0 }
18+
});
19+
} catch (error) {
20+
logger.log(error);
21+
}
1522
}
1623
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { CreateLogRepository } from '@/data/protocols/db';
2-
import { convertCamelCaseKeysToSnakeCase } from '@/util';
2+
import { convertCamelCaseKeysToSnakeCase, logger } from '@/util';
33

44
import { LogModel } from './log-model';
55

66
export class LogRepository implements CreateLogRepository {
77
public async create(
88
params: CreateLogRepository.Params
99
): CreateLogRepository.Result {
10-
const formattedParams = convertCamelCaseKeysToSnakeCase(params);
11-
await LogModel.create(formattedParams);
10+
try {
11+
const formattedParams = convertCamelCaseKeysToSnakeCase(params);
12+
await LogModel.create(formattedParams, { writeConcern: { w: 0 } });
13+
} catch (error) {
14+
logger.log(error);
15+
}
1216
}
1317
}

src/infra/db/mssql/util/connection/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@ const configs = {
1212
port: +DB.PORT,
1313
user: DB.USERNAME,
1414
password: DB.PASSWORD,
15+
requestTimeout: DB.CONNECTION_TIMEOUT_MS,
1516
options: {
1617
encrypt: false,
1718
enableArithAbort: false,
1819
appName: pkg.name
1920
}
21+
},
22+
pool: {
23+
max: DB.MAX_POOL,
24+
min: DB.MIN_POOL,
25+
acquireTimeoutMillis: DB.CONNECTION_TIMEOUT_MS,
26+
createTimeoutMillis: DB.CONNECTION_TIMEOUT_MS,
27+
destroyTimeoutMillis: DB.CONNECTION_TIMEOUT_MS,
28+
idleTimeoutMillis: DB.IDLE_TIMEOUT_IN_MILLISECONDS,
29+
reapIntervalMillis: DB.REPEAT_INTERVAL_IN_MS,
30+
createRetryIntervalMillis: DB.CREATE_RETRY_INTERVAL_IN_MS
2031
}
2132
},
2233

src/main/util/get-mongoose-connection.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ import mongoose from 'mongoose';
33
import { MONGO } from '@/util';
44

55
export function getMongooseConnection() {
6-
return mongoose.set('strictQuery', false).connect(MONGO.URL(), {
7-
dbName: MONGO.NAME,
8-
authSource: MONGO.AUTH_SOURCE,
9-
authMechanism: 'SCRAM-SHA-1'
10-
});
6+
return mongoose
7+
.set('strictQuery', false)
8+
.set('bufferTimeoutMS', MONGO.CONNECTION_TIMEOUT_MS)
9+
.connect(MONGO.URL(), {
10+
dbName: MONGO.NAME,
11+
authSource: MONGO.AUTH_SOURCE,
12+
authMechanism: 'SCRAM-SHA-1',
13+
serverSelectionTimeoutMS: MONGO.CONNECTION_TIMEOUT_MS,
14+
socketTimeoutMS: MONGO.CONNECTION_TIMEOUT_MS,
15+
connectTimeoutMS: MONGO.CONNECTION_TIMEOUT_MS,
16+
maxPoolSize: MONGO.MAX_POOL_SIZE,
17+
minPoolSize: MONGO.MIN_POOL_SIZE
18+
});
1119
}

src/util/constants/environment.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ export const DB = {
5757
? stringToBoolean(process.env.DB_ENABLED)
5858
: true,
5959
DB_TEST_CONNECTION_QUERY: process.env.DB_TEST_CONNECTION_QUERY || 'SELECT 1',
60+
CONNECTION_TIMEOUT_MS: +(() =>
61+
process.env.DB_CONNECTION_TIMEOUT_MS || 60000)(),
62+
MAX_POOL: +(() => process.env.DB_MAX_POOL || 10)(),
63+
MIN_POOL: +(() => process.env.DB_MIN_POOL || 2)(),
64+
IDLE_TIMEOUT_IN_MILLISECONDS: +(() =>
65+
process.env.DB_IDLE_TIMEOUT_IN_MILLISECONDS || 30000)(),
66+
REPEAT_INTERVAL_IN_MS: +(() =>
67+
process.env.DB_REPEAT_INTERVAL_IN_MS || 1000)(),
68+
CREATE_RETRY_INTERVAL_IN_MS: +(() =>
69+
process.env.DB_CREATE_RETRY_INTERVAL_IN_MS || 200)(),
6070
CONFIG: process.env.DB_CONFIG || 'default',
6171
DIALECT: process.env.DB_DIALECT || 'mssql',
6272
HOST: process.env.DB_HOST || '',
@@ -85,6 +95,10 @@ export const MONGO = {
8595
ENABLED: process.env.MONGO_ENABLED
8696
? stringToBoolean(process.env.MONGO_ENABLED)
8797
: true,
98+
CONNECTION_TIMEOUT_MS: +(() =>
99+
process.env.MONGO_CONNECTION_TIMEOUT_MS || 60000)(),
100+
MAX_POOL_SIZE: +(() => process.env.MONGO_MAX_POOL_SIZE || 100)(),
101+
MIN_POOL_SIZE: +(() => process.env.MONGO_MIN_POOL_SIZE || 1)(),
88102
USER: process.env.MONGO_USER || '',
89103
PASSWORD: process.env.MONGO_PASSWORD || '',
90104
HOST: process.env.MONGO_HOST || '',

test/unit/infra/db/mongodb/input-and-output-log/input-and-output-log-repository.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ describe('InputAndOutputLog Repository', () => {
2828
},
2929
type: 'any_type'
3030
};
31-
expect(createSpy).toHaveBeenCalledWith(expected);
31+
32+
const writeConcern = {
33+
w: 0
34+
};
35+
36+
expect(createSpy).toHaveBeenCalledWith(expected, { writeConcern });
3237
});
3338
});

test/unit/infra/db/mongodb/log/log-repository.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ describe('Log Repository', () => {
2828
log_message: 'my_item',
2929
type: 'any_type'
3030
};
31-
expect(createSpy).toHaveBeenCalledWith(expected);
31+
32+
const writeConcern = {
33+
w: 0
34+
};
35+
36+
expect(createSpy).toHaveBeenCalledWith(expected, { writeConcern });
3237
});
3338
});

0 commit comments

Comments
 (0)