don't block the runloop when shutting down
This commit is contained in:
parent
b2de9352d3
commit
3dd7a7281b
4 changed files with 22 additions and 12 deletions
|
@ -139,6 +139,11 @@ impl App {
|
|||
KeyCode::Char('l') => self.logs(),
|
||||
KeyCode::Char('m') => self.main(),
|
||||
_ => match mode {
|
||||
CurrentScreen::Main => {
|
||||
if code == KeyCode::Char('d') {
|
||||
self.service.refresh_peers().await;
|
||||
}
|
||||
}
|
||||
CurrentScreen::Logging => match code {
|
||||
KeyCode::Left => change_log_level(-1),
|
||||
KeyCode::Right => change_log_level(1),
|
||||
|
@ -168,7 +173,6 @@ impl App {
|
|||
},
|
||||
SendingScreen::Text => unreachable!(),
|
||||
},
|
||||
CurrentScreen::Main => {}
|
||||
CurrentScreen::Stopping => unreachable!(),
|
||||
},
|
||||
},
|
||||
|
|
|
@ -29,6 +29,7 @@ impl JocalService {
|
|||
|
||||
let handle = Handle::new();
|
||||
self.http_handle.get_or_init(|| handle.clone());
|
||||
|
||||
log::info!("starting http server");
|
||||
|
||||
axum_server::bind_rustls(addr, ssl_config)
|
||||
|
|
|
@ -32,8 +32,6 @@ pub const MULTICAST_IP: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 167);
|
|||
pub const LISTENING_SOCKET_ADDR: SocketAddrV4 =
|
||||
SocketAddrV4::new(Ipv4Addr::from_bits(0), DEFAULT_PORT);
|
||||
|
||||
pub type ShutdownSender = mpsc::Sender<()>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Listeners {
|
||||
Udp,
|
||||
|
@ -79,7 +77,7 @@ pub struct JocalService {
|
|||
pub socket: Arc<UdpSocket>,
|
||||
pub client: reqwest::Client,
|
||||
pub config: Config,
|
||||
http_handle: Arc<OnceLock<axum_server::Handle>>,
|
||||
http_handle: OnceLock<axum_server::Handle>,
|
||||
// the receiving end will be held by the application so it can update the UI based on backend
|
||||
// events
|
||||
transfer_event_tx: UnboundedSender<TransferEvent>,
|
||||
|
|
23
src/main.rs
23
src/main.rs
|
@ -55,7 +55,9 @@ async fn start_and_run(terminal: &mut DefaultTerminal, config: Config) -> Result
|
|||
|
||||
let mut handles = JoinSet::new();
|
||||
app.service.start(&mut handles).await;
|
||||
let mut alarm = tokio::time::interval(Duration::from_millis(200));
|
||||
let mut tick = tokio::time::interval(Duration::from_millis(200));
|
||||
let shutdown = shutdown(&mut handles);
|
||||
let mut shutdown = std::pin::pin!(shutdown);
|
||||
loop {
|
||||
terminal.draw(|frame| app.draw(frame))?;
|
||||
|
||||
|
@ -63,11 +65,18 @@ async fn start_and_run(terminal: &mut DefaultTerminal, config: Config) -> Result
|
|||
res = app.handle_events() => {
|
||||
res?;
|
||||
}
|
||||
_ = alarm.tick() => {}
|
||||
_ = tick.tick() => {}
|
||||
}
|
||||
|
||||
if app.screen() == CurrentScreen::Stopping {
|
||||
break;
|
||||
tokio::select! {
|
||||
_ = shutdown.as_mut() => {
|
||||
break;
|
||||
}
|
||||
_ = tick.tick() => {
|
||||
log::info!("shutting down");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let peers = app.service.peers.lock().await;
|
||||
|
@ -94,14 +103,12 @@ async fn start_and_run(terminal: &mut DefaultTerminal, config: Config) -> Result
|
|||
}
|
||||
}
|
||||
|
||||
shutdown(&mut handles).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn shutdown(handles: &mut JoinSet<Listeners>) {
|
||||
let mut alarm = tokio::time::interval(Duration::from_secs(5));
|
||||
alarm.tick().await;
|
||||
let mut timeout = tokio::time::interval(Duration::from_secs(5));
|
||||
timeout.tick().await;
|
||||
loop {
|
||||
tokio::select! {
|
||||
join_result = handles.join_next() => {
|
||||
|
@ -113,7 +120,7 @@ async fn shutdown(handles: &mut JoinSet<Listeners>) {
|
|||
None => break,
|
||||
}
|
||||
}
|
||||
_ = alarm.tick() => {
|
||||
_ = timeout.tick() => {
|
||||
info!("Exit timeout reached, aborting all unjoined tasks");
|
||||
handles.abort_all();
|
||||
break;
|
||||
|
|
Loading…
Reference in a new issue