A multithreaded http web server example written in rust. Taught as a mini project in chapter 20 of the official rust book.
Basically, the point was to apply both the beginner and advanced concepts learned throughout the book in one go. Examples of the advanced concepts are:- smart pointers, atomic smart pointers(thread-safe implementation of smart pointers), fearless concurrency provided by rust etc.
-
Clone this repository:-
git clone https://github.com/xoldyckk/hello.git
-
Change directory of your terminal into the cloned project folder.
cd ./hello
-
Run the following command to start the http web server:-
cargo run
The server is started at the following address:- http://127.0.0.1:7878
-
Uses hardcoded value of
4
threads as the default number of threads available for serving concurrent requests. The number of threads can be changed when running locally. Here's how to do it:-Go to line
15
of the file main.rs and change the number4
to the desired value.let pool = ThreadPool::new(4);
-
Uses hardcoded value of
20
tcp streams as the amount of streams(http requests) to respond to before shutting down the server, this is done to illustrate the concept of graceful server shut down. The number of requests to handle can be changed when running locally. Here's how to do it:-Go to line
21
of the file main.rs and change the number20
to the desired value.for stream in listener.incoming().take(20) {
-
Uses hardcoded value of
10
seconds as the amount of time to delay the execution of thread handling the request to http://127.0.0.1:7878/sleep route before responding to the client. This is done to illustrate the concept of concurrency and multi-threading provided by rust for the http web server. The amount of time to delay the request to http://127.0.0.1:7878/sleep route can be changed when running locally. Here's how to do it:-Go to line
50
of the file main.rs and change the number10
to the desired value.thread::sleep(Duration::from_secs(10));
root route /
, returns the hello.html page stored in root directory of this project.
/sleep
route, also returns the hello.html page stored in root directory of this project. The difference is that this route takes at least 10 seconds to return the page.
/**/*
route, denotes any route which is not the /
or /sleep
route, returns the 404.html page stored in root directory of this project.
Open the routes http://127.0.0.1:7878 and http://127.0.0.1:7878/sleep in seperate browser tabs.
Make a request to http://127.0.0.1:7878/sleep route in one tab and within the span of 10
seconds make a request to http://127.0.0.1:7878 route in another tab.
Notice that the request in second tab is fullfilled immediately while the request in first tab in first tab is still processing.
This is because the request in first tab is being handled by a different thread than the request in second tab.
So, the thread handling request for the second tab responds to it immediately and we see the hello.html page instantaneously while the thread handling request for the first tab responds it after 10
seconds.
Throughout the codebase, there are detailed comments about why things are implemented the way they are. Also, this implementation is a teeny-tiny bit different from the implementation in the official rust book.