Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to send a message to Client to stop sending data while uploading files? #180

Open
ishan111111 opened this issue Nov 6, 2021 · 0 comments

Comments

@ishan111111
Copy link

ishan111111 commented Nov 6, 2021

We have a server based on ssh2 and ssh2-streams package. During uploading files, we are pushing data to a readable stream which is consumed by some other method. Is there a way to communicate to client to stop sending any more data when our readable stream's buffer is full?

I have observed that if we don't send the OK response from the write event handler from server, the client stops sending data after like 60 chunks and waits for OK response for corresponding requestId's before sending any more data.

In the previous version of ssh2 , we were doing something like this:

const myReadable = new MyReadable({}, this.sftpStream);
this.sftpStream.on('WRITE', (requestId, handle, offset, data) => { 
   myReadable.pushData(data);

  this.sftpStream.status(requestId, SFTP_STATUS_CODE.OK) 

 });
 class MyReadable extends stream.Readable {

    constructor(options, sftpStream) {
        super(options);
        this.sftpStream = sftpStream;
    }

    pushData = (data) => {
        if (!this.push(data) && !this.sftpStream.isPaused()) {
            this.sftpStream.pause();
        }
    }
    
    _read() {
        if (this.sftpStream.isPaused()) {
            this.sftpStream.resume();
        }
    }
}

I am not sure but the above code must have caused the sftpStream to go in paused mode when calling this.sftpStream.pause() and it might have stopped sending response back to client until this.sftpStream.resume() is called, switching back the sftpStream into flowing mode.

Since the new version of ssh2 is released, we don't have pause and resume methods available on this.sftpStream.
We have modified the code something like this:

const myReadable = new MyReadable({}, this.sftpStream);
this.sftpStream.on('WRITE', (requestId, handle, offset, data) => {
    
    if (!myReadable.pushData(data))
    return myReadable.pushToPending(requestId)


 
    this.sftpStream.status(requestId, SFTP_STATUS_CODE.OK)
});
class MyReadable extends stream.Readable {

    pendingResponse = [];


    constructor(options, sftpStream) {
        super(options);
        this.sftpStream = sftpStream;
    }

    pushData = (data) => {
       return this.push(data);
    }


    _read() {
      while (this.pendingResponse.length > 0) {
          const requestId = this.pendingResponse.shift();
          this.sftpStream.status(requestId, SFTP_STATUS_CODE.OK);
      }
    }

    pushToPending(requestId) {
        this.pendingResponse.push(requestId)
    }
}

Can you please give me suggestion on if it is a good way to handle such scenario or some better way exists?
Is there a way to send a message from server to client to pause and resume the upload?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant