Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

sftp: fix timeout on hashing large files by sending keepalives

Nick Craig-Wooda351484997b8
diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go
index ebdc4a24c..7df0f5286 100644
--- a/backend/sftp/sftp.go
+++ b/backend/sftp/sftp.go
@@ -42,7 +42,8 @@ const (
hashCommandNotSupported = "none"
minSleep = 100 * time.Millisecond
maxSleep = 2 * time.Second
- decayConstant = 2 // bigger for slower decay, exponential
+ decayConstant = 2 // bigger for slower decay, exponential
+ keepAliveInterval = time.Minute // send keepalives every this long while running commands
)
var (
@@ -339,6 +340,32 @@ func (c *conn) wait() {
c.err <- c.sshClient.Conn.Wait()
}
+// Send a keepalive over the ssh connection
+func (c *conn) sendKeepAlive() {
+ _, _, err := c.sshClient.SendRequest("[email protected]", true, nil)
+ if err != nil {
+ fs.Debugf(nil, "Failed to send keep alive: %v", err)
+ }
+}
+
+// Send keepalives every interval over the ssh connection until done is closed
+func (c *conn) sendKeepAlives(interval time.Duration) (done chan struct{}) {
+ done = make(chan struct{})
+ go func() {
+ t := time.NewTicker(interval)
+ defer t.Stop()
+ for {
+ select {
+ case <-t.C:
+ c.sendKeepAlive()
+ case <-done:
+ return
+ }
+ }
+ }()
+ return done
+}
+
// Closes the connection
func (c *conn) close() error {
sftpErr := c.sftpClient.Close()
@@ -1098,6 +1125,9 @@ func (f *Fs) run(ctx context.Context, cmd string) ([]byte, error) {
}
defer f.putSftpConnection(&c, err)
+ // Send keepalives while the connection is open
+ defer close(c.sendKeepAlives(keepAliveInterval))
+
session, err := c.sshClient.NewSession()
if err != nil {
return nil, fmt.Errorf("run: get SFTP session: %w", err)
diff --git a/docs/content/sftp.md b/docs/content/sftp.md
index fc1e5fad8..8236afb3c 100644
--- a/docs/content/sftp.md
+++ b/docs/content/sftp.md
@@ -620,7 +620,7 @@ issue](https://github.com/pkg/sftp/issues/156) is fixed.
Note that since SFTP isn't HTTP based the following flags don't work
with it: `--dump-headers`, `--dump-bodies`, `--dump-auth`
-Note that `--timeout` isn't supported (but `--contimeout` is).
+Note that `--timeout` and `--contimeout` are both supported.
## C14 {#c14}