AspectUnk / russh-sftp

SFTP subsystem supported server and client for Russh
Apache License 2.0
62 stars 21 forks source link

ERROR - Disconnected #49

Open TheMuteM opened 2 weeks ago

TheMuteM commented 2 weeks ago

When I am on a server with high latency, it is normal to call upd_content at the beginning. But after a few minutes, ERROR - Disconnected will appear when calling

pub async fn create_sftp_session(session: &Session) -> Result<SftpSession> {
    let sftp_channel = session.channel_open_session().await?;
    sftp_channel.request_subsystem(true, "sftp").await?;
    let session1 = SftpSession::new(sftp_channel.into_stream()).await?;
    session1.set_timeout(2000).await;
    Ok(session1)
}

    pub async fn upd_content(&self, file_path: String, content: String) -> Result<()> {
        info!("upd_content file_path:{}",file_path);
        let guard = self.ssh_session.lock().await;
        let session = guard.as_ref().unwrap();
        let sftp_channel = create_sftp_session(&session).await?;

        let mut remote_file = sftp_channel.open_with_flags(file_path, OpenFlags::CREATE | OpenFlags::TRUNCATE | OpenFlags::WRITE).await?;
        // file.write_all(content.as_ref()).await?;
        // let mut remote_file = sftp_channel.create(&remote_path).await?;

        const CHUNK_SIZE: usize = 5120; 
        let mut pos = 0;
        let content_bytes = content.as_bytes();
        while pos < content_bytes.len() {
            let end = std::cmp::min(pos + CHUNK_SIZE, content_bytes.len());
            remote_file.write_all(&content_bytes[pos..end]).await?;
            pos = end;
        }
        remote_file.shutdown().await?;
        sftp_channel.close().await?;
        Ok(())
    }

Config

   let mut config = client::Config {
        inactivity_timeout: Some(Duration::from_secs(20000)),
        keepalive_interval: Some(Duration::from_secs(5000)),
        keepalive_max: 6,
        ..<_>::default()
    };
AspectUnk commented 1 day ago

Please attach full debug logs and/or a working example out of the box