public
Description: New and ultra-turbo-crazy-fast backend for Thin
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin-turbo.git
Use pool to allocate read buffers
macournoyer (author)
Wed Mar 26 19:00:01 -0700 2008
commit  0a03eb0848d17fea7afe276670c5ecd94489fec6
tree    bd2ff88b38300db1bc00c774f5937cf2da4b2190
parent  ade03b50f8adf4d388a1471611246084d314e4f0
...
9
10
11
 
 
12
13
14
...
9
10
11
12
13
14
15
16
0
@@ -9,6 +9,8 @@ ext/*/vc60.pdb
0
 ext/*/*.so
0
 lib/*.bundle
0
 lib/*.so
0
+test/*.o
0
+test/*_test
0
 log
0
 spec/rails_app/log
0
 doc/rdoc/*
...
11
12
13
14
15
 
 
 
 
 
16
...
11
12
13
 
14
15
16
17
18
19
20
0
@@ -11,4 +11,8 @@ Dir['tasks/**/*.rake'].each { |rake| load rake }
0
 task :default => :spec
0
 
0
 ext_task :thin_parser
0
-ext_task :thin_backend
0
\ No newline at end of file
0
+ext_task :thin_backend
0
+
0
+task :test do
0
+ cd 'test' { sh 'make test' }
0
+end
0
\ No newline at end of file
...
39
40
41
42
43
 
 
44
45
46
47
48
 
 
49
50
51
52
 
 
 
53
54
55
56
57
 
 
58
 
59
60
61
...
67
68
69
70
71
 
72
73
 
 
74
75
 
76
77
78
79
 
 
80
81
82
83
84
 
85
86
87
...
160
161
162
 
163
164
165
...
191
192
193
 
 
194
195
196
197
...
39
40
41
 
 
42
43
44
45
46
 
 
47
48
49
 
 
 
50
51
52
53
 
 
 
 
54
55
56
57
58
59
60
...
66
67
68
 
 
69
70
 
71
72
73
 
74
75
76
77
 
78
79
80
81
82
83
 
84
85
86
87
...
160
161
162
163
164
165
166
...
192
193
194
195
196
197
198
199
200
0
@@ -39,23 +39,22 @@ int thin_print_headers(thin_connection_t *connection, char *str)
0
 void thin_connection_writable_cb(EV_P_ struct ev_io *watcher, int revents)
0
 {
0
   thin_connection_t *connection = get_ev_data(connection, watcher, write);
0
- char *msg;
0
- int n;
0
+ char *resp;
0
+ int n, sent;
0
   
0
   /* TODO write in chunk */
0
 
0
- /* TODO get rid of that malloc or do something less stupid! */
0
- msg = (char *) malloc(connection->body.len + 1024);
0
+ resp = (char *) palloc(connection->buffer_pool,
0
+ connection->body.len / connection->buffer_pool->size + 1);
0
   
0
- n = sprintf(msg, "HTTP/1.1 %d OK\r\n", connection->status);
0
- n += thin_print_headers(connection, (char *) msg + n);
0
- n += sprintf((char *) msg + n, "\r\n%s", connection->body.ptr);
0
+ n = sprintf(resp, "HTTP/1.1 %d OK\r\n", connection->status);
0
+ n += thin_print_headers(connection, (char *) resp + n);
0
+ n += sprintf((char *) resp + n, "\r\n%s", connection->body.ptr);
0
   
0
- if (send(connection->fd, msg, n, 0) < 0)
0
- rb_sys_fail("send");
0
-
0
- free(msg); /* see, doesn't make sense... */
0
+ sent = send(connection->fd, resp, n, 0);
0
+ /* TODO do something w/ sent, maybe buffer, not all sent? */
0
   
0
+ pfree(connection->buffer_pool, resp);
0
   watcher->cb = thin_connection_closable_cb;
0
 }
0
 
0
@@ -67,21 +66,22 @@ void thin_connection_readable_cb(EV_P_ struct ev_io *watcher, int revents)
0
   char *new, *old;
0
   
0
   /* alloc more mem when buffer full */
0
- if (connection->read_buffer.len == connection->read_buffer.nalloc) {
0
- /* TODO refactor this into a buffer.c file? */
0
+ if (connection->read_buffer.len >= connection->read_buffer.salloc) {
0
     old = connection->read_buffer.ptr;
0
- new = (char *) malloc(connection->read_buffer.nalloc * 2);
0
+ new = (char *) palloc(connection->buffer_pool,
0
+ connection->read_buffer.nalloc * 2);
0
     if (new == NULL)
0
- rb_sys_fail("malloc");
0
+ rb_sys_fail("palloc");
0
     
0
     connection->read_buffer.ptr = new;
0
     connection->read_buffer.nalloc *= 2;
0
- free(old);
0
+ connection->read_buffer.salloc *= 2;
0
+ pfree(connection->buffer_pool, old);
0
   }
0
 
0
   len = recv(connection->fd,
0
              connection->read_buffer.ptr + connection->read_buffer.len,
0
- THIN_BUFFER_SIZE - connection->read_buffer.len,
0
+ connection->read_buffer.salloc - connection->read_buffer.len,
0
              0);
0
   
0
   connection->read_buffer.len += len;
0
@@ -160,6 +160,7 @@ void thin_connection_start(thin_backend_t *backend, int fd, struct sockaddr_in r
0
   if (connection->read_buffer.ptr == NULL)
0
     rb_sys_fail("palloc");
0
   connection->read_buffer.nalloc = 1;
0
+ connection->read_buffer.salloc = connection->buffer_pool->size;
0
   connection->read_buffer.len = 0;
0
   
0
   /* reinit parser */
0
@@ -191,6 +192,8 @@ void thin_connection_close(thin_connection_t *connection)
0
   
0
   if (connection->read_buffer.ptr != NULL)
0
     pfree(connection->buffer_pool, connection->read_buffer.ptr);
0
+ connection->read_buffer.salloc = 0;
0
+ connection->read_buffer.nalloc = 0;
0
   
0
   connection->open = 0;
0
 }
0
\ No newline at end of file
...
38
39
40
 
 
41
42
43
44
45
...
38
39
40
41
42
43
 
44
45
46
0
@@ -38,8 +38,9 @@ typedef struct thin_buffer_s thin_buffer_t;
0
 typedef struct thin_connection_s thin_connection_t;
0
 
0
 struct thin_buffer_s {
0
+ size_t nalloc; /* num slices alloced */
0
+ size_t salloc; /* total size alloced */
0
   size_t len;
0
- size_t nalloc;
0
   char *ptr;
0
 };
0
 

Comments

    No one has commented yet.