如何在 Node js 中处理并发 sftp 连接。出现错误 没有可用的 sftp 连接

vij*_*ala 9 concurrency singleton sftp node.js

在此处输入图片说明 对于 sftp,我们使用最新版本的 ssh2-sftp-client 模块(5.1.3) 服务文件(sftpConnectionService)和 sftp 连接 {Error: get->get: No SFTP connection available}

class SftpConnectionService {
  constructor() {
    this.sftp = null;
  }
  async getSftpConnection(randomDigit) {
    try {
      this.sftp = new Client(`connection-${randomDigit}`);
      await this.sftp.connect({
        host: sftpconfig.host,
        port: sftpconfig.port,
        username: sftpconfig.username,
        password: sftpconfig.password,
      });
      return this.sftp;
    } catch (exception) {
        throw exception;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

获取连接并下载文件的js文件

   downloadFile(){   
     let sftp;
      const randomDigit = generateRandomCodeSrvc.generateCode(4);
      sftp = await sftpConnectionService.getSftpConnection(randomDigit);
      const remoteDir = `month/statement/filename.pdf`
     let bufferStream = await sftp.get(remoteDir);
     if (sftp) await sftp.end();
   } 
Run Code Online (Sandbox Code Playgroud)