Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

prefork III

  • Loading branch information...
commit 84e969e0088290f1927cdc84f0504d3e4f75e814 1 parent 8afa067
@piscisaureus piscisaureus authored
Showing with 72 additions and 17 deletions.
  1. +71 −16 prefork.c
  2. +1 −1  src/win/tcp.c
View
87 prefork.c
@@ -23,6 +23,39 @@ char message[] = "HTTP 1.1 200 OK\r\nContent-Length: 12\r\nConnection: close\r\n
printf("Server %d: " ## msg, server_id, __VA_ARGS__); \
+typedef struct client_s {
+ uv_tcp_t handle;
+ uv_write_t write;
+ char read_buffer[8192];
+ struct client_s* next_free;
+} client_t;
+
+static client_t* free_client_list = NULL;
+
+static client_t* alloc_client() {
+ if (free_client_list == NULL) {
+ return (client_t*) malloc(sizeof(client_t));
+ } else {
+ client_t* client = free_client_list;
+ free_client_list = client->next_free;
+ return client;
+ }
+}
+
+static void free_client(client_t* client) {
+ client->next_free = free_client_list;
+ free_client_list = client;
+}
+
+static client_t* get_client_from_handle(uv_tcp_t* handle) {
+ return CONTAINING_RECORD(handle, client_t, handle);
+}
+
+static client_t* get_client_from_write(uv_write_t* write) {
+ return CONTAINING_RECORD(write, client_t, write);
+}
+
+
void slave_close_cb(uv_handle_t* handle) {
free(handle);
}
@@ -91,29 +124,25 @@ void spawn(int id, SOCKET sock) {
}
-void cl_close_cb(uv_handle_t* handle) {
- free(handle);
+static void cl_close_cb(uv_handle_t* handle) {
+ free_client(get_client_from_handle((uv_tcp_t*) handle));
}
-void cl_write_cb(uv_write_t* req, int status) {
- //CHECK(status == 0);
-
+static void cl_write_cb(uv_write_t* req, int status) {
uv_close((uv_handle_t*) req->handle, cl_close_cb);
-
- free(req);
}
-void cl_write(uv_tcp_t* handle) {
+static void cl_write(uv_tcp_t* handle) {
int r;
+ client_t* client = get_client_from_handle(handle);
uv_buf_t buf = uv_buf_init(message, (sizeof message) - 1);
- uv_write_t* req = malloc(sizeof *req);
+ uv_write_t* req = &client->write;
r = uv_write(req, (uv_stream_t*) handle, &buf, 1, cl_write_cb);
if (r) {
LOG("error");
uv_close((uv_handle_t*) handle, cl_close_cb);
- free(req);
}
@@ -121,21 +150,47 @@ void cl_write(uv_tcp_t* handle) {
// Sleep(10);
}
+volatile int fubar;
+
+
+static void cl_read_cb(uv_stream_t* handle, ssize_t nread, uv_buf_t buf) {
+ uv_tcp_t* tcp = (uv_tcp_t*) handle;
+ int r;
+
+ r = uv_read_stop(handle);
+ CHECK(r == 0);
+
+ // Do some work
+ for (fubar = 50000; fubar > 0; fubar--) {}
+
+ cl_write(tcp);
+}
+
+uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) {
+ client_t* client = get_client_from_handle((uv_tcp_t*) handle);
+ uv_buf_t buf;
+ buf.base = client->read_buffer;
+ buf.len = sizeof client->read_buffer;
+ return buf;
+}
+
+
void connection_cb(uv_stream_t* server, int status) {
int r;
- uv_tcp_t* client = (uv_tcp_t*) malloc(sizeof *client);
+ client_t* client = alloc_client();
CHECK(status == 0);
- r = uv_tcp_init(client);
+ r = uv_tcp_init(&client->handle);
CHECK(r == 0);
- r = uv_accept(server, (uv_stream_t*) client);
+ r = uv_accept(server, (uv_stream_t*) &client->handle);
CHECK(r == 0);
accepted++;
- cl_write(client);
+ r = uv_read_start((uv_stream_t*) &client->handle, alloc_cb, cl_read_cb);
+ CHECK(r == 0);
}
@@ -145,7 +200,7 @@ void timer_cb(uv_timer_t* timer, int status) {
void master() {
- int i, r;
+ int r;
r = uv_tcp_init(&server);
CHECK(r == 0);
@@ -199,7 +254,7 @@ int main(int argv, char** argc) {
// Start listening now
LOG("listen\n");
- r = uv_listen((uv_stream_t*) &server, 10, connection_cb);
+ r = uv_listen((uv_stream_t*) &server, 512, connection_cb);
CHECK(r == 0);
// Spawn slaves
View
2  src/win/tcp.c
@@ -32,7 +32,7 @@
* the optimization is temporarily disabled (threshold=0). This will be
* revisited once node allocator is improved.)
*/
-const unsigned int uv_active_tcp_streams_threshold = 0;
+const unsigned int uv_active_tcp_streams_threshold = 1000;
/*
* Number of simultaneous pending AcceptEx calls.
Please sign in to comment.
Something went wrong with that request. Please try again.