Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
prefork III
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Sep 22, 2011
1 parent 8afa067 commit 84e969e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
87 changes: 71 additions & 16 deletions prefork.c
Expand Up @@ -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);
}
Expand Down Expand Up @@ -91,51 +124,73 @@ 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);
}


// Pretend our server is very busy:
// 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);
}


Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/win/tcp.c
Expand Up @@ -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.
Expand Down

0 comments on commit 84e969e

Please sign in to comment.