diff --git a/Cargo.lock b/Cargo.lock index 887906f..b90c1fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -689,6 +689,21 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "futures" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.31" @@ -705,6 +720,23 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" + [[package]] name = "futures-macro" version = "0.3.31" @@ -734,10 +766,13 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" dependencies = [ + "futures-channel", "futures-core", + "futures-io", "futures-macro", "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", "slab", @@ -1136,6 +1171,7 @@ dependencies = [ "chrono", "clap", "env_logger", + "futures", "handlebars", "imageproc", "include_dir", diff --git a/Cargo.toml b/Cargo.toml index 326df24..4bc9412 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ webpki-roots = "0.26.8" rustls-pemfile = "2.2.0" socket2 = "0.5.8" listenfd = "1.0.2" +futures = "0.3.31" #ip maxminddb = "0.25.0" #image processing diff --git a/src/http/tcp_socket.rs b/src/http/tcp_socket.rs index a9135e6..ec362b5 100644 --- a/src/http/tcp_socket.rs +++ b/src/http/tcp_socket.rs @@ -7,6 +7,7 @@ use std::io::{Error, ErrorKind}; use std::net::{IpAddr, SocketAddr}; use std::str::FromStr; use tokio::net::{TcpListener, TcpStream}; +use futures::future::select_all; pub struct TcpSocket { tcp_listeners: Vec, @@ -62,16 +63,18 @@ impl TcpSocket { } pub async fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> { - for listener in &self.tcp_listeners { - tokio::select! { - result = listener.accept() => { - let (stream, addr) = result?; - return Ok((stream,addr)); - } - _ = tokio::time::sleep(std::time::Duration::from_millis(1)) => {} - } + if self.tcp_listeners.is_empty() { + return Err(Error::new( + ErrorKind::NotConnected, + "No listeners found.", + )); } - Err(Error::new(ErrorKind::Other, "No listeners found.")) + + let accept_futures = self.tcp_listeners.iter().map(|listener| { + Box::pin(listener.accept()) + }); + let (result, _index, _remaining) = select_all(accept_futures).await; + result // result of the first future to complete } }