Skip to content

Commit

Permalink
improv: await all handlers before exit
Browse files Browse the repository at this point in the history
this ensures we don't leave any IO action in progress
  • Loading branch information
imLinguin committed Jul 27, 2024
1 parent 8ecce3f commit 37d410c
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{collections::HashMap, sync::Arc};

use clap::{Parser, Subcommand};
use env_logger::{Builder, Env, Target};
use futures_util::future::join_all;
use log::{error, info, warn};
use reqwest::Client;
use tokio::net::TcpListener;
Expand Down Expand Up @@ -271,6 +272,7 @@ async fn main() {
};
let mut ever_connected = false;
let mut active_clients = 0;
let mut handlers = Vec::new();
loop {
let (socket, _addr) = tokio::select! {
accept = listener.accept() => {
Expand Down Expand Up @@ -303,7 +305,7 @@ async fn main() {
let client_exit = client_exit.clone();
active_clients += 1;
ever_connected = args.quit;
tokio::spawn(async move {
handlers.push(tokio::spawn(async move {
api::handlers::entry_point(
socket,
cloned_client,
Expand All @@ -314,9 +316,10 @@ async fn main() {
)
.await;
let _ = client_exit.send(true);
});
}));
}

// Ignore errors, we are exiting
let _ = pusher_handle.await;
join_all(handlers).await;
}

0 comments on commit 37d410c

Please sign in to comment.