Skip to content

Instantly share code, notes, and snippets.

@narodnik
Created July 3, 2020 21:06
Show Gist options
  • Save narodnik/bf77c38302d4df2dcb81ae0e66fe9361 to your computer and use it in GitHub Desktop.
Save narodnik/bf77c38302d4df2dcb81ae0e66fe9361 to your computer and use it in GitHub Desktop.
use futures::io;
use smol::{Async, Task, Timer};
use std::net::{TcpListener, TcpStream};
use std::thread;
use std::time::Duration;
async fn echo(stream: Async<TcpStream>) -> io::Result<()> {
Timer::after(Duration::from_secs(5)).await;
println!("sleep over");
io::copy(&stream, &mut &stream).await?;
Ok(())
}
async fn start() -> std::io::Result<()> {
// Create a listener.
let listener = Async::<TcpListener>::bind("127.0.0.1:7000")?;
println!("Listening on {}", listener.get_ref().local_addr()?);
println!("Now start a TCP client.");
// Accept clients in a loop.
loop {
let (stream, peer_addr) = listener.accept().await?;
println!("Accepted client: {}", peer_addr);
// Spawn a task that echoes messages from the client back to it.
Task::spawn(echo(stream)).unwrap().detach();
}
}
fn main() {
// Same number of threads as there are CPU cores.
let num_threads = num_cpus::get();
// A channel that sends the shutdown signal.
let (s, r) = async_channel::bounded::<()>(1);
let mut threads = Vec::new();
// Create an executor thread pool.
for _ in 0..num_threads {
// Spawn an executor thread that waits for the shutdown signal.
let r = r.clone();
threads.push(thread::spawn(move || smol::run(r.recv())));
}
// No need to `run()`, now we can just block on the main future.
smol::block_on(async {
let _ = Task::spawn(start()).await;
});
// Send a shutdown signal.
drop(s);
// Wait for threads to finish.
for t in threads {
let _ = t.join().unwrap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment