Skip to content

Commit e58e8e9

Browse files
feat: wrapper client setKeepAlive method (#339)
Co-authored-by: Karen Chen <karenc@bitquilltech.com>
1 parent 02bf0c9 commit e58e8e9

7 files changed

Lines changed: 52 additions & 16 deletions

File tree

common/lib/database_dialect/database_dialect.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import { HostListProvider } from "../host_list_provider/host_list_provider";
1818
import { HostListProviderService } from "../host_list_provider_service";
1919
import { ClientWrapper } from "../client_wrapper";
2020
import { FailoverRestriction } from "../plugins/failover/failover_restriction";
21-
import { AwsPoolClient } from "../aws_pool_client";
22-
import { AwsPoolConfig } from "../aws_pool_config";
2321
import { ErrorHandler } from "../error_handler";
2422

2523
export enum DatabaseType {

common/lib/driver_dialect/driver_dialect.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ export interface DriverDialect {
2727
preparePoolClientProperties(props: Map<string, any>, poolConfig: AwsPoolConfig | undefined): any;
2828

2929
getAwsPoolClient(props: any): AwsPoolClient;
30+
31+
setKeepAliveProperties(props: Map<string, any>, keepAliveProps: any): void;
3032
}

common/lib/wrapper_property.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ export class WrapperProperties {
335335
600_000 // 10 min
336336
);
337337

338+
static readonly KEEPALIVE_PROPERTIES = new WrapperProperty<Map<string, any>>(
339+
"wrapperKeepAliveProperties",
340+
"Map containing any keepAlive properties that the target driver accepts in the client configuration.",
341+
null
342+
);
343+
338344
static removeWrapperProperties(props: Map<string, any>): any {
339345
const persistingProperties = [
340346
WrapperProperties.USER.name,

docs/using-the-nodejs-wrapper/UsingTheNodejsWrapper.md

Lines changed: 12 additions & 11 deletions
Large diffs are not rendered by default.

mysql/lib/client.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import { ClientUtils } from "../../common/lib/utils/client_utils";
3131
import { RdsMultiAZMySQLDatabaseDialect } from "./dialect/rds_multi_az_mysql_database_dialect";
3232
import { TelemetryTraceLevel } from "../../common/lib/utils/telemetry/telemetry_trace_level";
3333
import { MySQL2DriverDialect } from "./dialect/mysql2_driver_dialect";
34-
import { PluginManager } from "../../common/lib";
3534

3635
export class AwsMySQLClient extends AwsClient {
3736
private static readonly knownDialectsByCode: Map<string, DatabaseDialect> = new Map([

mysql/lib/dialect/mysql2_driver_dialect.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { AwsPoolClient } from "../../../common/lib/aws_pool_client";
2323
import { AwsMysqlPoolClient } from "../mysql_pool_client";
2424
import { MySQLClientWrapper } from "../../../common/lib/mysql_client_wrapper";
2525
import { HostInfo } from "../../../common/lib/host_info";
26+
import { UnsupportedMethodError } from "../../../common/lib/utils/errors";
2627

2728
export class MySQL2DriverDialect implements DriverDialect {
2829
protected dialectName: string = this.constructor.name;
@@ -32,7 +33,10 @@ export class MySQL2DriverDialect implements DriverDialect {
3233
}
3334

3435
async connect(hostInfo: HostInfo, props: Map<string, any>): Promise<ClientWrapper> {
35-
const targetClient = await createConnection(WrapperProperties.removeWrapperProperties(props));
36+
const driverProperties = WrapperProperties.removeWrapperProperties(props);
37+
// MySQL2 does not support keep alive, explicitly check and throw an exception if this value is set.
38+
this.setKeepAliveProperties(driverProperties, props.get(WrapperProperties.KEEPALIVE_PROPERTIES.name));
39+
const targetClient = await createConnection(driverProperties);
3640
return Promise.resolve(new MySQLClientWrapper(targetClient, hostInfo, props));
3741
}
3842

@@ -52,4 +56,10 @@ export class MySQL2DriverDialect implements DriverDialect {
5256
getAwsPoolClient(props: PoolOptions): AwsPoolClient {
5357
return new AwsMysqlPoolClient(props);
5458
}
59+
60+
setKeepAliveProperties(props: Map<string, any>, keepAliveProps: any) {
61+
if (keepAliveProps) {
62+
throw new UnsupportedMethodError("Keep alive configuration is not supported for MySQL2.");
63+
}
64+
}
5565
}

pg/lib/dialect/node_postgres_driver_dialect.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ import { HostInfo } from "../../../common/lib/host_info";
2828

2929
export class NodePostgresDriverDialect implements DriverDialect {
3030
protected dialectName: string = this.constructor.name;
31+
private static keepAlivePropertyName = "keepAlive";
32+
private static keepAliveInitialDelayMillisPropertyName = "keepAliveInitialDelayMillis";
3133

3234
getDialectName(): string {
3335
return this.dialectName;
3436
}
3537

3638
async connect(hostInfo: HostInfo, props: Map<string, any>): Promise<ClientWrapper> {
37-
const targetClient = new pkgPg.Client(WrapperProperties.removeWrapperProperties(props));
39+
const driverProperties = WrapperProperties.removeWrapperProperties(props);
40+
this.setKeepAliveProperties(driverProperties, props.get(WrapperProperties.KEEPALIVE_PROPERTIES.name));
41+
const targetClient = new pkgPg.Client(driverProperties);
3842
await targetClient.connect();
3943
return Promise.resolve(new PgClientWrapper(targetClient, hostInfo, props));
4044
}
@@ -55,4 +59,20 @@ export class NodePostgresDriverDialect implements DriverDialect {
5559
getAwsPoolClient(props: pkgPg.PoolConfig): AwsPoolClient {
5660
return new AwsPgPoolClient(props);
5761
}
62+
63+
setKeepAliveProperties(props: Map<string, any>, keepAliveProps: any) {
64+
if (!keepAliveProps) {
65+
return;
66+
}
67+
68+
const keepAlive = keepAliveProps.get(NodePostgresDriverDialect.keepAlivePropertyName);
69+
const keepAliveInitialDelayMillis = keepAliveProps.get(NodePostgresDriverDialect.keepAliveInitialDelayMillisPropertyName);
70+
71+
if (keepAlive) {
72+
props.set(NodePostgresDriverDialect.keepAlivePropertyName, keepAlive);
73+
}
74+
if (keepAliveInitialDelayMillis) {
75+
props.set(NodePostgresDriverDialect.keepAliveInitialDelayMillisPropertyName, keepAliveInitialDelayMillis);
76+
}
77+
}
5878
}

0 commit comments

Comments
 (0)