Skip to content

Commit

Permalink
Fast retry still alive
Browse files Browse the repository at this point in the history
  • Loading branch information
james58899 committed Mar 7, 2024
1 parent a07ac46 commit 5117167
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let client3 = client.clone();
let keepalive = tokio::spawn(async move {
let mut counter: u32 = 0;
let mut alive_interval = 11;
let mut next_run = Instant::now() + Duration::from_secs(10);
loop {
sleep_until(next_run).await;
Expand All @@ -309,8 +310,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

// Alive check every 110s
if counter % 11 == 0 && !client3.still_alive(false).await {
let _ = shutdown_send.send(()); // Check fail, shutdown.
if counter % alive_interval == 0 {
match client3.still_alive(false).await {
Some(true) => alive_interval = 11, // OK, reset interval
Some(false) => {
let _ = shutdown_send.send(()); // Check fail, shutdown.
}
None => alive_interval = 1, // Connection error, retry after 10s
}
}

// Check purge list every 7hr
Expand Down
29 changes: 17 additions & 12 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,20 +389,25 @@ The program will now terminate.
self.running.load(Ordering::Relaxed)
}

pub async fn still_alive(&self, resume: bool) -> bool {
if let Ok(res) = self.send_action("still_alive", Some(if resume { "resume" } else { "" })).await {
if res.is_ok() {
debug!("Successfully performed a stillAlive test for the server.");
} else if res.status == "TERM_BAD_NETWORK" {
error!("Client is shutting down since the network is misconfigured; correct firewall/forwarding settings then restart the client.");
return false; // Shutdown by RPC
} else {
warn!("Failed stillAlive test: ({}) - will retry later", res.status);
pub async fn still_alive(&self, resume: bool) -> Option<bool> {
match self.send_action("still_alive", Some(if resume { "resume" } else { "" })).await {
Ok(res) => {
if res.is_ok() {
debug!("Successfully performed a stillAlive test for the server.");
} else {
if res.status == "TERM_BAD_NETWORK" {
error!("Client is shutting down since the network is misconfigured; correct firewall/forwarding settings then restart the client.");
return Some(false);
}
warn!("Failed stillAlive test: ({}) - will retry later", res.status);
}
Some(true)
}
Err(_) => {
warn!("Failed to connect to the server for the stillAlive test. This is probably a temporary connection problem.");
None
}
} else {
warn!("Failed to connect to the server for the stillAlive test. This is probably a temporary connection problem.");
}
true
}

pub async fn notify_overload(&self) {
Expand Down

0 comments on commit 5117167

Please sign in to comment.