$ chmod +x first_time_run.sh
$ ./first_time_run.sh
# Run
$ ./run.sh
- You may need to increase the Open Files Limitation to a larger number (i.e.
20000) in local environment to handle more connections.
$ sudo apt-get install build-essential libssl-dev git -y
$ git clone https://github.com/wg/wrk.git wrk
$ cd wrk
$ sudo make
# move the executable to somewhere in your PATH, ex:
$ sudo cp wrk /usr/local/bin
- Multiple clients to connect to the server at the same time.
- Supports HTTP/1.1.
- Get the response by request type and endpoint.
- "GET /" : return
index.htmlpage. - "GET /{some text file}" : return any pre-loaded files to the client.
- "GET /" : return
- Get files (image, text, ...)
- "GET /{fileName}": return that file to the client.
-
Maintains a file descriptor for listening socket called
socketMaster. ThesocketMasteris responsible for listening portPORT(PORT = 2101by default). -
To monitor events on the file descriptors, I use
epollthat is a data structure (Red-Black Tree) to monitor events on file descriptors. -
First, creates an
epollContextwithepoll_create()and registerssocketMasterto theepollContext. -
epoll_wait()is used to get the file descriptors that are ready for I/O. -
When there is any I/O event on the
socketMaster, that means there is a connection is coming. -
Then
accept()to open another socket to deal with that connection and register it to theepollContext. -
If an event is seen on the file descriptor other than
socketMaster, base on the type of event, it will handle it.- When an
EPOLLINevent occurs, then there is a request, I will handle the request and send some response to the client via that corresponding socket. - When an
EPOLLERRorEPOLLHUPevent occurs, I will close that connection.
- When an
-
When the server is starting up, I load some static
HTMLfiles (JStoo) to respond quickly. -
The server supports transferring files. If you open
/about.htmlpage, you can see the browser is loadingabout.css,about.jsand images. -
I used a Thread Pool to handle "heavy" request (may be transferring a file, search in database, ...).
First thepoolis created withXthread(s) asXworkers, which are infinitely looking into a mutex-locked queue of tasks.
Whenever aworkertakes a task from the queue, thatworkerexecutes it. -
This
event-drivenarchitecture allows the server to handle many connections and requests efficiently.
$ ./wrk -t5 -c10000 -d30s http://127.0.0.1:2101
# Testing with 10000 connections kept openned within 30s by 5 threads