Reconnect on connection closed
See original GitHub issueWe are building an upload tool that must be resilient to connection loss. Right now when the connection is lost while an append or put operation is in progress, we get a “close” event, but the append or put operations don’t resolve or throw. Currently I’m wrapping our upload as such:
try {
const success = await new Promise<boolean>(resolv => {
const listener = (s: boolean) => {
this.client.removeListener("close", listener);
resolv(s);
};
this.client.on("close", () => listener(false));
// If the connection is lost, neither of these ever resolve.
if (tryCont) {
this.client.append(stream, destFile).then(() => listener(true));
} else {
this.client.put(stream, destFile).then(() => listener(true));
}
});
if (!success) throw new Error("Connection lost");
} catch (e) {
console.log(e);
throw e;
}
This works, but feels like its leaking in the append and put calls since they are not resolving.
Along the same lines, calling connect multiple times to test for a connection results in the sftp server erroring out with a long list of “no more sessons” errors when a connection is finally made. Its like each failed connect call finally makes a connection even when it reports failure when its initially called. Is there an intended approach for doing this kind of thing? Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Comments:22 (20 by maintainers)
Top GitHub Comments
Yes, I think your right. Once we have exhausted all attemptw and done the error callback, we should just return. Have added it.
Looking at the code, it appears to me that there should be a return after line 111 in index.js. If there is an error, we shouldn’t continue on after the callback is executed. Does that look right to you?