Skip to content

Commit ea31097

Browse files
committed
feat: add timeouts and pool settings to the Knex connection
1 parent 0cefe81 commit ea31097

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

.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",

TEMPLATE-CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Add timeouts and pool settings to the Mongoose connection.
13+
- Add timeouts and pool settings to the Knex connection.
1314

1415
## [2.4.0] - 2025-05-08
1516

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/util/constants/environment.ts

Lines changed: 10 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 || '',

test/unit/infra/db/mssql/util/connection/knext-connection.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ describe('KNEX connection config', () => {
88
const config = getConfig();
99

1010
const values = Object.keys(config.connection);
11-
const expectedValues = ['host', 'port', 'user', 'password', 'options'];
11+
const expectedValues = [
12+
'host',
13+
'port',
14+
'user',
15+
'password',
16+
'requestTimeout',
17+
'options'
18+
];
19+
1220
expect(values).toEqual(expectedValues);
1321
expect(config.client).toBeTruthy();
1422
});

0 commit comments

Comments
 (0)