Stop wasting CPU cycles on infinite loops
This commit is contained in:
parent
74a359061c
commit
34b91eb1e4
13
src/main.rs
13
src/main.rs
|
@ -13,8 +13,10 @@ async fn main() -> anyhow::Result<()> {
|
|||
|
||||
let ports: HashMap<SocketAddr, u16> = toml::from_str(&contents)?;
|
||||
|
||||
let mut handles = Vec::with_capacity(ports.len());
|
||||
|
||||
for (&saddr, &port) in &ports {
|
||||
tokio::spawn(async move {
|
||||
let handle = tokio::spawn(async move {
|
||||
let listener = TcpListener::bind(SocketAddr::new("0.0.0.0".parse()?, port)).await?;
|
||||
loop {
|
||||
let (stream, _) = listener.accept().await?;
|
||||
|
@ -30,10 +32,15 @@ async fn main() -> anyhow::Result<()> {
|
|||
#[allow(unreachable_code)]
|
||||
Ok::<(), anyhow::Error>(())
|
||||
});
|
||||
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
#[allow(clippy::empty_loop)]
|
||||
loop {}
|
||||
for handle in handles {
|
||||
handle.await??;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn proxy(left: TcpStream, right: TcpStream) -> anyhow::Result<()> {
|
||||
|
|
Loading…
Reference in a new issue