Skip to content

web search done right

for some reason, poll fn seems to block sometimes when checking if tcpstream is closed. let's instead check by trying to read 0-bytes with a timeout and see if it returns an err

Mikkel Denker798ef2fe9e73
diff --git a/crates/core/src/distributed/sonic/mod.rs b/crates/core/src/distributed/sonic/mod.rs
index 32e93749..1d747e28 100644
--- a/crates/core/src/distributed/sonic/mod.rs
+++ b/crates/core/src/distributed/sonic/mod.rs
@@ -20,11 +20,10 @@ pub mod service;
pub use connection_pool::ConnectionPool;
-use std::{marker::PhantomData, task::Poll, time::Duration};
+use std::{marker::PhantomData, time::Duration};
-use futures::future::poll_fn;
use tokio::{
- io::{AsyncReadExt, AsyncWriteExt, ReadBuf},
+ io::{AsyncReadExt, AsyncWriteExt},
net::{TcpListener, TcpStream, ToSocketAddrs},
};
@@ -162,14 +161,11 @@ where
}
}
- pub async fn is_closed(&self) -> bool {
- poll_fn(
- |cx| match self.stream.poll_peek(cx, &mut ReadBuf::new(&mut [0u8])) {
- Poll::Ready(t) => Poll::Ready(t.is_err()),
- Poll::Pending => Poll::Ready(false),
- },
+ pub async fn is_closed(&mut self) -> bool {
+ !matches!(
+ tokio::time::timeout(Duration::from_secs(1), self.stream.read_exact(&mut [])).await,
+ Ok(Ok(_))
)
- .await
}
}
diff --git a/crates/core/src/distributed/sonic/service.rs b/crates/core/src/distributed/sonic/service.rs
index 01a40715..2376c424 100644
--- a/crates/core/src/distributed/sonic/service.rs
+++ b/crates/core/src/distributed/sonic/service.rs
@@ -176,7 +176,7 @@ impl<S: Service> Connection<S> {
.collect())
}
- pub async fn is_closed(&self) -> bool {
+ pub async fn is_closed(&mut self) -> bool {
self.inner.is_closed().await
}
}