public
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
Search Repo:
Add timeout watcher
macournoyer (author)
Mon Apr 07 19:09:48 -0700 2008
commit  747a41479340d52976aa067c3e93198a40d74627
tree    354de35f74bf9ed1b063f09781b799017d8604d8
parent  86dd5e916aee6553ba0936e9b356c43a53f46b17
...
29
30
31
 
32
33
34
...
49
50
51
 
52
53
54
55
...
58
59
60
 
 
 
 
 
 
61
 
 
62
63
64
...
114
115
116
117
118
 
 
 
 
119
120
121
...
234
235
236
 
237
238
239
...
29
30
31
32
33
34
35
...
50
51
52
53
54
55
56
57
...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
...
124
125
126
 
 
127
128
129
130
131
132
133
...
246
247
248
249
250
251
252
0
@@ -29,6 +29,7 @@
0
               (char *) c->write_buffer.ptr + c->write_buffer.current,
0
               c->write_buffer.len - c->write_buffer.current,
0
               0);
0
+ ev_timer_again(c->loop, &c->timeout_watcher);
0
   
0
   if (sent > 0) {
0
     c->write_buffer.current += sent;
0
@@ -49,6 +50,7 @@
0
   char buf[BUFFER_SIZE];
0
   
0
   n = recv(c->fd, buf, BUFFER_SIZE, 0);
0
+ ev_timer_again(c->loop, &c->timeout_watcher);
0
   
0
   if (n == -1) {
0
     connection_errno(c);
0
0
@@ -58,7 +60,15 @@
0
   connection_parse(c, buf, n);
0
 }
0
 
0
+static void connection_timeout_cb(EV_P_ struct ev_timer *watcher, int revents)
0
+{
0
+ connection_t *c = get_ev_data(connection, watcher, timeout);
0
+
0
+ connection_close(c);
0
+}
0
 
0
+
0
+
0
 /* public api */
0
 
0
 void connection_start(backend_t *backend, int fd, struct sockaddr_in remote_addr)
0
@@ -114,8 +124,10 @@
0
   
0
   /* init libev stuff */
0
   watch(c, connection_readable_cb, read, EV_READ);
0
-
0
- /* TODO add timeout watcher */
0
+ /* timeout watcher, close connection when peer not responding */
0
+ c->timeout_watcher.data = c;
0
+ ev_timer_init(&c->timeout_watcher, connection_timeout_cb, CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);
0
+ ev_timer_start(c->loop, &c->timeout_watcher);
0
 }
0
 
0
 void connection_parse(connection_t *c, char *buf, int len)
0
@@ -234,6 +246,7 @@
0
 {
0
   unwatch(c, read);
0
   unwatch(c, write);
0
+ ev_timer_stop(c->loop, &c->timeout_watcher);
0
 
0
   close(c->fd);
0
   
...
26
27
28
29
 
30
31
 
32
33
34
35
36
 
 
 
 
 
37
38
39
...
67
68
69
 
70
71
72
...
26
27
28
 
29
30
 
31
32
 
 
 
 
33
34
35
36
37
38
39
40
...
68
69
70
71
72
73
74
0
@@ -26,14 +26,15 @@
0
 #include "status.h"
0
 
0
 #ifdef __FreeBSD__
0
-#define LISTEN_BACKLOG -1
0
+#define LISTEN_BACKLOG -1
0
 #else
0
-#define LISTEN_BACKLOG 511
0
+#define LISTEN_BACKLOG 511
0
 #endif
0
-#define CONNECTIONS_SIZE 100
0
-#define BUFFER_SLICES (80 + 32) /* big enough so we can fit MAX_HEADER */
0
-#define BUFFER_SIZE 1024
0
-#define MAX_HEADER 1024 * (80 + 32)
0
+#define CONNECTIONS_SIZE 100
0
+#define CONNECTION_TIMEOUT 30.0
0
+#define BUFFER_SLICES (80 + 32) /* big enough so we can fit MAX_HEADER */
0
+#define BUFFER_SIZE 1024
0
+#define MAX_HEADER 1024 * (80 + 32)
0
 
0
 
0
 #define LF (u_char) 10
0
@@ -67,6 +68,7 @@
0
   struct ev_loop *loop;
0
   ev_io read_watcher;
0
   ev_io write_watcher;
0
+ ev_timer timeout_watcher;
0
 };
0
 
0
 struct backend_s {

Comments

    No one has commented yet.