A lightweight HTTP server written in Rust that uses a custom-built thread pool to handle requests concurrently. Built for educational purposes while exploring systems-level concurrency, thread management, and graceful shutdown in Rust.
- ✅ Multi-threaded request handling with a custom
ThreadPool - ✅ Graceful shutdown of threads via a messaging system
- ✅ Minimal standard library dependencies
- ✅ Based on the Rust Book - Chapter 20
src/
├── main.rs # Entry point; starts the server
└── lib.rs # ThreadPool implementation
- A
ThreadPoolis created with a fixed number of worker threads. - Each thread waits on a channel for incoming jobs (
Message::NewJob). - Jobs are closures sent to the workers via the
Sender.
- Each
Workerowns a thread. - When a job arrives, a worker executes it.
- On shutdown, a
Message::Terminateis sent to each thread. - The
Dropimplementation forThreadPoolensures all threads are joined before exit.
enum Message {
NewJob(Job),
Terminate,
}This allows communication between the thread pool and its workers.
- Rust & Cargo (stable)
- Unix-based system or Windows with proper terminal setup
cargo build
cargo runServer listens on: 127.0.0.1:7878
By default, the main loop can use:
for stream in listener.incoming().take(2)to process only 2 requests for demonstration and then shut down cleanly.
When the server exits (or goes out of scope), it sends Message::Terminate to all workers. Each worker exits its loop and is joined by the main thread, ensuring no thread is left dangling.
You will see log output like:
Sending terminate message to all workers.
Shutting down all workers.
Shutting down worker 0
Worker 0 was told to terminate.
...
- Custom error enum (
PoolCreationError) for safer construction - No panics unless absolutely necessary (
unwrap()only where safe) - Uses
MutexandArcfor safe shared access to the job queue Option<JoinHandle>ensures we can.take()before.join()threads
Want to contribute? These are great places to start:
- Handle
Ctrl+C(SIGINT) for graceful shutdown usingctrlccrate - Add a
/shutdownHTTP route to trigger remote shutdown - Write unit and integration tests
- Replace
unwrap()calls with error handling - Improve documentation with Rustdoc
- Try
threadpoolorrayoncrate and compare performance
This project demonstrates:
- How to build a thread pool from scratch
- How to use channels and mutexes for thread-safe messaging
- How to gracefully shut down a multithreaded application in Rust
- A practical use of Rust’s ownership and concurrency model
MIT © You — Feel free to copy, modify, and distribute.