Concurrent Servers
Building a concurrent server in Rust that won't fall over under load means combining async I/O with shared state. The type system handles the thread-safety policing for you.
Shared State with Arc and Mutex
use std::sync::Arc;use tokio::io::AsyncReadExt;use tokio::sync::Mutex;
#[derive(Default)]struct ServerState { connections: u64, messages: Vec<String>,}
#[tokio::main]async fn main() { let state = Arc::new(Mutex::new(ServerState::default())); let listener = tokio::net::TcpListener::bind("127.0.0.1:8080") .await .unwrap();
loop { let (socket, _) = listener.accept().await.unwrap(); let state = Arc::clone(&state);
tokio::spawn(async move { let mut guard = state.lock().await; guard.connections += 1; drop(guard); // release lock before I/O
handle_client(socket, state).await; }); }}
async fn handle_client(mut socket: tokio::net::TcpStream, state: Arc<Mutex<ServerState>>) { let mut buf = [0u8; 1024]; while let Ok(n) = socket.read(&mut buf).await { if n == 0 { break; } // client closed the connection let msg = String::from_utf8_lossy(&buf[..n]).into_owned(); state.lock().await.messages.push(msg); // lock held just for the push }}handle_client takes its own clone of the Arc, so it can lock the state whenever it needs to. Note the drop(guard) above: holding a lock across an .await on the socket would make every other task wait on one slow client.
Arc: Atomic Reference Counting
Arc<T> is Rc<T> with atomic refcount bumps. Multiple tasks can hold the same data without anyone owning it outright:
let data = Arc::new(vec![1, 2, 3]);let data_clone = Arc::clone(&data);
// Inside an async fn running on Tokiotokio::spawn(async move { println!("{:?}", data_clone); // reads shared data});Arc hands out shared ownership across threads; Mutex gates access to whatever lives inside. Stack them as Arc<Mutex<T>> and you have the canonical recipe for shared mutable state in concurrent Rust. The compiler refuses to let you touch the data without the lock in hand.
Tokio Channels
For message-passing concurrency (often preferred over shared state):
use tokio::sync::mpsc;
#[tokio::main]async fn main() { let (tx, mut rx) = mpsc::channel::<String>(100);
// Spawn producer tasks for i in 0..5 { let tx = tx.clone(); tokio::spawn(async move { tx.send(format!("Message from task {}", i)).await.unwrap(); }); } drop(tx); // drop original sender
// Receive all messages while let Some(msg) = rx.recv().await { println!("{}", msg); }}Broadcast Channel
For one-to-many messaging (like a chat server):
use tokio::sync::broadcast;
#[tokio::main]async fn main() { let (tx, _) = broadcast::channel::<String>(100);
// Each client gets its own receiver let mut rx1 = tx.subscribe(); let mut rx2 = tx.subscribe();
tx.send("Hello everyone!".to_string()).unwrap();
// Both receivers get the message println!("{}", rx1.recv().await.unwrap()); println!("{}", rx2.recv().await.unwrap());}Tokio and sockets aren't available in the runner, so try this one in your head.
Predict: what happens if you call tx.send before anyone has called subscribe()?
The _ in let (tx, _) drops the original receiver straight away, so there's nobody to deliver to: send returns Err and the .unwrap() panics. Subscribing later doesn't help either, because a receiver only sees messages sent after it subscribed. A chat client that joins late never gets the backlog.
Graceful Shutdown
use tokio::signal;
// accept_loop is the accept-and-spawn loop from the first exampleasync fn accept_loop(listener: tokio::net::TcpListener) { while let Ok((socket, _addr)) = listener.accept().await { tokio::spawn(async move { // handle the client here drop(socket); }); }}
#[tokio::main]async fn main() { let listener = tokio::net::TcpListener::bind("127.0.0.1:8080") .await .unwrap();
tokio::select! { _ = accept_loop(listener) => {}, _ = signal::ctrl_c() => { println!("Shutting down..."); }, }}tokio::select! races several futures and returns whichever finishes first. Handy for letting a shutdown signal interrupt the accept loop without polling some flag every iteration.