From 6a0349287066261d7953b4220de5dd4df05049c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 11 Apr 2021 22:53:04 +0200 Subject: use tokio timeout --- mumd/src/network/udp.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'mumd/src') diff --git a/mumd/src/network/udp.rs b/mumd/src/network/udp.rs index d8cbc2a..2ca8f85 100644 --- a/mumd/src/network/udp.rs +++ b/mumd/src/network/udp.rs @@ -14,9 +14,9 @@ use std::collections::{hash_map::Entry, HashMap}; use std::convert::TryFrom; use std::net::{Ipv6Addr, SocketAddr}; use std::sync::{atomic::{AtomicU64, Ordering}, Arc, RwLock}; -use tokio::{join, net::UdpSocket, select}; +use tokio::{join, net::UdpSocket}; use tokio::sync::{mpsc, oneshot, watch, Mutex}; -use tokio::time::{interval, Duration}; +use tokio::time::{interval, timeout, Duration}; use tokio_util::udp::UdpFramed; use super::{run_until, VoiceStreamType}; @@ -246,20 +246,19 @@ pub async fn handle_pings( } tokio::spawn(async move { - let rx = rx.fuse(); - let timeout = async { - tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await; - }; - handle(select! { - r = rx => match r { - Ok(r) => Some(r), + handle( + match timeout(Duration::from_secs(1), rx).await { + Ok(Ok(r)) => Some(r), + Ok(Err(_)) => { + warn!("Ping response sender for server {}, ping id {} dropped", socket_addr, id); + None + } Err(_) => { - warn!("Ping response sender dropped"); + debug!("Server {} timed out when sending ping id {}", socket_addr, id); None } - }, - _ = timeout => None, - }); + } + ); }); } }; -- cgit v1.2.1