Skip to content
This repository
Browse code

WIP: nw conn

  • Loading branch information...
commit 390c8b3ebf1b38b2c823be64647e3936cbb9ca30 1 parent eac0a2c
Alvaro Lopez Ortega authored January 12, 2012
3  cherokee/Makefile.am
@@ -1578,6 +1578,8 @@ $(common_rrd_tools) \
1578 1578
 request-protected.h \
1579 1579
 request.h \
1580 1580
 request.c \
  1581
+connection.h \
  1582
+connection.c \
1581 1583
 connection-poll.h \
1582 1584
 connection-poll.c \
1583 1585
 handler.h \
@@ -1677,6 +1679,7 @@ logger.h \
1677 1679
 handler.h \
1678 1680
 encoder.h \
1679 1681
 request.h \
  1682
+connection.h \
1680 1683
 plugin.h \
1681 1684
 plugin_loader.h \
1682 1685
 nonce.h \
21  cherokee/request-protected.h
@@ -105,6 +105,7 @@ struct cherokee_request {
105 105
 	void                         *server;
106 106
 	void                         *vserver;
107 107
 	void                         *thread;
  108
+	void                         *conn;
108 109
 	cherokee_bind_t              *bind;
109 110
 	cherokee_config_entry_ref_t   config_entry;
110 111
 
@@ -115,9 +116,8 @@ struct cherokee_request {
115 116
 
116 117
 	/* Socket stuff
117 118
 	 */
118  
-	cherokee_socket_t             socket;
119 119
 	cherokee_http_upgrade_t       upgrade;
120  
-	cherokee_request_options_t options;
  120
+	cherokee_request_options_t    options;
121 121
 	cherokee_handler_t           *handler;
122 122
 
123 123
 	cherokee_logger_t            *logger_ref;
@@ -131,7 +131,7 @@ struct cherokee_request {
131 131
 
132 132
 	/* State
133 133
 	 */
134  
-	cherokee_request_phase_t   phase;
  134
+	cherokee_request_phase_t      phase;
135 135
 	cherokee_http_t               error_code;
136 136
 	cherokee_buffer_t             error_internal_url;
137 137
 	cherokee_buffer_t             error_internal_qs;
@@ -185,14 +185,12 @@ struct cherokee_request {
185 185
 
186 186
 	/* Net connection
187 187
 	 */
188  
-	uint32_t                      keepalive;
189 188
 	time_t                        timeout;
190 189
 	time_t                        timeout_lapse;
191 190
 	cherokee_buffer_t            *timeout_header;
192 191
 
193 192
 	/* Polling
194 193
 	 */
195  
-	cherokee_connection_pool_t    polling_aim;
196 194
 
197 195
 	off_t                         range_start;
198 196
 	off_t                         range_end;
@@ -239,12 +237,13 @@ struct cherokee_request {
239 237
 	cherokee_msec_t               limit_blocked_until;
240 238
 };
241 239
 
242  
-#define REQ_SRV(c)    (SRV(REQ(c)->server))
243  
-#define REQ_HDR(c)    (HDR(REQ(c)->header))
244  
-#define REQ_SOCK(c)   (SOCKET(REQ(c)->socket))
245  
-#define REQ_VSRV(c)   (VSERVER(REQ(c)->vserver))
246  
-#define REQ_THREAD(c) (THREAD(REQ(c)->thread))
247  
-#define REQ_BIND(c)   (BIND(REQ(c)->bind))
  240
+#define REQ_CONN(r)   (CONN(REQ(r)->conn))
  241
+#define REQ_SRV(r)    (SRV(REQ(r)->server))
  242
+#define REQ_HDR(r)    (HDR(REQ(r)->header))
  243
+#define REQ_SOCK(r)   (SOCKET(REQ(r)->socket))
  244
+#define REQ_VSRV(r)   (VSERVER(REQ(r)->vserver))
  245
+#define REQ_THREAD(r) (THREAD(REQ(r)->thread))
  246
+#define REQ_BIND(r)   (BIND(REQ(r)->bind))
248 247
 
249 248
 #define TRACE_REQ(c)  TRACE("conn", "%s", cherokee_request_print(c));
250 249
 
16  cherokee/request.c
@@ -102,7 +102,6 @@ cherokee_request_new  (cherokee_request_t **req)
102 102
 	n->encoder_new_func          = NULL;
103 103
 	n->encoder_props             = NULL;
104 104
 	n->logger_ref                = NULL;
105  
-	n->keepalive                 = 0;
106 105
 	n->range_start               = -1;
107 106
 	n->range_end                 = -1;
108 107
 	n->vserver                   = NULL;
@@ -111,6 +110,7 @@ cherokee_request_new  (cherokee_request_t **req)
111 110
 	n->mmaped_len                = 0;
112 111
 	n->io_entry_ref              = NULL;
113 112
 	n->thread                    = NULL;
  113
+	n->conn                      = NULL;
114 114
 	n->rx                        = 0;
115 115
 	n->tx                        = 0;
116 116
 	n->rx_partial                = 0;
@@ -153,10 +153,8 @@ cherokee_request_new  (cherokee_request_t **req)
153 153
 	cherokee_buffer_init (&n->request_original);
154 154
 	cherokee_buffer_init (&n->query_string_original);
155 155
 
156  
-	cherokee_socket_init (&n->socket);
157 156
 	cherokee_header_init (&n->header, header_type_request);
158 157
 	cherokee_post_init (&n->post, n);
159  
-	cherokee_request_poll_init (&n->polling_aim);
160 158
 
161 159
 	memset (n->regex_ovector, 0, OVECTOR_LEN * sizeof(int));
162 160
 	n->regex_ovecsize = 0;
@@ -180,7 +178,6 @@ ret_t
180 178
 cherokee_request_free (cherokee_request_t *req)
181 179
 {
182 180
 	cherokee_header_mrproper (&req->header);
183  
-	cherokee_socket_mrproper (&req->socket);
184 181
 
185 182
 	if (req->handler != NULL) {
186 183
 		cherokee_handler_free (req->handler);
@@ -230,12 +227,6 @@ cherokee_request_free (cherokee_request_t *req)
230 227
 		req->arguments = NULL;
231 228
 	}
232 229
 
233  
-
234  
-	if (req->polling_aim.fd != -1) {
235  
-                cherokee_fd_close (req->polling_aim.fd);
236  
-	}
237  
-	cherokee_request_poll_mrproper (&req->polling_aim);
238  
-
239 230
 	free (req);
240 231
 	return ret_ok;
241 232
 }
@@ -288,11 +279,6 @@ cherokee_request_clean (cherokee_request_t *req,
288 279
 	req->encoder_new_func = NULL;
289 280
 	req->encoder_props    = NULL;
290 281
 
291  
-	if (req->polling_aim.fd != -1) {
292  
-                cherokee_fd_close (req->polling_aim.fd);
293  
-	}
294  
-	cherokee_request_poll_clean (&req->polling_aim);
295  
-
296 282
 	if (req->validator != NULL) {
297 283
 		cherokee_validator_free (req->validator);
298 284
 		req->validator = NULL;
6  cherokee/request.h
@@ -41,12 +41,12 @@ typedef struct cherokee_request cherokee_request_t;
41 41
 
42 42
 /* Public methods
43 43
  */
44  
-ret_t  cherokee_request_set_cork      (cherokee_request_t *conn, cherokee_boolean_t enable);
45  
-ret_t  cherokee_request_parse_args    (cherokee_request_t *conn);
  44
+ret_t cherokee_request_set_cork   (cherokee_request_t *req, cherokee_boolean_t enable);
  45
+ret_t cherokee_request_parse_args (cherokee_request_t *req);
46 46
 
47 47
 /* Handy functions
48 48
  */
49  
-const char *cherokee_request_get_phase_str (cherokee_request_t *conn);
  49
+const char *cherokee_request_get_phase_str (cherokee_request_t *req);
50 50
 
51 51
 CHEROKEE_END_DECLS
52 52
 
158  cherokee/thread.c
@@ -145,10 +145,9 @@ cherokee_thread_new  (cherokee_thread_t      **thd,
145 145
 
146 146
 	/* Init
147 147
 	 */
148  
-	INIT_LIST_HEAD (LIST(&n->base));
149  
-	INIT_LIST_HEAD (LIST(&n->active_list));
150  
-	INIT_LIST_HEAD (LIST(&n->reuse_list));
151  
-	INIT_LIST_HEAD (LIST(&n->polling_list));
  148
+	INIT_LIST_HEAD (&n->base);
  149
+	INIT_LIST_HEAD (&n->active_list);
  150
+	INIT_LIST_HEAD (&n->polling_list);
152 151
 
153 152
 	n->exit                = false;
154 153
 	n->ended               = false;
@@ -161,11 +160,16 @@ cherokee_thread_new  (cherokee_thread_t      **thd,
161 160
 	n->conns_max           = conns_max;
162 161
 	n->conns_keepalive_max = keepalive_max;
163 162
 
164  
-	n->reuse_list_num      = 0;
165  
-
166 163
 	n->fastcgi_servers     = NULL;
167 164
 	n->fastcgi_free_func   = NULL;
168 165
 
  166
+	/* Reuse */
  167
+	INIT_LIST_HEAD (&n->reuse.reqs);
  168
+	INIT_LIST_HEAD (&n->reuse.conns);
  169
+
  170
+	n->reuse.reqs_num  = 0;
  171
+	n->reuse.conns_num = 0;
  172
+
169 173
 	/* Thread Local Storage
170 174
 	 */
171 175
 	CHEROKEE_THREAD_PROP_SET (thread_error_writer_ptr, NULL);
@@ -288,23 +292,80 @@ del_connection_polling (cherokee_thread_t *thd, cherokee_request_t *req)
288 292
 }
289 293
 
290 294
 
291  
-static ret_t
292  
-connection_reuse_or_free (cherokee_thread_t *thread, cherokee_request_t *req)
  295
+ret_t
  296
+cherokee_thread_get_new_connection (cherokee_thread_t      *thd,
  297
+				    cherokee_connection_t **conn)
  298
+{
  299
+	/* New obj */
  300
+	if (thread->reuse.conns_num <= 0) {
  301
+		return cherokee_connection_new (conn);
  302
+	}
  303
+
  304
+	/* Reuse obj */
  305
+	*conn = CONN(thd->reuse.conns.prev);
  306
+	cherokee_list_del (LIST(*conn));
  307
+	INIT_LIST_HEAD (LIST(*conn));
  308
+	thread->reuse.conns_num--;
  309
+
  310
+	return ret_ok;
  311
+}
  312
+
  313
+ret_t
  314
+cherokee_thread_recycle_connection (cherokee_thread_t     *thd,
  315
+				    cherokee_connection_t *conn)
293 316
 {
294 317
 	/* Disable keepalive in the connection
295 318
 	 */
296  
-	req->keepalive = 0;
  319
+	conn->keepalive = 0;
297 320
 
298 321
 	/* Check the max connection reuse number
299 322
 	 */
300  
-	if (thread->reuse_list_num >= THREAD_SRV(thread)->conns_reuse_max) {
  323
+	if (thread->reuse.conns_num >= THREAD_SRV(thread)->conns_reuse_max) {
  324
+		return cherokee_connection_free (conn);
  325
+	}
  326
+
  327
+	/* Add it to the reusable connection list
  328
+	 */
  329
+	cherokee_list_add (LIST(conn), &thread->reuse.conns);
  330
+	thread->reuse.conns_num++;
  331
+
  332
+	return ret_ok;
  333
+}
  334
+
  335
+
  336
+ret_t
  337
+cherokee_thread_get_new_request (cherokee_thread_t   *thd,
  338
+				 cherokee_request_t **req)
  339
+{
  340
+	/* New obj */
  341
+	if (thread->reuse.reqs_num <= 0) {
  342
+		return cherokee_request_new (req);
  343
+	}
  344
+
  345
+	/* Reuse obj */
  346
+	*req = REQ(thd->reuse.reqs.prev);
  347
+	cherokee_list_del (LIST(*req));
  348
+	INIT_LIST_HEAD (LIST(*req));
  349
+	thread->reuse.reqs_num--;
  350
+
  351
+	return ret_ok;
  352
+}
  353
+
  354
+
  355
+ret_t
  356
+cherokee_thread_recycle_request (cherokee_thread_t     *thd,
  357
+				 cherokee_connection_t *req)
  358
+{
  359
+	/* Check the max connection reuse number
  360
+	 */
  361
+	if (thread->reuse.reqs_num >= THREAD_SRV(thread)->conns_reuse_max) {
301 362
 		return cherokee_request_free (req);
302 363
 	}
303 364
 
304 365
 	/* Add it to the reusable connection list
305 366
 	 */
306  
-	cherokee_list_add (LIST(req), &thread->reuse_list);
307  
-	thread->reuse_list_num++;
  367
+	cherokee_list_add (LIST(req), &thread->reuse.reqs);
  368
+	thread->reuse.reqs_num++;
308 369
 
309 370
 	return ret_ok;
310 371
 }
@@ -328,7 +389,7 @@ purge_connection (cherokee_thread_t *thread, cherokee_request_t *req)
328 389
 
329 390
 	/* Add it to the reusable list
330 391
 	 */
331  
-	connection_reuse_or_free (thread, req);
  392
+	cherokee_thread_recycle_connection (thread, conn);
332 393
 }
333 394
 
334 395
 
@@ -454,13 +515,13 @@ send_hardcoded_error (cherokee_socket_t *sock,
454 515
 static void
455 516
 process_polling_connections (cherokee_thread_t *thd)
456 517
 {
457  
-	int                 re;
458  
-	ret_t               ret;
459  
-	cherokee_list_t    *tmp, *i;
460  
-	cherokee_request_t *req;
  518
+	int                    re;
  519
+	ret_t                  ret;
  520
+	cherokee_connection_t *conn;
  521
+	cherokee_list_t       *tmp, *i;
461 522
 
462 523
 	list_for_each_safe (i, tmp, LIST(&thd->polling_list)) {
463  
-		req = REQ(i);
  524
+		conn = CONN(i);
464 525
 
465 526
 		/* Thread's properties
466 527
 		 */
@@ -1472,38 +1533,25 @@ thread_full_handler (cherokee_thread_t *thd,
1472 1533
 
1473 1534
 
1474 1535
 static ret_t
1475  
-get_new_connection (cherokee_thread_t *thd, cherokee_request_t **req)
  1536
+get_new_connection (cherokee_thread_t      *thd,
  1537
+		    cherokee_connection_t **conn)
1476 1538
 {
1477  
-	cherokee_request_t *new_connection;
1478  
-	cherokee_server_t  *server;
1479  
-	static cuint_t      last_conn_id = 0;
  1539
+	cherokee_connection_t *new_connection;
  1540
+	cherokee_server_t     *server          = SRV(thd->server);
  1541
+	static cuint_t         last_conn_id    = 0;
1480 1542
 
1481  
-	server = SRV(thd->server);
1482  
-
1483  
-	if (cherokee_list_empty (&thd->reuse_list)) {
1484  
-		ret_t ret;
1485  
-
1486  
-		/* Create new connection object
1487  
-		 */
1488  
-		ret = cherokee_request_new (&new_connection);
1489  
-		if (unlikely(ret < ret_ok)) return ret;
1490  
-	} else {
1491  
-		/* Reuse an old one
1492  
-		 */
1493  
-		new_connection = REQ(thd->reuse_list.prev);
1494  
-		cherokee_list_del (LIST(new_connection));
1495  
-		thd->reuse_list_num--;
1496  
-
1497  
-		INIT_LIST_HEAD (LIST(new_connection));
  1543
+	/* New connection object */
  1544
+	ret = cherokee_thread_get_new_connection (thd, &new_connection);
  1545
+	if (unlikely(ret < ret_ok)) {
  1546
+		return ret;
1498 1547
 	}
1499 1548
 
1500 1549
 	/* Set the basic information to the connection
1501 1550
 	 */
1502  
-	new_connection->id        = last_conn_id++;
1503  
-	new_connection->thread    = thd;
1504  
-	new_connection->server    = server;
1505  
-	new_connection->vserver   = VSERVER(server->vservers.prev);
1506  
-
  1551
+	new_connection->id           = last_conn_id++;
  1552
+	new_connection->thread       = thd;
  1553
+	new_connection->server       = server;
  1554
+	new_connection->vserver      = VSERVER(server->vservers.prev);
1507 1555
 	new_connection->traffic_next = cherokee_bogonow_now + DEFAULT_TRAFFIC_UPDATE;
1508 1556
 
1509 1557
 	/* Set the default server timeout
@@ -1512,7 +1560,7 @@ get_new_connection (cherokee_thread_t *thd, cherokee_request_t **req)
1512 1560
 	new_connection->timeout_lapse  = server->timeout;
1513 1561
 	new_connection->timeout_header = &server->timeout_header;
1514 1562
 
1515  
-	*req = new_connection;
  1563
+	*conn = new_connection;
1516 1564
 	return ret_ok;
1517 1565
 }
1518 1566
 
@@ -1527,6 +1575,7 @@ accept_new_connection (cherokee_thread_t *thd,
1527 1575
 	cherokee_request_t  *new_conn  = NULL;
1528 1576
 	int                  new_fd    = -1;
1529 1577
 	cherokee_server_t   *srv       = THREAD_SRV(thd);
  1578
+	cherokee_boolean_t   lock_set  = false;
1530 1579
 
1531 1580
 	/* Check whether there are connections waiting
1532 1581
 	 */
@@ -1556,21 +1605,21 @@ accept_new_connection (cherokee_thread_t *thd,
1556 1605
 		goto error;
1557 1606
 	}
1558 1607
 
1559  
-	/* We got a new_conn object, on error we can goto error.
  1608
+	/* Set the actual fd info in the connection
1560 1609
 	 */
1561 1610
 	ret = cherokee_socket_set_sockaddr (&new_conn->socket, new_fd, &new_sa);
  1611
+	if (unlikely(ret < ret_ok)) {
  1612
+		LOG_ERROR_S (CHEROKEE_ERROR_THREAD_SET_SOCKADDR);
  1613
+		goto error;
  1614
+	}
1562 1615
 
1563 1616
 	/* It is about to add a new connection to the thread,
1564 1617
 	 * so it MUST adquire the thread ownership
1565 1618
 	 * (do it now to better handle error cases).
1566 1619
 	 */
  1620
+	lock_set = true;
1567 1621
 	CHEROKEE_MUTEX_LOCK (&thd->ownership);
1568 1622
 
1569  
-	if (unlikely(ret < ret_ok)) {
1570  
-		LOG_ERROR_S (CHEROKEE_ERROR_THREAD_SET_SOCKADDR);
1571  
-		goto error;
1572  
-	}
1573  
-
1574 1623
 	/* TLS support, set initial connection phase.
1575 1624
 	 */
1576 1625
 	if (bind->socket.is_tls == TLS) {
@@ -1593,7 +1642,6 @@ accept_new_connection (cherokee_thread_t *thd,
1593 1642
 	/* Lets add the new connection
1594 1643
 	 */
1595 1644
 	add_connection (thd, new_conn);
1596  
-
1597 1645
 	thd->conns_num++;
1598 1646
 
1599 1647
 	/* Release the thread ownership
@@ -1615,12 +1663,14 @@ accept_new_connection (cherokee_thread_t *thd,
1615 1663
 	 */
1616 1664
 	if (new_conn) {
1617 1665
 		S_SOCKET_FD(new_conn->socket) = -1;
1618  
-		connection_reuse_or_free (thd, new_conn);
  1666
+		cherokee_thread_recycle_connection (thd, new_conn);
1619 1667
 	}
1620 1668
 
1621 1669
 	/* Release the thread ownership
1622 1670
 	 */
1623  
-	CHEROKEE_MUTEX_UNLOCK (&thd->ownership);
  1671
+	if (lock_set) {
  1672
+		CHEROKEE_MUTEX_UNLOCK (&thd->ownership);
  1673
+	}
1624 1674
 	return ret_deny;
1625 1675
 }
1626 1676
 
18  cherokee/thread.h
@@ -76,8 +76,17 @@ typedef struct {
76 76
 
77 77
 	cherokee_list_t         active_list;
78 78
 	cherokee_list_t         polling_list;
79  
-	cherokee_list_t         reuse_list;
80  
-	int                     reuse_list_num;      /* reusable connections objs */
  79
+
  80
+	struct {
  81
+		cherokee_list_t conns;
  82
+		int             conns_num;
  83
+		cherokee_list_t reqs;
  84
+		int             reqs_num;
  85
+	} reuse;
  86
+
  87
+
  88
+//	cherokee_list_t         reuse_list;
  89
+//	int                     reuse_list_num;      /* reusable connections objs */
81 90
 	cherokee_limiter_t      limiter;             /* Traffic shaping */
82 91
 	cherokee_boolean_t      is_full;
83 92
 
@@ -124,4 +133,9 @@ ret_t cherokee_thread_inject_active_connection   (cherokee_thread_t *thd, cherok
124 133
 ret_t cherokee_thread_close_all_connections      (cherokee_thread_t *thd);
125 134
 ret_t cherokee_thread_close_polling_connections  (cherokee_thread_t *thd, int fd, cuint_t *num);
126 135
 
  136
+ret_t cherokee_thread_get_new_request            (cherokee_thread_t *thd, cherokee_request_t    **req);
  137
+ret_t cherokee_thread_recycle_request            (cherokee_thread_t *thd, cherokee_request_t     *req);
  138
+ret_t cherokee_thread_get_new_connection         (cherokee_thread_t *thd, cherokee_connection_t **conn);
  139
+ret_t cherokee_thread_recycle_connection         (cherokee_thread_t *thd, cherokee_connection_t  *conn);
  140
+
127 141
 #endif /* CHEROKEE_THREAD_H */
4  cherokee/trace.c
@@ -149,7 +149,7 @@ cherokee_trace_do_trace (const char *entry, const char *file, int line, const ch
149 149
 	char                  *lentry;
150 150
 	char                  *lentry_end;
151 151
 	va_list                args;
152  
-	cherokee_request_t    *conn;
  152
+	cherokee_connection_t *conn;
153 153
 	cherokee_buffer_t     *trace_modules = &trace.modules;
154 154
 	cherokee_boolean_t     do_log        = false;
155 155
 	cherokee_buffer_t      entries       = CHEROKEE_BUF_INIT;
@@ -171,7 +171,7 @@ cherokee_trace_do_trace (const char *entry, const char *file, int line, const ch
171 171
 	/* Check the connection source, if possible
172 172
 	 */
173 173
 	if (trace.from_filter != NULL) {
174  
-		conn = REQ (CHEROKEE_THREAD_PROP_GET (thread_connection_ptr));
  174
+		conn = CONN(CHEROKEE_THREAD_PROP_GET (thread_connection_ptr));
175 175
 
176 176
 		/* No conn, no trace entry
177 177
 		 */

0 notes on commit 390c8b3

Please sign in to comment.
Something went wrong with that request. Please try again.