Skip to content

Commit 58ce0fe

Browse files
EnqueueDuckEnqueueDuck
authored andcommitted
Allocate buffer for each worker before handling query
1 parent bc6f24b commit 58ce0fe

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

README.md

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

15-
### Version 2.0
15+
### Version 2.1.0
1616

1717
HTTP Server with N workers. Connections are distributed to the workers, each work handles IO
1818
using EPOLL.
@@ -22,12 +22,12 @@ user~/wrk (master) ./wrk -t8 -c 10000 -d20s http://host:14396
2222
Running 20s test @ http://host:14396
2323
8 threads and 10000 connections
2424
Thread Stats Avg Stdev Max +/- Stdev
25-
Latency 51.63us 129.97us 48.81ms 99.68%
26-
Req/Sec 103.74k 11.08k 134.55k 71.14%
27-
2073920 requests in 20.10s, 79.11MB read
25+
Latency 31.02us 37.16us 15.38ms 99.95%
26+
Req/Sec 74.05k 26.72k 103.45k 59.33%
27+
4420272 requests in 20.03s, 168.62MB read
2828
Socket errors: connect 8987, read 0, write 0, timeout 0
29-
Requests/sec: 103189.69
30-
Transfer/sec: 3.94MB
29+
Requests/sec: 220632.74
30+
Transfer/sec: 8.42MB
3131
```
3232

3333
### Version 1.1

WORKSPACE

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,13 @@ http_archive(
2727
patches = ["@//:third_party/bazel_patches/glog.patch"],
2828
patch_args = ["-p1"],
2929
)
30+
31+
http_archive(
32+
name = "third_party-event",
33+
sha256 = "92e6de1be9ec176428fd2367677e61ceffc2ee1cb119035037a27d346b0403bb",
34+
strip_prefix = "libevent-2.1.12-stable",
35+
urls = [
36+
"https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz",
37+
],
38+
build_file = "@//:third_party/bazel/BUILD.event",
39+
)

src/main/cpp/http/http_server.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@
55
namespace http {
66

77
void HttpServer::Worker::HandleConnection(int connection_sd) {
8-
char buffer[opts_.max_http_request_size];
9-
108
int bytes_read = 1;
119
while (bytes_read) {
12-
bytes_read = recv(connection_sd, buffer, sizeof(buffer), 0);
10+
bytes_read = recv(connection_sd, buffer_.get(), opts_.max_http_request_size, 0);
1311
if (bytes_read < 0) {
14-
LOG(ERROR) << "Error reading connection data";
12+
// Connection reset by peer is normal
13+
if (errno != ECONNRESET) {
14+
LOG(ERROR) << "Error reading connection data " << errno;
15+
}
1516
close(connection_sd);
1617
return;
1718
}
1819
if (bytes_read == 0) break;
1920

2021
// Parse the http request
2122
request::HttpRequest request;
22-
request.ParseFromString(buffer);
23+
request.ParseFromString(buffer_.get());
2324

2425
// Handle the request
2526
response::HttpResponse response = HandleRequest(request);
@@ -37,6 +38,7 @@ void HttpServer::Worker::Stop() {
3738
}
3839

3940
void HttpServer::Worker::Initialize() {
41+
buffer_ = std::make_unique<char[]>(opts_.max_http_request_size);
4042
epoll_fd_ = epoll_create1(0);
4143
thread_ = std::thread([this]() {
4244
struct epoll_event events[MAX_EPOLL_EVENTS];

src/main/cpp/http/http_server.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct HttpServerOpts {
1818

1919
class HttpServer : public Server {
2020
public:
21-
constexpr static int DEFAULT_HTTP_REQUEST_SIZE = 1024;
21+
constexpr static int DEFAULT_HTTP_REQUEST_SIZE = 8192;
2222

2323
explicit HttpServer(const HttpServerOpts &opts) : Server(opts.server_opts), opts_(opts) {
2424
for (int i = 0; i < opts_.num_workers; ++i) {
@@ -47,6 +47,7 @@ class HttpServer : public Server {
4747

4848
private:
4949
WorkerOpts opts_;
50+
std::unique_ptr<char[]> buffer_;
5051

5152
int epoll_fd_;
5253
std::thread thread_;

0 commit comments

Comments
 (0)