From 81a98c3d3c44732f04180a83fda450b493c30db5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Thu, 26 Aug 2021 22:42:10 +0200 Subject: [PATCH] slightly better handling of socket going silent, maybe --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/group_handler/mod.rs | 36 ++++++++++++++++++++++-------------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 561e833..9f37efc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -328,7 +328,7 @@ checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" [[package]] name = "fedigroups" -version = "0.2.0" +version = "0.2.1" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 9bec613..6533fa4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fedigroups" -version = "0.2.0" +version = "0.2.1" authors = ["Ondřej Hruška "] edition = "2018" publish = false diff --git a/src/group_handler/mod.rs b/src/group_handler/mod.rs index d5b260b..ca1879b 100644 --- a/src/group_handler/mod.rs +++ b/src/group_handler/mod.rs @@ -35,6 +35,8 @@ const MAX_CATCHUP_NOTIFS: usize = 25; const MAX_CATCHUP_STATUSES: usize = 50; // higher because we can expect a lot of non-hashtag statuses here const PERIODIC_SAVE: Duration = Duration::from_secs(60); +const SOCKET_ALIVE_TIMEOUT: Duration = Duration::from_secs(30); +const SOCKET_RETIRE_TIME: Duration = Duration::from_secs(120); const PING_INTERVAL: Duration = Duration::from_secs(15); // must be < periodic save! impl GroupHandle { @@ -93,7 +95,7 @@ impl GroupHandle { let mut events = self.client.streaming_user().await?; let socket_open_time = Instant::now(); let mut last_rx = Instant::now(); - let mut last_ping = Instant::now(); + // let mut last_ping = Instant::now(); match self.catch_up_with_missed_notifications().await { Ok(true) => { @@ -131,12 +133,14 @@ impl GroupHandle { next_save = Instant::now() + PERIODIC_SAVE; } - if last_rx.elapsed() > PING_INTERVAL * 2 { + let remains_to_idle_close = SOCKET_ALIVE_TIMEOUT.saturating_sub(last_rx.elapsed()); + let remains_to_retire = SOCKET_RETIRE_TIME.saturating_sub(socket_open_time.elapsed()); + + if remains_to_idle_close.is_zero() { warn!("Socket idle too long, close"); break 'rx; } - - if socket_open_time.elapsed() > Duration::from_secs(120) { + if remains_to_retire.is_zero() { debug!("Socket open too long, closing"); break 'rx; } @@ -144,8 +148,9 @@ impl GroupHandle { trace!("Waiting for message"); let timeout = next_save .saturating_duration_since(Instant::now()) - .min(PING_INTERVAL) - .max(Duration::from_secs(1)); + .min(remains_to_idle_close) + .min(remains_to_retire) + .max(Duration::from_secs(1)); // at least 1s match tokio::time::timeout(timeout, events.next()).await { Ok(Some(event)) => { @@ -172,14 +177,17 @@ impl GroupHandle { } } - if last_ping.elapsed() > PING_INTERVAL { - last_ping = Instant::now(); - trace!("Pinging"); - if events.send_ping() - .await.is_err() { - break 'rx; - } - } + /* ping is nice, but pleroma still sometimes doesnt send + notifs after a while, just let it expire */ + + // if last_ping.elapsed() > PING_INTERVAL { + // last_ping = Instant::now(); + // trace!("Pinging"); + // if events.send_ping() + // .await.is_err() { + // break 'rx; + // } + // } } warn!("Notif stream closed, will reopen");