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

discussion: timeout handling using AbortController #1299

Open
iambighead opened this issue May 19, 2023 · 1 comment
Open

discussion: timeout handling using AbortController #1299

iambighead opened this issue May 19, 2023 · 1 comment

Comments

@iambighead
Copy link

Not sure if this is discussed before. Can ssh2 support AbortController so certain operations can be cancellable due to say timeout?

Currently I implemented a timeout before invoking FastGet. But while the main app timeout the operation (do not wait for it to finish), I believe FastGet actually is still running. So I am looking for a way to cancel the operation when timeout, without terminating the connection. The advantage is I can reduce unnecessary bandwidth usage, and clean up the file without the file being locked.

Thanks for your time.

@BitStream1
Copy link

BitStream1 commented Jun 3, 2023

One option to get cancel/timeout to work with fastGet/Put, would be to modify opts.step() in fastXfer() to error if step() returns true.

https://github.com/mscdex/ssh2/blob/281c290fc706ed3c2215f5e80a90a2e2498368f3/lib/protocol/SFTP.js#LL2331C48-L2331C48

e.g. Change (In ssh2 library):

             onstep && onstep(total, nb, fsize);

To:

              // Abort if onstep returns true
              if (onstep && onstep(total, nb, fsize)) {
                return onerror(new Error('Cancelled by user'));
              }

Then you could use the following (application code) to cancel fastGet()/fastPut() mid-transfer.

            var opts = {
                step: (transferred, chunk, total)=>{
                    if (canceled())
                        return true;
                }
            };
            fastGet(..., opts);

But could be a breaking change if someone's incorrectly returning a value from step(), though.

It would also be handy to expose fastXfer() in the public API, so you can copy files between ftp servers. fastGet/Put() only work local->remote or remote->local, but sometimes you may need to do remote->remote.

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

No branches or pull requests

3 participants