ry / ebb fork watch download tarball
public
Description: web server
Homepage: http://ebb.rubyforge.org
Clone URL: git://github.com/ry/ebb.git
Search Repo:
Misc clean up

* Renamed ebb_server_start/stop to ebb_server_listen/deafen since those
  functions don't actually have control over the event loop.

* Cleaned up Client#env to make it hopefully more Rack compatible. This
  involved removing some hash items from C and putting them in Ruby.
Ryan Dahl (author)
Sat Feb 23 03:54:48 -0800 2008
commit  cc7e88c3cdb76168e3717a51b74b872871e26695
tree    cfbeaec25a02fd0d134da58416a8af26876317f7
parent  0a7f5a523e92d1b1462b89353b2a64331abbb4d0
...
171
172
173
174
 
 
 
 
 
175
176
 
177
178
 
179
180
181
182
...
188
189
190
191
192
193
194
195
196
197
198
 
199
200
201
...
229
230
231
232
 
233
234
235
...
250
251
252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
...
171
172
173
 
174
175
176
177
178
179
 
180
181
 
182
183
184
185
186
...
192
193
194
 
195
196
197
198
199
200
 
201
202
203
204
...
232
233
234
 
235
236
237
238
...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
0
@@ -171,11 +171,15 @@
0
     }
0
   end
0
   
0
- def wait_trial(wait, concurrency = 50)
0
+
0
+ def self.wait_scale
0
+ [1,20,40,60,80,100]
0
+ end
0
+ def wait_trial(concurrency)
0
     
0
- print "#{@name} (c=#{concurrency},wait=#{wait}) "
0
+ print "#{@name} (c=#{concurrency}}) "
0
     $stdout.flush
0
- r = %x{ab -t #{wait*3} -q -c #{concurrency} http://0.0.0.0:#{@port}/periodical_activity/fibonacci/#{wait}}
0
+ r = %x{ab -t #{3} -q -c #{concurrency} http://0.0.0.0:#{@port}/periodical_activity/fibonacci/20}
0
     # Complete requests: 1000
0
 
0
     return nil unless r =~ /Requests per second:\s*(\d+\.\d\d)/
0
0
@@ -188,14 +192,13 @@
0
       :test => 'get',
0
       :server=> @name,
0
       :concurrency => concurrency,
0
- :wait => wait,
0
       :rps => rps,
0
       :requests_completed => completed_requests,
0
       :time => Time.now
0
     }
0
   end
0
   
0
-
0
+
0
   def post_trial(size = 1, concurrency = 10)
0
     
0
     print "#{@name} (c=#{concurrency},posting=#{size}) "
0
@@ -229,7 +232,7 @@
0
 
0
 $servers = []
0
 app = SimpleApp.new
0
-$servers << ServerTest.new('evented mongrel', 4001) do
0
+$servers << ServerTest.new('emongrel', 4001) do
0
   require 'mongrel'
0
   require 'swiftcore/evented_mongrel'
0
   ENV['EVENT'] = "1"
0
@@ -250,5 +253,34 @@
0
 $servers << ServerTest.new('thin', 4004) do
0
   require 'thin'
0
   Rack::Handler::Thin.run(app, :Port => 4004)
0
+end
0
+
0
+
0
+benchmark_type = ARGV.shift
0
+servers_to_use = ARGV
0
+
0
+trap('INT') { exit(1) }
0
+dumpfile = "#{benchmark_type}.dump"
0
+begin
0
+ results = ServerTestResults.open(dumpfile)
0
+ $servers.each { |s| s.start }
0
+ sleep 3
0
+ ServerTest.send("#{benchmark_type}_scale").each do |s|
0
+ $servers.rand_each do |server|
0
+ if r = server.send("#{benchmark_type}_trial", s)
0
+ results << r
0
+ else
0
+ puts "error! restarting server"
0
+ server.kill
0
+ server.start
0
+ end
0
+ sleep 0.2 # give the other process some time to cool down?
0
+ end
0
+ puts "---"
0
+ end
0
+ensure
0
+ puts "\n\nkilling servers"
0
+ $servers.each { |server| server.kill }
0
+ results.write(dumpfile)
0
 end
...
18
19
20
21
22
 
 
23
24
25
...
18
19
20
 
 
21
22
23
24
25
0
@@ -18,8 +18,8 @@
0
   opts.separator ""
0
   opts.separator "Server options:"
0
 
0
- opts.on("-o", "--host HOST", "listen on HOST (default: 0.0.0.0)") { |host| options[:Hort] = host }
0
- opts.on("-p", "--port PORT", "use PORT (default: 3000)") { |port| options[:Port] = port }
0
+ opts.on("-h", "--host HOST", "listen on HOST (default: 0.0.0.0)") { |host| options[:hort] = host }
0
+ opts.on("-p", "--port PORT", "use PORT (default: 3000)") { |port| options[:port] = port }
0
   opts.on("-e", "--env ENV", "Rails environment (default: development)") { |env| options[:env] = env }
0
   opts.on("-c", "--chdir PATH", "Rails root dir (default: current dir)") { |dir| options[:root] = dir }
0
   opts.on("-d", "--daemonize", "Daemonize") { options[:daemonize] = true }
...
1
2
3
4
5
6
7
8
9
10
...
14
15
16
17
 
 
 
 
 
 
 
 
 
 
18
19
20
21
22
 
 
 
 
 
23
24
25
...
70
71
72
73
74
75
76
77
78
...
99
100
101
102
103
 
104
105
106
...
111
112
113
114
 
115
116
117
...
1
2
3
 
 
 
4
5
6
7
...
11
12
13
 
14
15
16
17
18
19
20
21
22
23
24
25
 
 
 
26
27
28
29
30
31
32
33
...
78
79
80
 
 
 
81
82
83
...
104
105
106
 
 
107
108
109
110
...
115
116
117
 
118
119
120
121
0
@@ -1,9 +1,6 @@
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
-require 'rubygems'
0
-require 'rack'
0
-
0
 module Ebb
0
   LIBDIR = File.expand_path(File.dirname(__FILE__))
0
 end
0
0
@@ -14,12 +11,23 @@
0
 
0
 module Ebb
0
   class Client
0
- attr_reader :env, :upload_filename
0
+ BASE_ENV = {
0
+ 'SERVER_SOFTWARE' => 'Ebb 0.0.1',
0
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
0
+ 'GATEWAY_INTERFACE' => 'CGI/1.2',
0
+ 'rack.version' => [0, 1],
0
+ 'rack.errors' => STDERR,
0
+ 'rack.multithread' => false,
0
+ 'rack.multiprocess' => false,
0
+ 'rack.run_once' => false
0
+ }.freeze
0
     
0
     def env
0
- @env.update(
0
- 'rack.input' => Input.new(self)
0
- )
0
+ @env ||= begin
0
+ @env = @ebb_env.update(BASE_ENV)
0
+ @env['rack.input'] = Input.new(self)
0
+ @env
0
+ end
0
     end
0
   end
0
   
0
@@ -70,9 +78,6 @@
0
       init(@host, @port)
0
     end
0
     
0
- # Called by the C library on each request.
0
- # env is a hash containing all the variables of the request
0
- # client is a TCPSocket
0
     def process_client(client)
0
       status, headers, body = @app.call(client.env)
0
       
0
@@ -99,8 +104,7 @@
0
     
0
     def start
0
       trap('INT') { @running = false }
0
- trap('TERM') { @running = false }
0
- really_start
0
+ listen
0
       @running = true
0
       while process_connections and @running
0
         unless @waiting_clients.empty?
0
@@ -111,7 +115,7 @@
0
           process_client(client)
0
         end
0
       end
0
- stop
0
+ deafen
0
     end
0
   end
0
   
...
313
314
315
316
 
317
318
319
...
438
439
440
441
 
442
443
444
...
453
454
455
456
 
457
458
459
...
474
475
476
477
 
478
479
480
...
482
483
484
485
 
486
487
488
489
...
560
561
562
563
564
 
565
566
567
568
569
...
571
572
573
574
575
576
 
577
578
579
...
582
583
584
585
586
587
588
...
313
314
315
 
316
317
318
319
...
438
439
440
 
441
442
443
444
...
453
454
455
 
456
457
458
459
...
474
475
476
 
477
478
479
480
...
482
483
484
 
485
486
487
488
489
...
560
561
562
 
 
563
564
 
565
566
567
...
569
570
571
 
 
 
572
573
574
575
...
578
579
580
 
581
582
583
0
@@ -313,7 +313,7 @@
0
   
0
   if(EV_ERROR & revents) {
0
     ebb_info("on_request() got error event, closing server.");
0
- ebb_server_stop(server);
0
+ ebb_server_deafen(server);
0
     return;
0
   }
0
   
0
@@ -438,7 +438,7 @@
0
 
0
 void ebb_server_free(ebb_server *server)
0
 {
0
- ebb_server_stop(server);
0
+ ebb_server_deafen(server);
0
   
0
   int i;
0
   for(i=0; i < EBB_MAX_CLIENTS; i++)
0
@@ -453,7 +453,7 @@
0
 }
0
 
0
 
0
-void ebb_server_stop(ebb_server *server)
0
+void ebb_server_deafen(ebb_server *server)
0
 {
0
   if(server->open) {
0
     ebb_info("Stopping Ebb server");
0
@@ -474,7 +474,7 @@
0
 }
0
 
0
 
0
-void ebb_server_start(ebb_server *server)
0
+void ebb_server_listen(ebb_server *server)
0
 {
0
   int r = bind( server->fd
0
               , (struct sockaddr*)&(server->sockaddr)
0
@@ -482,7 +482,7 @@
0
               );
0
   if(r < 0) {
0
     ebb_error("Failed to bind to %s %s", server->address, server->port);
0
- ebb_server_stop(server);
0
+ ebb_server_deafen(server);
0
     return;
0
   }
0
   r = listen(server->fd, EBB_MAX_CLIENTS);
0
0
@@ -560,10 +560,8 @@
0
   
0
   ev_timer_again(loop, &(client->timeout_watcher));
0
   
0
- if(client->written == client->write_buffer->len) {
0
- if(client->after_write_cb) client->after_write_cb(client);
0
+ if(client->written == client->write_buffer->len)
0
     ebb_client_close(client);
0
- }
0
 }
0
 
0
 void ebb_client_write(ebb_client *client, const char *data, int length)
0
@@ -571,9 +569,7 @@
0
   g_string_append_len(client->write_buffer, data, length);
0
 }
0
 
0
-void ebb_client_start_writing( ebb_client *client
0
- , ebb_client_cb after_write_cb
0
- )
0
+void ebb_client_finished( ebb_client *client)
0
 {
0
   assert(client->open);
0
   assert(FALSE == ev_is_active(&(client->write_watcher)));
0
@@ -582,7 +578,6 @@
0
   assert(0 <= fcntl(client->fd, F_SETFL, flags | O_NONBLOCK));
0
   
0
   client->written = 0;
0
- client->after_write_cb = after_write_cb;
0
   client->write_watcher.data = client;
0
   ev_init (&(client->write_watcher), ebb_on_writable);
0
   ev_io_set (&(client->write_watcher), client->fd, EV_WRITE | EV_ERROR);
...
38
39
40
41
42
43
44
45
46
47
48
49
 
50
51
52
...
74
75
76
77
78
79
80
...
108
109
110
111
112
 
 
113
114
115
...
38
39
40
 
 
 
41
42
43
 
 
 
44
45
46
47
...
69
70
71
 
72
73
74
...
102
103
104
 
 
105
106
107
108
109
0
@@ -38,15 +38,10 @@
0
   struct sockaddr_in sockaddr;
0
 
0
 /*** Ebb Client ***/
0
-
0
-typedef void (*ebb_client_cb)(ebb_client*);
0
-
0
 void ebb_client_close(ebb_client*);
0
 size_t ebb_client_read(ebb_client*, char *buffer, int length);
0
 void ebb_client_write(ebb_client*, const char *data, int length);
0
-void ebb_client_start_writing( ebb_client *client
0
- , ebb_client_cb after_write_cb
0
- );
0
+void ebb_client_finished( ebb_client *client);
0
 /* User must free the GString returned from ebb_client_read_input */
0
 GString* ebb_client_read_input(ebb_client *client, ssize_t size);
0
 
0
@@ -74,7 +69,6 @@
0
   ev_io write_watcher;
0
   GString *write_buffer;
0
   size_t written;
0
- ebb_client_cb after_write_cb;
0
   
0
   void *data;
0
   
0
@@ -108,8 +102,8 @@
0
                     , void *request_cb_data
0
                     );
0
 void ebb_server_free(ebb_server*);
0
-void ebb_server_start(ebb_server*);
0
-void ebb_server_stop(ebb_server*);
0
+void ebb_server_listen(ebb_server*);
0
+void ebb_server_deafen(ebb_server*);
0
 
0
 struct ebb_server {
0
   EBB_TCP_COMMON
...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
...
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
...
119
120
121
 
122
123
124
125
126
127
128
129
130
131
...
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
...
173
174
175
176
 
177
178
179
180
181
 
182
183
184
...
218
219
220
 
221
222
223
...
225
226
227
228
229
 
 
230
231
232
233
234
235
236
237
...
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
 
 
256
 
257
258
259
 
 
 
 
260
...
23
24
25
 
 
 
 
 
 
26
27
28
29
30
31
32
33
34
...
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
...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
...
117
118
119
120
121
122
123
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
171
172
...
178
179
180
 
181
182
183
 
184
 
185
186
187
188
...
222
223
224
225
226
227
228
...
230
231
232
 
 
233
234
235
236
237
238
239
240
241
242
...
244
245
246
 
 
 
 
 
247
 
 
248
249
250
251
 
 
252
253
254
255
256
 
257
258
259
260
261
262
0
@@ -23,12 +23,6 @@
0
 static VALUE global_server_port;
0
 static VALUE global_path_info;
0
 static VALUE global_content_length;
0
-static VALUE global_gateway_interface;
0
-static VALUE global_gateway_interface_value;
0
-static VALUE global_server_protocol;
0
-static VALUE global_server_protocol_value;
0
-static VALUE global_server_software;
0
-static VALUE global_ebb_version;
0
 static VALUE global_http_host;
0
 
0
 
0
0
0
0
0
0
0
@@ -69,35 +63,31 @@
0
   return Qnil;
0
 }
0
 
0
-VALUE client_env(VALUE client)
0
+VALUE client_env(ebb_client *_client)
0
 {
0
- ebb_client *_client;
0
   VALUE hash = rb_hash_new();
0
   int i;
0
-
0
- Data_Get_Struct(client, ebb_client, _client);
0
+ /* This client->env_fields, client->env_value structure is pretty hacky
0
+ * and a bit hard to follow. Look at the #defines at the top of ebb.c to
0
+ * see what they are doing. Basically it's a list of (ptr,length) pairs
0
+ * for both a field and value
0
+ */
0
   for(i=0; i < _client->env_size; i++) {
0
     rb_hash_aset(hash, env_field(_client->env_fields[i], _client->env_field_lengths[i])
0
                      , rb_str_new(_client->env_values[i], _client->env_value_lengths[i])
0
                      );
0
   }
0
- rb_hash_aset(hash, global_gateway_interface, global_gateway_interface_value);
0
- rb_hash_aset(hash, global_server_protocol, global_server_protocol_value);
0
- rb_hash_aset(hash, global_server_software, global_ebb_version);
0
   rb_hash_aset(hash, global_path_info, rb_hash_aref(hash, global_request_path));
0
-
0
   return hash;
0
 }
0
 
0
 VALUE client_new(ebb_client *_client)
0
 {
0
   VALUE client = Data_Wrap_Struct(cClient, 0, 0, _client);
0
-
0
   _client->data = (void*)client;
0
-
0
- rb_iv_set(client, "@env", client_env(client));
0
- rb_iv_set(client, "@upload_filename", rb_str_new2(_client->upload_file_filename));
0
- rb_iv_set(client, "@write_buffer", rb_ary_new());
0
+ // if(_client->upload_file_filename)
0
+ // rb_iv_set(client, "@upload_filename", rb_str_new2(_client->upload_file_filename));
0
+ rb_iv_set(client, "@ebb_env", client_env(_client));
0
   return client;
0
 }
0
 
0
@@ -119,6 +109,7 @@
0
   return server;
0
 }
0
 
0
+
0
 VALUE server_init(VALUE server, VALUE host, VALUE port)
0
 {
0
   struct ev_loop *loop = ev_default_loop (0);
0
0
0
0
0
0
0
0
@@ -126,40 +117,54 @@
0
   
0
   Data_Get_Struct(server, ebb_server, _server);
0
   ebb_server_init(_server, loop, StringValuePtr(host), FIX2INT(port), request_cb, (void*)server);
0
+
0
+
0
+
0
   return Qnil;
0
 }
0
 
0
-VALUE server_start(VALUE server)
0
+VALUE server_listen(VALUE server)
0
 {
0
   ebb_server *_server;
0
   
0
   Data_Get_Struct(server, ebb_server, _server);
0
   rb_iv_set(server, "@waiting_clients", rb_ary_new());
0
- ebb_server_start(_server);
0
+ ebb_server_listen(_server);
0
   return Qnil;
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 server)
0
 {
0
   ebb_server *_server;
0
   VALUE host, port;
0
   
0
   Data_Get_Struct(server, ebb_server, _server);
0
- //ev_loop(_server->loop, EVLOOP_NONBLOCK);
0
+
0
+
0
+ ev_timer timeout;
0
+ ev_timer_init (&timeout, oneshot_timeout, 0.5, 0.);
0
+ ev_timer_start (_server->loop, &timeout);
0
+
0
   ev_loop(_server->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(_server->loop, &timeout);
0
+
0
   if(_server->open)
0
     return Qtrue;
0
   else
0
     return Qfalse;
0
 }
0
 
0
-VALUE server_stop(VALUE server)
0
+VALUE server_deafen(VALUE server)
0
 {
0
   ebb_server *_server;
0
   Data_Get_Struct(server, ebb_server, _server);
0
- ebb_server_stop(_server);
0
+ ebb_server_deafen(_server);
0
   return Qnil;
0
 }
0
 
0
0
0
@@ -173,12 +178,11 @@
0
   return Qnil;
0
 }
0
 
0
-VALUE client_start_writing(VALUE client)
0
+VALUE client_finished(VALUE client)
0
 {
0
   ebb_client *_client;
0
-
0
   Data_Get_Struct(client, ebb_client, _client);
0
- ebb_client_start_writing(_client, NULL);
0
+ ebb_client_finished(_client);
0
   return Qnil;
0
 }
0
 
0
@@ -218,6 +222,7 @@
0
   return string;
0
 }
0
 
0
+VALUE client_init(VALUE self, VALUE something) {return self;}
0
 
0
 void Init_ebb_ext()
0
 {
0
@@ -225,8 +230,8 @@
0
   cServer = rb_define_class_under(mEbb, "Server", rb_cObject);
0
   cClient = rb_define_class_under(mEbb, "Client", rb_cObject);
0
   
0
-/** Defines global strings in the init method. */
0
-#define DEF_GLOBAL(N, val) global_##N = rb_obj_freeze(rb_str_new2(val)); rb_global_variable(&global_##N)
0
+ /** Defines global strings in the init method. */
0
+#define DEF_GLOBAL(N, val) global_##N = rb_obj_freeze(rb_str_new2(val)); rb_global_variable(&global_##N)
0
   DEF_GLOBAL(http_prefix, "HTTP_");
0
   DEF_GLOBAL(request_method, "REQUEST_METHOD");
0
   DEF_GLOBAL(request_uri, "REQUEST_URI");
0
0
0
0
0
0
@@ -239,23 +244,20 @@
0
   DEF_GLOBAL(server_port, "SERVER_PORT");
0
   DEF_GLOBAL(path_info, "PATH_INFO");
0
   DEF_GLOBAL(content_length, "HTTP_CONTENT_LENGTH");
0
-
0
- DEF_GLOBAL(gateway_interface, "GATEWAY_INTERFACE");
0
- DEF_GLOBAL(gateway_interface_value, "CGI/1.2");
0
- DEF_GLOBAL(server_protocol, "SERVER_PROTOCOL");
0
- DEF_GLOBAL(server_protocol_value, "HTTP/1.1");
0
   DEF_GLOBAL(http_host, "HTTP_HOST");
0
- DEF_GLOBAL(ebb_version, "Ebb 0.0.1"); /* XXX Why is this defined here? */
0
- DEF_GLOBAL(server_software, "SERVER_SOFTWARE");
0
   
0
   rb_define_alloc_func(cServer, server_alloc);
0
   rb_define_method(cServer, "init", server_init, 2);
0
   rb_define_method(cServer, "process_connections", server_process_connections, 0);
0
- rb_define_method(cServer, "really_start", server_start, 0);
0
- rb_define_method(cServer, "stop", server_stop, 0);
0
+ rb_define_method(cServer, "listen", server_listen, 0);
0
+ rb_define_method(cServer, "deafen", server_deafen, 0);
0
   
0
+ rb_define_method(cClient, "initialize", client_init, 1);
0
   rb_define_method(cClient, "write", client_write, 1);
0
- rb_define_method(cClient, "finished", client_start_writing, 0);
0
   rb_define_method(cClient, "read_input", client_read_input, 1);
0
+ rb_define_method(cClient, "finished", client_finished, 0);
0
+
0
+ rb_define_method(cClient, "read_input", client_read_input, 1);
0
+
0
 }

Comments

    No one has commented yet.