From d08945844b64a4f25f87434aab0f7ee001b81e93 Mon Sep 17 00:00:00 2001 From: EAimTY Date: Fri, 3 Feb 2023 00:59:19 +0900 Subject: [PATCH] adding gc for fragmentary packet --- tuic-client/src/config.rs | 18 +++++++++++++++--- tuic-client/src/connection.rs | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/tuic-client/src/config.rs b/tuic-client/src/config.rs index eb06b28..8c2a0b6 100644 --- a/tuic-client/src/config.rs +++ b/tuic-client/src/config.rs @@ -58,8 +58,12 @@ pub struct Relay { pub timeout: Duration, #[serde(default = "default::relay::heartbeat")] pub heartbeat: Duration, - #[serde(default = "default::relay::disable_native_certificates")] - pub disable_native_certificates: bool, + #[serde(default = "default::relay::disable_native_certs")] + pub disable_native_certs: bool, + #[serde(default = "default::relay::gc_interval")] + pub gc_interval: Duration, + #[serde(default = "default::relay::gc_lifetime")] + pub gc_lifetime: Duration, } #[derive(Deserialize)] @@ -141,9 +145,17 @@ mod default { Duration::from_secs(3) } - pub fn disable_native_certificates() -> bool { + pub fn disable_native_certs() -> bool { false } + + pub fn gc_interval() -> Duration { + Duration::from_secs(3) + } + + pub fn gc_lifetime() -> Duration { + Duration::from_secs(15) + } } pub mod local { diff --git a/tuic-client/src/connection.rs b/tuic-client/src/connection.rs index 38787d7..dfb7bb3 100644 --- a/tuic-client/src/connection.rs +++ b/tuic-client/src/connection.rs @@ -44,11 +44,13 @@ pub struct Endpoint { udp_relay_mode: UdpRelayMode, zero_rtt_handshake: bool, heartbeat: Duration, + gc_interval: Duration, + gc_lifetime: Duration, } impl Endpoint { pub fn set_config(cfg: Relay) -> Result<(), Error> { - let certs = utils::load_certs(cfg.certificates, cfg.disable_native_certificates)?; + let certs = utils::load_certs(cfg.certificates, cfg.disable_native_certs)?; let mut crypto = RustlsClientConfig::builder() .with_safe_default_cipher_suites() @@ -95,6 +97,8 @@ impl Endpoint { udp_relay_mode: cfg.udp_relay_mode, zero_rtt_handshake: cfg.zero_rtt_handshake, heartbeat: cfg.heartbeat, + gc_interval: cfg.gc_interval, + gc_lifetime: cfg.gc_lifetime, }; ENDPOINT @@ -155,7 +159,12 @@ impl Endpoint { .await { Ok(conn) => { - tokio::spawn(conn.clone().init(self.token.clone(), self.heartbeat)); + tokio::spawn(conn.clone().init( + self.token.clone(), + self.heartbeat, + self.gc_interval, + self.gc_lifetime, + )); return Ok(conn); } Err(err) => last_err = Some(err), @@ -383,9 +392,28 @@ impl Connection { } } - async fn init(self, token: Arc<[u8]>, heartbeat: Duration) { + async fn collect_garbage(self, gc_interval: Duration, gc_lifetime: Duration) { + loop { + time::sleep(gc_interval).await; + + if self.is_closed() { + break; + } + + self.model.collect_garbage(gc_lifetime); + } + } + + async fn init( + self, + token: Arc<[u8]>, + heartbeat: Duration, + gc_interval: Duration, + gc_lifetime: Duration, + ) { tokio::spawn(self.clone().authenticate(token)); tokio::spawn(self.clone().heartbeat(heartbeat)); + tokio::spawn(self.clone().collect_garbage(gc_interval, gc_lifetime)); let err = loop { tokio::select! {