Skip to content

Commit

Permalink
chore: separate connection abortion methods (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
karenc-bq authored Oct 21, 2024
1 parent 48e3cf4 commit ac33089
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
14 changes: 6 additions & 8 deletions common/lib/plugin_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,18 +389,16 @@ export class PluginService implements ErrorHandler, HostListProviderService {
return await this.getDialect().isClientValid(targetClient);
}

async tryClosingTargetClient(): Promise<void>;
async tryClosingTargetClient(targetClient: ClientWrapper): Promise<void>;
async tryClosingTargetClient(targetClient?: ClientWrapper): Promise<void>;
async tryClosingTargetClient(targetClient?: ClientWrapper | undefined): Promise<void> {
// Note: This last overload is only required because of the way typescript overloads work https://www.typescriptlang.org/docs/handbook/functions.html#overloads
if (targetClient) {
await this.getDialect().tryClosingTargetClient(targetClient);
} else if (this._currentClient.targetClient) {
async abortCurrentClient(): Promise<void> {
if (this._currentClient.targetClient) {
await this.getDialect().tryClosingTargetClient(this._currentClient.targetClient);
}
}

async tryClosingTargetClient(targetClient: ClientWrapper): Promise<void> {
await this.getDialect().tryClosingTargetClient(targetClient);
}

getSessionStateService() {
return this.sessionStateService;
}
Expand Down
6 changes: 3 additions & 3 deletions common/lib/plugins/failover/failover_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export class FailoverPlugin extends AbstractConnectionPlugin {
}

this.pluginService.getCurrentHostInfo()?.removeAlias(Array.from(oldAliases));
await this.pluginService.tryClosingTargetClient();
await this.pluginService.abortCurrentClient();
await this.pluginService.setCurrentClient(result.client, result.newHost);
await this.updateTopology(true);
}
Expand All @@ -379,7 +379,7 @@ export class FailoverPlugin extends AbstractConnectionPlugin {
throw new AwsWrapperError();
}

await this.pluginService.tryClosingTargetClient();
await this.pluginService.abortCurrentClient();
await this.pluginService.setCurrentClient(result.client, writerHostInfo);
logger.debug(Messages.get("Failover.establishedConnection", this.pluginService.getCurrentHostInfo()?.host ?? ""));
await this.pluginService.refreshHostList();
Expand All @@ -403,7 +403,7 @@ export class FailoverPlugin extends AbstractConnectionPlugin {
try {
const isValid = await client.isValid();
if (!isValid) {
await this.pluginService.tryClosingTargetClient();
await this.pluginService.abortCurrentClient();
}
} catch (error) {
// swallow this error, current target client should be useless anyway.
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/failover_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe("reader failover handler", () => {
when(mockRdsHostListProvider.getRdsUrlType()).thenReturn(RdsUrlType.RDS_WRITER_CLUSTER);
when(mockPluginService.getHostListProvider()).thenReturn(instance(mockRdsHostListProvider));
when(mockPluginService.getCurrentClient()).thenReturn(instance(mockAwsClient));
when(mockPluginService.tryClosingTargetClient()).thenResolve();
when(mockPluginService.abortCurrentClient()).thenResolve();
properties.clear();
});

Expand Down Expand Up @@ -399,11 +399,11 @@ describe("reader failover handler", () => {

await plugin.invalidateCurrentClient();

when(mockPluginService.tryClosingTargetClient()).thenThrow(new Error("test"));
when(mockPluginService.abortCurrentClient()).thenThrow(new Error("test"));

await plugin.invalidateCurrentClient();

verify(mockPluginService.tryClosingTargetClient()).twice();
verify(mockPluginService.abortCurrentClient()).twice();
});

it("test execute", async () => {
Expand Down

0 comments on commit ac33089

Please sign in to comment.