override async connect()

in common/lib/plugins/failover2/failover2_plugin.ts [128:198]


  override async connect(
    hostInfo: HostInfo,
    props: Map<string, any>,
    isInitialConnection: boolean,
    connectFunc: () => Promise<ClientWrapper>
  ): Promise<ClientWrapper> {
    if (
      // Call was initiated by Failover2 Plugin, does not require additional processing.
      props.has(Failover2Plugin.INTERNAL_CONNECT_PROPERTY_NAME) ||
      // Failover is not enabled, does not require additional processing.
      !this.enableFailoverSetting ||
      !WrapperProperties.ENABLE_CLUSTER_AWARE_FAILOVER.get(props)
    ) {
      return await this._staleDnsHelper.getVerifiedConnection(hostInfo.host, isInitialConnection, this.hostListProviderService!, props, connectFunc);
    }

    const hostInfoWithAvailability: HostInfo = this.pluginService.getHosts().find((x) => x.getHostAndPort() === hostInfo.getHostAndPort());

    let client: ClientWrapper = null;
    if (!hostInfoWithAvailability || hostInfoWithAvailability.getAvailability() != HostAvailability.NOT_AVAILABLE) {
      try {
        client = await this._staleDnsHelper.getVerifiedConnection(
          hostInfo.host,
          isInitialConnection,
          this.hostListProviderService!,
          props,
          connectFunc
        );
      } catch (error) {
        if (!this.shouldErrorTriggerClientSwitch(error)) {
          throw error;
        }

        this.pluginService.setAvailability(hostInfo.allAliases, HostAvailability.NOT_AVAILABLE);

        try {
          // Unable to directly connect, attempt failover.
          await this.failover();
        } catch (error) {
          if (error instanceof FailoverSuccessError) {
            client = this.pluginService.getCurrentClient().targetClient;
          } else {
            throw error;
          }
        }
      }
    } else {
      try {
        // Host is unavailable or not part of the topology. Try to refresh host list and failover.
        await this.pluginService.refreshHostList();
        await this.failover();
      } catch (error) {
        if (error instanceof FailoverSuccessError) {
          client = this.pluginService.getCurrentClient().targetClient;
        } else {
          throw error;
        }
      }
    }

    if (!client) {
      // This should be unreachable, the above logic will either get a connection successfully or throw an error.
      throw new AwsWrapperError(Messages.get("Failover2.unableToConnect"));
    }

    if (isInitialConnection) {
      await this.pluginService.refreshHostList(client);
    }

    return client;
  }