async connect()

in common/lib/driver_connection_provider.ts [50:104]


  async connect(hostInfo: HostInfo, pluginService: PluginService, props: Map<string, any>): Promise<ClientWrapper> {
    let resultTargetClient;
    const resultProps = new Map(props);
    resultProps.set(WrapperProperties.HOST.name, hostInfo.host);
    if (hostInfo.isPortSpecified()) {
      resultProps.set(WrapperProperties.PORT.name, hostInfo.port);
    }
    const driverDialect: DriverDialect = pluginService.getDriverDialect();
    try {
      resultTargetClient = await driverDialect.connect(hostInfo, resultProps);
    } catch (e: any) {
      if (!WrapperProperties.ENABLE_GREEN_HOST_REPLACEMENT.get(props)) {
        throw e;
      }

      if (!e.message.includes("Error: getaddrinfo ENOTFOUND")) {
        throw e;
      }

      if (!this.rdsUtils.isRdsDns(hostInfo.host) || !this.rdsUtils.isGreenInstance(hostInfo.host)) {
        throw e;
      }

      // check DNS for green host name
      let resolvedAddress;
      try {
        resolvedAddress = await promisify(lookup)(hostInfo.host, {});
      } catch (tmp) {
        // do nothing
      }

      if (resolvedAddress) {
        // Green node DNS exists
        throw e;
      }

      // Green instance DNS doesn't exist. Try to replace it with corresponding node name and connect again.
      const originalHost: string = hostInfo.host;
      const fixedHost: string = this.rdsUtils.removeGreenInstancePrefix(hostInfo.host);
      resultProps.set(WrapperProperties.HOST.name, fixedHost);

      logger.info(
        "Connecting to " +
          fixedHost +
          " after correcting the hostname from " +
          originalHost +
          "\nwith properties: \n" +
          JSON.stringify(Object.fromEntries(maskProperties(resultProps)))
      );

      resultTargetClient = driverDialect.connect(hostInfo, resultProps);
    }
    pluginService.attachErrorListener(resultTargetClient);
    return resultTargetClient;
  }