async switchToReaderTargetClient()

in common/lib/plugins/read_write_splitting_plugin.ts [336:366]


  async switchToReaderTargetClient(hosts: HostInfo[]) {
    const currentHost = this.pluginService.getCurrentHostInfo();
    const currentClient = this.pluginService.getCurrentClient();
    if (currentHost !== null && currentHost?.role === HostRole.READER && currentClient) {
      return;
    }

    if (this._readerHostInfo && !hosts.some((hostInfo: HostInfo) => hostInfo.host === this._readerHostInfo?.host)) {
      // The old reader cannot be used anymore because it is no longer in the list of allowed hosts.
      await this.closeTargetClientIfIdle(this.readerTargetClient);
    }

    this._inReadWriteSplit = true;
    if (!(await this.isTargetClientUsable(this.readerTargetClient))) {
      await this.initializeReaderClient(hosts);
    } else if (this.readerTargetClient != null && this._readerHostInfo != null) {
      try {
        await this.switchCurrentTargetClientTo(this.readerTargetClient, this._readerHostInfo);
        logger.debug(Messages.get("ReadWriteSplittingPlugin.switchedFromWriterToReader", this._readerHostInfo.url));
      } catch (error: any) {
        logger.debug(Messages.get("ReadWriteSplittingPlugin.errorSwitchingToCachedReader", this._readerHostInfo.url));
        await this.pluginService.abortTargetClient(this.readerTargetClient);
        this.readerTargetClient = undefined;
        this._readerHostInfo = undefined;
        await this.initializeReaderClient(hosts);
      }
    }
    if (this.isWriterClientFromInternalPool) {
      await this.closeTargetClientIfIdle(this.writerTargetClient);
    }
  }