ry / ebb fork watch download tarball
public this repo is viewable by everyone
Description: web server
Homepage: http://ebb.rubyforge.org
Clone URL: git://github.com/ry/ebb.git
Another attempt at moving ev_loop to a seperate thread

It turns out this is signifigantly slower than the original and I will 
leave
these changes out of the master. See
http://four.livejournal.com/847870.html
for some details and benchmarks
Ryan Dahl (author)
2 months ago
commit  5068077d187211464654c5b95ff4fa63c91b2c48
tree    8810eb01570d41f6292525c887df38fbe2ca7621
parent  c023fa46c39157b09728f57fa5014d71bfcb935f
...
74
75
76
77
78
79
80
81
82
83
84
...
74
75
76
 
 
 
 
 
77
78
79
0
@@ -74,10 +74,5 @@ end
0
 
0
 if $0 == __FILE__
0
   require DIR + '/../ruby_lib/ebb'
0
- require 'rubygems'
0
- require 'ruby-debug'
0
- Debugger.start
0
-
0
- puts "Ebb started on http://0.0.0.0:4001/"
0
   server = Ebb::start_server(SimpleApp.new, :port => 4001)
0
 end
0
\ No newline at end of file
...
80
81
82
83
 
84
85
86
...
149
150
151
152
153
 
...
80
81
82
 
83
84
85
86
...
149
150
151
 
152
153
0
@@ -80,7 +80,7 @@ class ServerTest
0
   end
0
   
0
   def kill
0
- Process.kill('KILL', @pid)
0
+ Process.kill('KILL', @pid) if @pid
0
   end
0
   
0
   def running?
0
@@ -149,4 +149,4 @@ class ServerTest
0
       :ab_cmd => cmd
0
     }
0
   end
0
-end
0
\ No newline at end of file
0
+end
...
1
2
3
 
4
5
 
6
7
8
9
10
11
12
13
14
15
 
16
17
18
...
20
21
22
23
24
 
 
25
26
27
28
29
30
31
32
33
 
 
 
 
34
35
36
 
37
38
39
...
70
71
72
73
74
75
76
 
 
77
78
79
...
1
2
3
4
5
6
7
8
9
 
 
 
 
 
 
 
 
10
11
12
13
...
15
16
17
 
 
18
19
20
 
 
 
 
 
 
 
 
21
22
23
24
25
26
27
28
29
30
31
...
62
63
64
 
 
 
 
65
66
67
68
69
0
@@ -1,18 +1,13 @@
0
 # A ruby binding to the ebb web server
0
 # Copyright (c) 2007 Ry Dahl <ry.d4hl@gmail.com>
0
 # This software is released under the "MIT License". See README file for details.
0
+
0
 module Ebb
0
   LIBDIR = File.dirname(__FILE__)
0
+ require LIBDIR + '/../src/ebb_ext'
0
   VERSION = File.read(LIBDIR + "/../VERSION").gsub(/\s/,'')
0
   autoload :Runner, LIBDIR + '/ebb/runner'
0
-end
0
-
0
-require Ebb::LIBDIR + '/../src/ebb_ext'
0
-
0
-module Ebb
0
- # "Gasp! No Server class! But this is Object Oriented Programming - we
0
- # classes for servers!", you say. Not when there always will be
0
- # exactly one server per virtual machine.
0
+
0
   def self.start_server(app, options={})
0
     port = (options[:port] || 4001).to_i
0
     #socket = options[:socket]
0
@@ -20,20 +15,17 @@ module Ebb
0
     
0
     trap('INT') { @running = false }
0
     
0
- FFI::server_listen_on_port(port)
0
-
0
+ notify_fd = FFI::server_listen_on_port(port)
0
+ notifier = IO.new(notify_fd)
0
     puts "Ebb listening at http://0.0.0.0:#{port}/"
0
-
0
- @running = true
0
- while FFI::server_process_connections() and @running
0
- unless FFI::waiting_clients.empty?
0
- if $DEBUG and $ebb_waiting_clients.length > 1
0
- puts "#{FFI::waiting_clients.length} waiting clients"
0
- end
0
- client = FFI::waiting_clients.shift
0
+ while notifier.read(1)
0
+ if client = FFI::server_next_client()
0
+ # puts "#{FFI::waiting_clients.length} waiting clients"
0
+ # p client
0
         process_client(app, client)
0
       end
0
     end
0
+
0
     puts "Ebb unlistening"
0
     FFI::server_unlisten()
0
   end
0
@@ -70,10 +62,8 @@ module Ebb
0
     client.finished
0
   end
0
   
0
- module FFI
0
- def self.waiting_clients
0
- @waiting_clients
0
- end
0
+ def FFI.waiting_clients
0
+ @waiting_clients
0
   end
0
   
0
   class Client
...
124
125
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
128
129
...
143
144
145
 
 
 
 
 
 
146
147
148
...
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
...
600
601
602
603
 
604
605
606
...
611
612
613
614
615
616
617
618
619
 
620
621
622
...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
...
184
185
186
187
188
189
190
191
192
193
194
195
...
580
581
582
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
583
584
585
...
611
612
613
 
614
615
616
617
...
622
623
624
 
625
 
 
 
 
626
627
628
629
0
@@ -124,6 +124,47 @@ void content_length_cb(void *data, const char *at, size_t length)
0
 }
0
 
0
 
0
+static void
0
+on_client_writable(struct ev_loop *loop, ev_io *watcher, int revents)
0
+{
0
+ ebb_client *client = (ebb_client*)(watcher->data);
0
+ ssize_t sent;
0
+
0
+ if(client->headers_sent == FALSE)
0
+ return;
0
+
0
+ if(EV_ERROR & revents) {
0
+ g_message("on_client_writable() got error event, closing peer");
0
+ return;
0
+ }
0
+
0
+ //if(client->written != 0)
0
+ // g_debug("total written: %d", (int)(client->written));
0
+
0
+ sent = send( client->fd
0
+ , client->response_buffer->str + sizeof(gchar)*(client->written)
0
+ , client->response_buffer->len - client->written
0
+ , 0
0
+ );
0
+ if(sent < 0) {
0
+#ifdef DEBUG
0
+ g_message("Error writing: %s", strerror(errno));
0
+#endif
0
+ ebb_client_close(client);
0
+ return;
0
+ }
0
+ client->written += sent;
0
+
0
+ assert(client->written <= client->response_buffer->len);
0
+ //g_message("wrote %d bytes. total: %d", (int)sent, (int)(client->written));
0
+
0
+ ev_timer_again(loop, &(client->timeout_watcher));
0
+
0
+ if(client->written == client->response_buffer->len)
0
+ ebb_client_close(client);
0
+}
0
+
0
+
0
 const char* localhost_str = "0.0.0.0";
0
 void dispatch(ebb_client *client)
0
 {
0
@@ -143,6 +184,12 @@ void dispatch(ebb_client *client)
0
                         , strlen(server->port)
0
                         );
0
   }
0
+
0
+ client->write_watcher.data = client;
0
+ ev_init (&client->write_watcher, on_client_writable);
0
+ ev_io_set (&client->write_watcher, client->fd, EV_WRITE | EV_ERROR);
0
+ ev_io_start(server->loop, &client->write_watcher);
0
+
0
   server->request_cb(client, server->request_cb_data);
0
 }
0
 
0
@@ -533,42 +580,6 @@ void ebb_client_close(ebb_client *client)
0
 }
0
 
0
 
0
-void on_client_writable(struct ev_loop *loop, ev_io *watcher, int revents)
0
-{
0
- ebb_client *client = (ebb_client*)(watcher->data);
0
- ssize_t sent;
0
-
0
- if(EV_ERROR & revents) {
0
- g_message("on_client_writable() got error event, closing peer");
0
- return;
0
- }
0
-
0
- //if(client->written != 0)
0
- // g_debug("total written: %d", (int)(client->written));
0
-
0
- sent = send( client->fd
0
- , client->response_buffer->str + sizeof(gchar)*(client->written)
0
- , client->response_buffer->len - client->written
0
- , 0
0
- );
0
- if(sent < 0) {
0
-#ifdef DEBUG
0
- g_message("Error writing: %s", strerror(errno));
0
-#endif
0
- ebb_client_close(client);
0
- return;
0
- }
0
- client->written += sent;
0
-
0
- assert(client->written <= client->response_buffer->len);
0
- //g_message("wrote %d bytes. total: %d", (int)sent, (int)(client->written));
0
-
0
- ev_timer_again(loop, &(client->timeout_watcher));
0
-
0
- if(client->written == client->response_buffer->len)
0
- ebb_client_close(client);
0
-}
0
-
0
 void ebb_client_write_status(ebb_client *client, int status, const char *human_status)
0
 {
0
   assert(client->status_sent == FALSE);
0
@@ -600,7 +611,7 @@ void ebb_client_write(ebb_client *client, const char *data, int length)
0
 void ebb_client_finished(ebb_client *client)
0
 {
0
   assert(client->open);
0
- assert(FALSE == ev_is_active(&(client->write_watcher)));
0
+ assert(ev_is_active(&client->write_watcher));
0
   
0
   /* assure the socket is still in non-blocking mode
0
    * in the ruby binding, for example, i change this flag
0
@@ -611,12 +622,8 @@ void ebb_client_finished(ebb_client *client)
0
     ebb_client_close(client);
0
     return;
0
   }
0
-
0
   client->written = 0;
0
- client->write_watcher.data = client;
0
- ev_init (&(client->write_watcher), on_client_writable);
0
- ev_io_set (&(client->write_watcher), client->fd, EV_WRITE | EV_ERROR);
0
- ev_io_start(client->server->loop, &(client->write_watcher));
0
+ client->headers_sent = TRUE;
0
 }
0
 
0
 
...
7
8
9
 
 
10
11
12
...
29
30
31
 
 
 
 
32
33
34
...
38
39
40
41
 
42
43
44
45
46
47
48
49
50
51
52
 
 
 
 
 
 
53
54
55
56
57
58
 
 
 
 
 
 
59
60
61
62
63
64
65
66
67
68
 
 
 
 
 
 
69
70
71
 
 
72
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
75
76
...
215
216
217
 
 
218
219
220
221
222
...
231
232
233
234
235
236
 
 
 
 
237
...
7
8
9
10
11
12
13
14
...
31
32
33
34
35
36
37
38
39
40
...
44
45
46
 
47
48
 
 
 
 
 
 
 
 
 
 
49
50
51
52
53
54
55
56
57
58
59
 
60
61
62
63
64
65
66
 
 
 
 
 
 
 
 
 
67
68
69
70
71
72
73
 
 
74
75
76
 
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
...
242
243
244
245
246
247
 
248
249
250
...
259
260
261
 
 
 
262
263
264
265
266
0
@@ -7,6 +7,8 @@
0
 #include <fcntl.h>
0
 #include <ebb.h>
0
 #include <ev.h>
0
+#include <pthread.h>
0
+#include <glib.h>
0
 
0
 static VALUE cClient;
0
 static VALUE global_http_prefix;
0
@@ -29,6 +31,10 @@ static VALUE global_http_host;
0
  */
0
 static ebb_server *server;
0
 struct ev_loop *loop;
0
+static notify_fd;
0
+static pthread_mutex_t waiting_clients_lock = PTHREAD_MUTEX_INITIALIZER;
0
+static GQueue *waiting_clients;
0
+
0
 
0
 /* Variables with a leading underscore are C-level variables */
0
 
0
@@ -38,39 +44,60 @@ struct ev_loop *loop;
0
 # define RSTRING_LEN(s) (RSTRING(s)->len)
0
 #endif
0
 
0
-void request_cb(ebb_client *client, void *data)
0
+void request_cb(ebb_client *client, void *_)
0
 {
0
- VALUE waiting_clients = (VALUE)data;
0
- VALUE rb_client = Data_Wrap_Struct(cClient, 0, 0, client);
0
- rb_ary_push(waiting_clients, rb_client);
0
-}
0
-
0
-VALUE server_listen_on_port(VALUE _, VALUE port)
0
-{
0
- if(ebb_server_listen_on_port(server, FIX2INT(port)) < 0)
0
- rb_sys_fail("Problem listening on port");
0
- return Qnil;
0
+ pthread_mutex_lock(&waiting_clients_lock);
0
+ g_queue_push_tail(waiting_clients, (void*)client);
0
+ pthread_mutex_unlock(&waiting_clients_lock);
0
+
0
+ assert(notify_fd > 2);
0
+ assert(1 == write(notify_fd, "N", 1));
0
 }
0
 
0
 static void
0
 oneshot_timeout (struct ev_loop *loop, struct ev_timer *w, int revents) {;}
0
 
0
-VALUE server_process_connections(VALUE _)
0
+static void* process_connections(void *_)
0
+{
0
+ ev_loop(loop, 0);
0
+}
0
+
0
+VALUE server_next_client(VALUE _)
0
 {
0
- ev_timer timeout;
0
- ev_timer_init (&timeout, oneshot_timeout, 0.01, 0.);
0
- ev_timer_start (loop, &timeout);
0
-
0
- ev_loop(loop, EVLOOP_ONESHOT);
0
- /* XXX: Need way to know when the loop is finished...
0
- * should return true or false */
0
-
0
- ev_timer_stop(loop, &timeout);
0
+ ebb_client *client;
0
+ VALUE rb_client;
0
+
0
+ pthread_mutex_lock(&waiting_clients_lock);
0
+ client = g_queue_pop_head(waiting_clients);
0
+ pthread_mutex_unlock(&waiting_clients_lock);
0
   
0
- if(server->open)
0
- return Qtrue;
0
+ if(client == NULL)
0
+ rb_client = Qnil;
0
   else
0
- return Qfalse;
0
+ rb_client = Data_Wrap_Struct(cClient, 0, 0, client);
0
+ return rb_client;
0
+}
0
+
0
+VALUE server_listen_on_port(VALUE _, VALUE port)
0
+{
0
+ if(ebb_server_listen_on_port(server, FIX2INT(port)) < 0)
0
+ rb_sys_fail("Problem listening on port");
0
+ pthread_t thread;
0
+ int fildes[2];
0
+ assert(0 <= pipe(fildes));
0
+
0
+ /* the read side is blocking */
0
+ int flags = fcntl(fildes[0], F_GETFL, 0);
0
+ assert(0 <= fcntl(fildes[0], F_SETFL, flags & ~O_NONBLOCK));
0
+ /* the write side is nonblocking */
0
+ flags = fcntl(fildes[1], F_GETFL, 0);
0
+ assert(0 <= fcntl(fildes[1], F_SETFL, flags | O_NONBLOCK));
0
+ notify_fd = fildes[1];
0
+
0
+ assert(0 <= pthread_create(&thread, NULL, process_connections, NULL));
0
+ pthread_detach(thread);
0
+
0
+ return INT2FIX(fildes[0]);
0
 }
0
 
0
 
0
@@ -215,8 +242,9 @@ void Init_ebb_ext()
0
   DEF_GLOBAL(path_info, "PATH_INFO");
0
   DEF_GLOBAL(content_length, "CONTENT_LENGTH");
0
   DEF_GLOBAL(http_host, "HTTP_HOST");
0
+
0
+ rb_define_singleton_method(mFFI, "server_next_client", server_next_client, 0);
0
   
0
- rb_define_singleton_method(mFFI, "server_process_connections", server_process_connections, 0);
0
   rb_define_singleton_method(mFFI, "server_listen_on_port", server_listen_on_port, 1);
0
   rb_define_singleton_method(mFFI, "server_unlisten", server_unlisten, 0);
0
   
0
@@ -231,7 +259,8 @@ void Init_ebb_ext()
0
   /* initialize ebb_server */
0
   loop = ev_default_loop (0);
0
   server = ebb_server_alloc();
0
- VALUE waiting_clients = rb_ary_new();
0
- rb_iv_set(mFFI, "@waiting_clients", waiting_clients);
0
- ebb_server_init(server, loop, request_cb, (void*)waiting_clients);
0
+
0
+ waiting_clients = g_queue_new();
0
+
0
+ ebb_server_init(server, loop, request_cb, NULL);
0
 }
...
34
35
36
37
 
38
39
40
...
34
35
36
 
37
38
39
40
0
@@ -34,7 +34,7 @@ end
0
 dir = File.dirname(__FILE__)
0
 libev_dir = File.expand_path(dir + '/../libev')
0
 
0
-$LDFLAGS << " -lpthread "
0
+$LDFLAGS << " -lpthread -lprofiler "
0
 $CFLAGS << " -I#{libev_dir} " << flags.join(' ')
0
 $defs << "-DRUBY_VERSION_CODE=#{RUBY_VERSION.gsub(/\D/, '')}"
0
 
...
10
11
12
13
 
14
15
16
...
10
11
12
 
13
14
15
16
0
@@ -10,7 +10,7 @@ PORT = 4044
0
 class EbbTest < Test::Unit::TestCase
0
   def setup
0
     @pid = fork do
0
- STDOUT.reopen "/dev/null", "a"
0
+ #STDOUT.reopen "/dev/null", "a"
0
       server = Ebb::start_server(self, :port => PORT)
0
     end
0
     sleep 0.5

Comments

    No one has commented yet.