Skip to content

Commit

Permalink
feat: wrapper client setKeepAlive method (#339)
Browse files Browse the repository at this point in the history
Co-authored-by: Karen Chen <[email protected]>
  • Loading branch information
crystall-bitquill and karenc-bq authored Dec 11, 2024
1 parent 02bf0c9 commit e58e8e9
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 16 deletions.
2 changes: 0 additions & 2 deletions common/lib/database_dialect/database_dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import { HostListProvider } from "../host_list_provider/host_list_provider";
import { HostListProviderService } from "../host_list_provider_service";
import { ClientWrapper } from "../client_wrapper";
import { FailoverRestriction } from "../plugins/failover/failover_restriction";
import { AwsPoolClient } from "../aws_pool_client";
import { AwsPoolConfig } from "../aws_pool_config";
import { ErrorHandler } from "../error_handler";

export enum DatabaseType {
Expand Down
2 changes: 2 additions & 0 deletions common/lib/driver_dialect/driver_dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ export interface DriverDialect {
preparePoolClientProperties(props: Map<string, any>, poolConfig: AwsPoolConfig | undefined): any;

getAwsPoolClient(props: any): AwsPoolClient;

setKeepAliveProperties(props: Map<string, any>, keepAliveProps: any): void;
}
6 changes: 6 additions & 0 deletions common/lib/wrapper_property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ export class WrapperProperties {
600_000 // 10 min
);

static readonly KEEPALIVE_PROPERTIES = new WrapperProperty<Map<string, any>>(
"wrapperKeepAliveProperties",
"Map containing any keepAlive properties that the target driver accepts in the client configuration.",
null
);

static removeWrapperProperties(props: Map<string, any>): any {
const persistingProperties = [
WrapperProperties.USER.name,
Expand Down
23 changes: 12 additions & 11 deletions docs/using-the-nodejs-wrapper/UsingTheNodejsWrapper.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion mysql/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { ClientUtils } from "../../common/lib/utils/client_utils";
import { RdsMultiAZMySQLDatabaseDialect } from "./dialect/rds_multi_az_mysql_database_dialect";
import { TelemetryTraceLevel } from "../../common/lib/utils/telemetry/telemetry_trace_level";
import { MySQL2DriverDialect } from "./dialect/mysql2_driver_dialect";
import { PluginManager } from "../../common/lib";

export class AwsMySQLClient extends AwsClient {
private static readonly knownDialectsByCode: Map<string, DatabaseDialect> = new Map([
Expand Down
12 changes: 11 additions & 1 deletion mysql/lib/dialect/mysql2_driver_dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { AwsPoolClient } from "../../../common/lib/aws_pool_client";
import { AwsMysqlPoolClient } from "../mysql_pool_client";
import { MySQLClientWrapper } from "../../../common/lib/mysql_client_wrapper";
import { HostInfo } from "../../../common/lib/host_info";
import { UnsupportedMethodError } from "../../../common/lib/utils/errors";

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

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

Expand All @@ -52,4 +56,10 @@ export class MySQL2DriverDialect implements DriverDialect {
getAwsPoolClient(props: PoolOptions): AwsPoolClient {
return new AwsMysqlPoolClient(props);
}

setKeepAliveProperties(props: Map<string, any>, keepAliveProps: any) {
if (keepAliveProps) {
throw new UnsupportedMethodError("Keep alive configuration is not supported for MySQL2.");
}
}
}
22 changes: 21 additions & 1 deletion pg/lib/dialect/node_postgres_driver_dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ import { HostInfo } from "../../../common/lib/host_info";

export class NodePostgresDriverDialect implements DriverDialect {
protected dialectName: string = this.constructor.name;
private static keepAlivePropertyName = "keepAlive";
private static keepAliveInitialDelayMillisPropertyName = "keepAliveInitialDelayMillis";

getDialectName(): string {
return this.dialectName;
}

async connect(hostInfo: HostInfo, props: Map<string, any>): Promise<ClientWrapper> {
const targetClient = new pkgPg.Client(WrapperProperties.removeWrapperProperties(props));
const driverProperties = WrapperProperties.removeWrapperProperties(props);
this.setKeepAliveProperties(driverProperties, props.get(WrapperProperties.KEEPALIVE_PROPERTIES.name));
const targetClient = new pkgPg.Client(driverProperties);
await targetClient.connect();
return Promise.resolve(new PgClientWrapper(targetClient, hostInfo, props));
}
Expand All @@ -55,4 +59,20 @@ export class NodePostgresDriverDialect implements DriverDialect {
getAwsPoolClient(props: pkgPg.PoolConfig): AwsPoolClient {
return new AwsPgPoolClient(props);
}

setKeepAliveProperties(props: Map<string, any>, keepAliveProps: any) {
if (!keepAliveProps) {
return;
}

const keepAlive = keepAliveProps.get(NodePostgresDriverDialect.keepAlivePropertyName);
const keepAliveInitialDelayMillis = keepAliveProps.get(NodePostgresDriverDialect.keepAliveInitialDelayMillisPropertyName);

if (keepAlive) {
props.set(NodePostgresDriverDialect.keepAlivePropertyName, keepAlive);
}
if (keepAliveInitialDelayMillis) {
props.set(NodePostgresDriverDialect.keepAliveInitialDelayMillisPropertyName, keepAliveInitialDelayMillis);
}
}
}

0 comments on commit e58e8e9

Please sign in to comment.