Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): database connection timeout env overwrite support (#6187) #6674

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/fifty-apes-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@logto/shared": patch
"@logto/core": patch
---

add environment variable to override default database connection timeout

By default, out database connection timeout is set to 5 seconds, which might not be enough for some network conditions. This change allows users to override the default value by setting the `DATABASE_CONNECTION_TIMEOUT` environment variable.
4 changes: 3 additions & 1 deletion packages/core/src/env-set/create-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
const createPoolByEnv = async (
databaseDsn: string,
mockDatabaseConnection: boolean,
poolSize?: number
poolSize?: number,
connectionTimeout?: number
) => {
// Database connection is disabled in unit test environment
if (mockDatabaseConnection) {
Expand All @@ -22,6 +23,7 @@
return createPool(databaseDsn, {
interceptors: createInterceptorsPreset(),
maximumPoolSize: poolSize,
connectionTimeout,

Check warning on line 26 in packages/core/src/env-set/create-pool.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/env-set/create-pool.ts#L26

Added line #L26 was not covered by tests
});
};

Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/env-set/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export class EnvSet {
static sharedPool = createPoolByEnv(
this.dbUrl,
EnvSet.values.isUnitTest,
this.values.databasePoolSize
this.values.databasePoolSize,
EnvSet.values.databaseConnectionTimeout
);

#pool: Optional<DatabasePool>;
Expand Down Expand Up @@ -68,7 +69,8 @@ export class EnvSet {
const pool = await createPoolByEnv(
this.databaseUrl,
EnvSet.values.isUnitTest,
EnvSet.values.databasePoolSize
EnvSet.values.databasePoolSize,
EnvSet.values.databaseConnectionTimeout
);

this.#pool = pool;
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/node/env/GlobalValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ export default class GlobalValues {
/** Maximum number of clients to keep in a single database pool (i.e. per `Tenant` class). */
public readonly databasePoolSize = Number(getEnv('DATABASE_POOL_SIZE', '20'));

public readonly databaseConnectionTimeout = Number(getEnv('DATABASE_CONNECTION_TIMEOUT', '5000'));

/** Case insensitive username */
public readonly isCaseSensitiveUsername = yes(getEnv('CASE_SENSITIVE_USERNAME', 'true'));

Expand Down
Loading