v1.0.71: SFTP connection pooling — reuse SSH session across all operations
Build & Release APK / build (push) Failing after 20m59s
Build & Release APK / build (push) Failing after 20m59s
Previously every listFiles/uploadFile/downloadFile/deleteFile call created a fresh SSH connection (connect → auth → use → disconnect). For zahra's folder with 69 subdirectories, the recursive listing alone made 70 full SSH handshakes, then one more per downloaded file — causing connection timeouts and 65 upload/download failures reported as PARTIAL. Now the provider holds a persistent SSH session and reuses it for all calls, reconnecting automatically if the connection drops.
This commit is contained in:
@@ -23,19 +23,36 @@ class SftpProvider(private val account: CloudAccount, private val credentialStor
|
||||
private val password = creds["password"]?.jsonPrimitive?.content
|
||||
private val privateKey = creds["private_key"]?.jsonPrimitive?.content
|
||||
|
||||
private fun <T> withSftp(block: (SFTPClient) -> T): T {
|
||||
// Persistent SSH connection reused across all operations in the provider's lifetime.
|
||||
// Each call to withSftp checks liveness and reconnects if the connection dropped.
|
||||
// This eliminates the per-operation connect/auth/disconnect cycle that caused
|
||||
// 100+ SSH handshakes during a recursive directory listing + file-transfer sync,
|
||||
// leading to connection timeouts on large folder trees (e.g. 69 subdirectories).
|
||||
private var sshClient: SSHClient? = null
|
||||
|
||||
private fun getOrCreateSsh(): SSHClient {
|
||||
val existing = sshClient
|
||||
if (existing != null && existing.isConnected && existing.isAuthenticated) return existing
|
||||
val ssh = SSHClient()
|
||||
ssh.addHostKeyVerifier(TofuHostKeyVerifier(credentialStore))
|
||||
ssh.connect(host, port)
|
||||
try {
|
||||
if (!privateKey.isNullOrBlank()) {
|
||||
ssh.authPublickey(username, ssh.loadKeys(privateKey, null, null))
|
||||
} else {
|
||||
ssh.authPassword(username, password ?: "")
|
||||
}
|
||||
return ssh.newSFTPClient().use(block)
|
||||
} finally {
|
||||
ssh.disconnect()
|
||||
sshClient = ssh
|
||||
return ssh
|
||||
}
|
||||
|
||||
private fun <T> withSftp(block: (SFTPClient) -> T): T {
|
||||
return try {
|
||||
getOrCreateSsh().newSFTPClient().use(block)
|
||||
} catch (e: Exception) {
|
||||
// Connection may have gone stale — reset and retry once with a fresh connection.
|
||||
runCatching { sshClient?.disconnect() }
|
||||
sshClient = null
|
||||
getOrCreateSsh().newSFTPClient().use(block)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -1,2 +1,2 @@
|
||||
VERSION_NAME=1.0.70
|
||||
VERSION_CODE=70
|
||||
VERSION_NAME=1.0.71
|
||||
VERSION_CODE=71
|
||||
|
||||
Reference in New Issue
Block a user