Skip to content

Commit 608bcf4

Browse files
EnqueueDuckEnqueueDuck
authored andcommitted
Fix callback does not add back event to event_loop
1 parent e718d63 commit 608bcf4

File tree

9 files changed

+307
-321
lines changed

9 files changed

+307
-321
lines changed

README.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,42 @@ To run the server, default host is 14396:
1212
bazel run -c opt src/main/cpp:server
1313
```
1414

15-
### Version 3.0.0
15+
### Version 3.1.0
16+
17+
The base class for server is `ServerSocket` at `src/main/cpp/server_socket.h`. I use an
18+
`evconnlistener` from `libevent` to bind and listen on the server address. For a new connection
19+
accepted, `accept_conn_cb` is called. The server passes the connection to one of the workers
20+
(defined in `src/main/cpp/server`);
21+
22+
The server worker runs an event loop, connections received from the server will be added for
23+
monitoring. When the connection becomes active, `conn_read_callback` will be triggered. It reads
24+
from the socket, parses the HTTP request, call `HandleHttpRequest` to produce an `HttpResponse` and
25+
write the response to the connection socket.
26+
27+
Some notes:
28+
- Connections received from `evconnlistener` are non-blocking by default. When reading from
29+
non-blocking fd, if the buffer is not ready, error `EAGAIN` or `EWOULDBLOCK` would be returned. We
30+
should not close the connection there, but simply skip it, continue to proceed other connections.
31+
- When the event loop triggers `conn_read_callback`, the event associated with the connection
32+
becomes non-pending and will not trigger callbacks anymore, so in the callback function, we need
33+
to add the connection again after we're done processing it.
34+
- We run the event loop in one thread, when there's no event, and then adding connection to it from
35+
another thread, so we need to set flag `EVLOOP_NO_EXIT_ON_EMPTY`.
1636

1737
HTTP Server with N workers. Connections are monitored by libevent, active connections are passed
1838
to workers for handling.
1939

2040
```
21-
user~/wrk (master) ./wrk -t8 -c 10000 -d20s http://host:14396
41+
user~/wrk (master) ./wrk -t8 -c10000 -d20s http://host:14396
2242
Running 20s test @ http://host:14396
2343
8 threads and 10000 connections
2444
Thread Stats Avg Stdev Max +/- Stdev
25-
Latency 45.01us 33.27us 11.13ms 92.94%
26-
Req/Sec 115.95k 8.27k 140.82k 72.00%
27-
2306535 requests in 20.04s, 87.99MB read
28-
Socket errors: connect 8987, read 8, write 0, timeout 0
29-
Requests/sec: 115099.39
30-
Transfer/sec: 4.39MB
45+
Latency 3.39ms 16.09ms 1.68s 98.65%
46+
Req/Sec 37.57k 11.24k 107.14k 71.88%
47+
5897359 requests in 20.08s, 224.97MB read
48+
Socket errors: connect 8987, read 0, write 0, timeout 0
49+
Requests/sec: 293685.43
50+
Transfer/sec: 11.20MB
3151
```
3252

3353
### Version 2.2.0

src/main/cpp/http/http_server.cpp

Lines changed: 0 additions & 132 deletions
This file was deleted.

src/main/cpp/http/http_server.h

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)