From 8a17949c88559fb6cb702357c9033b858ddcd812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E9=B8=BF=E5=BF=97?= <2064338943@qq.com> Date: Thu, 15 May 2025 22:04:25 +0800 Subject: [PATCH 1/2] Client add reconnect --- crates/sshx/src/controller.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/sshx/src/controller.rs b/crates/sshx/src/controller.rs index 5eff767..dc012da 100644 --- a/crates/sshx/src/controller.rs +++ b/crates/sshx/src/controller.rs @@ -14,7 +14,7 @@ use tokio::task; use tokio::time::{self, Duration, Instant, MissedTickBehavior}; use tokio_stream::{wrappers::ReceiverStream, StreamExt}; use tonic::transport::Channel; -use tracing::{debug, error, warn}; +use tracing::{debug, error, info, warn}; use crate::encrypt::Encrypt; use crate::runner::{Runner, ShellData}; @@ -146,6 +146,28 @@ impl Controller { let mut retries = 0; loop { if let Err(err) = self.try_channel().await { + if let Some(status) = err.downcast_ref::() { + // server not found this session id + if status.code() == tonic::Code::NotFound && status.message() == "session not found" { + match Controller::new( + &self.origin, + &self.name, + self.runner.clone(), + self.write_url.is_some(), + ).await { + Ok(new_controller) => { + // successfully rebuilt the connection + // replaced controller + *self = new_controller; + info!("recreate session success"); + continue; // 直接进入下一次循环 + } + Err(e) => { + error!(error = ?e, "failed to recreate session"); + } + } + } + } if last_retry.elapsed() >= Duration::from_secs(10) { retries = 0; } From 783d7a22d364720e115d96a016d0b8223375908d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E9=B8=BF=E5=BF=97?= <2064338943@qq.com> Date: Thu, 15 May 2025 22:27:39 +0800 Subject: [PATCH 2/2] code fmt --- crates/sshx/src/controller.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/sshx/src/controller.rs b/crates/sshx/src/controller.rs index dc012da..1f9f819 100644 --- a/crates/sshx/src/controller.rs +++ b/crates/sshx/src/controller.rs @@ -148,19 +148,23 @@ impl Controller { if let Err(err) = self.try_channel().await { if let Some(status) = err.downcast_ref::() { // server not found this session id - if status.code() == tonic::Code::NotFound && status.message() == "session not found" { + if status.code() == tonic::Code::NotFound + && status.message() == "session not found" + { match Controller::new( &self.origin, &self.name, self.runner.clone(), self.write_url.is_some(), - ).await { + ) + .await + { Ok(new_controller) => { // successfully rebuilt the connection // replaced controller *self = new_controller; info!("recreate session success"); - continue; // 直接进入下一次循环 + continue; } Err(e) => { error!(error = ?e, "failed to recreate session");