1
0

adding gc for fragmentary packet

This commit is contained in:
EAimTY 2023-02-03 00:59:19 +09:00
parent 2eeaec66d4
commit d08945844b
2 changed files with 46 additions and 6 deletions

View File

@ -58,8 +58,12 @@ pub struct Relay {
pub timeout: Duration, pub timeout: Duration,
#[serde(default = "default::relay::heartbeat")] #[serde(default = "default::relay::heartbeat")]
pub heartbeat: Duration, pub heartbeat: Duration,
#[serde(default = "default::relay::disable_native_certificates")] #[serde(default = "default::relay::disable_native_certs")]
pub disable_native_certificates: bool, 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)] #[derive(Deserialize)]
@ -141,9 +145,17 @@ mod default {
Duration::from_secs(3) Duration::from_secs(3)
} }
pub fn disable_native_certificates() -> bool { pub fn disable_native_certs() -> bool {
false false
} }
pub fn gc_interval() -> Duration {
Duration::from_secs(3)
}
pub fn gc_lifetime() -> Duration {
Duration::from_secs(15)
}
} }
pub mod local { pub mod local {

View File

@ -44,11 +44,13 @@ pub struct Endpoint {
udp_relay_mode: UdpRelayMode, udp_relay_mode: UdpRelayMode,
zero_rtt_handshake: bool, zero_rtt_handshake: bool,
heartbeat: Duration, heartbeat: Duration,
gc_interval: Duration,
gc_lifetime: Duration,
} }
impl Endpoint { impl Endpoint {
pub fn set_config(cfg: Relay) -> Result<(), Error> { 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() let mut crypto = RustlsClientConfig::builder()
.with_safe_default_cipher_suites() .with_safe_default_cipher_suites()
@ -95,6 +97,8 @@ impl Endpoint {
udp_relay_mode: cfg.udp_relay_mode, udp_relay_mode: cfg.udp_relay_mode,
zero_rtt_handshake: cfg.zero_rtt_handshake, zero_rtt_handshake: cfg.zero_rtt_handshake,
heartbeat: cfg.heartbeat, heartbeat: cfg.heartbeat,
gc_interval: cfg.gc_interval,
gc_lifetime: cfg.gc_lifetime,
}; };
ENDPOINT ENDPOINT
@ -155,7 +159,12 @@ impl Endpoint {
.await .await
{ {
Ok(conn) => { 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); return Ok(conn);
} }
Err(err) => last_err = Some(err), 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().authenticate(token));
tokio::spawn(self.clone().heartbeat(heartbeat)); tokio::spawn(self.clone().heartbeat(heartbeat));
tokio::spawn(self.clone().collect_garbage(gc_interval, gc_lifetime));
let err = loop { let err = loop {
tokio::select! { tokio::select! {