Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

WIP: nw conn

  • Loading branch information...
commit 390c8b3ebf1b38b2c823be64647e3936cbb9ca30 1 parent eac0a2c
@alobbs alobbs authored
View
3  cherokee/Makefile.am
@@ -1578,6 +1578,8 @@ $(common_rrd_tools) \
request-protected.h \
request.h \
request.c \
+connection.h \
+connection.c \
connection-poll.h \
connection-poll.c \
handler.h \
@@ -1677,6 +1679,7 @@ logger.h \
handler.h \
encoder.h \
request.h \
+connection.h \
plugin.h \
plugin_loader.h \
nonce.h \
View
21 cherokee/request-protected.h
@@ -105,6 +105,7 @@ struct cherokee_request {
void *server;
void *vserver;
void *thread;
+ void *conn;
cherokee_bind_t *bind;
cherokee_config_entry_ref_t config_entry;
@@ -115,9 +116,8 @@ struct cherokee_request {
/* Socket stuff
*/
- cherokee_socket_t socket;
cherokee_http_upgrade_t upgrade;
- cherokee_request_options_t options;
+ cherokee_request_options_t options;
cherokee_handler_t *handler;
cherokee_logger_t *logger_ref;
@@ -131,7 +131,7 @@ struct cherokee_request {
/* State
*/
- cherokee_request_phase_t phase;
+ cherokee_request_phase_t phase;
cherokee_http_t error_code;
cherokee_buffer_t error_internal_url;
cherokee_buffer_t error_internal_qs;
@@ -185,14 +185,12 @@ struct cherokee_request {
/* Net connection
*/
- uint32_t keepalive;
time_t timeout;
time_t timeout_lapse;
cherokee_buffer_t *timeout_header;
/* Polling
*/
- cherokee_connection_pool_t polling_aim;
off_t range_start;
off_t range_end;
@@ -239,12 +237,13 @@ struct cherokee_request {
cherokee_msec_t limit_blocked_until;
};
-#define REQ_SRV(c) (SRV(REQ(c)->server))
-#define REQ_HDR(c) (HDR(REQ(c)->header))
-#define REQ_SOCK(c) (SOCKET(REQ(c)->socket))
-#define REQ_VSRV(c) (VSERVER(REQ(c)->vserver))
-#define REQ_THREAD(c) (THREAD(REQ(c)->thread))
-#define REQ_BIND(c) (BIND(REQ(c)->bind))
+#define REQ_CONN(r) (CONN(REQ(r)->conn))
+#define REQ_SRV(r) (SRV(REQ(r)->server))
+#define REQ_HDR(r) (HDR(REQ(r)->header))
+#define REQ_SOCK(r) (SOCKET(REQ(r)->socket))
+#define REQ_VSRV(r) (VSERVER(REQ(r)->vserver))
+#define REQ_THREAD(r) (THREAD(REQ(r)->thread))
+#define REQ_BIND(r) (BIND(REQ(r)->bind))
#define TRACE_REQ(c) TRACE("conn", "%s", cherokee_request_print(c));
View
16 cherokee/request.c
@@ -102,7 +102,6 @@ cherokee_request_new (cherokee_request_t **req)
n->encoder_new_func = NULL;
n->encoder_props = NULL;
n->logger_ref = NULL;
- n->keepalive = 0;
n->range_start = -1;
n->range_end = -1;
n->vserver = NULL;
@@ -111,6 +110,7 @@ cherokee_request_new (cherokee_request_t **req)
n->mmaped_len = 0;
n->io_entry_ref = NULL;
n->thread = NULL;
+ n->conn = NULL;
n->rx = 0;
n->tx = 0;
n->rx_partial = 0;
@@ -153,10 +153,8 @@ cherokee_request_new (cherokee_request_t **req)
cherokee_buffer_init (&n->request_original);
cherokee_buffer_init (&n->query_string_original);
- cherokee_socket_init (&n->socket);
cherokee_header_init (&n->header, header_type_request);
cherokee_post_init (&n->post, n);
- cherokee_request_poll_init (&n->polling_aim);
memset (n->regex_ovector, 0, OVECTOR_LEN * sizeof(int));
n->regex_ovecsize = 0;
@@ -180,7 +178,6 @@ ret_t
cherokee_request_free (cherokee_request_t *req)
{
cherokee_header_mrproper (&req->header);
- cherokee_socket_mrproper (&req->socket);
if (req->handler != NULL) {
cherokee_handler_free (req->handler);
@@ -230,12 +227,6 @@ cherokee_request_free (cherokee_request_t *req)
req->arguments = NULL;
}
-
- if (req->polling_aim.fd != -1) {
- cherokee_fd_close (req->polling_aim.fd);
- }
- cherokee_request_poll_mrproper (&req->polling_aim);
-
free (req);
return ret_ok;
}
@@ -288,11 +279,6 @@ cherokee_request_clean (cherokee_request_t *req,
req->encoder_new_func = NULL;
req->encoder_props = NULL;
- if (req->polling_aim.fd != -1) {
- cherokee_fd_close (req->polling_aim.fd);
- }
- cherokee_request_poll_clean (&req->polling_aim);
-
if (req->validator != NULL) {
cherokee_validator_free (req->validator);
req->validator = NULL;
View
6 cherokee/request.h
@@ -41,12 +41,12 @@ typedef struct cherokee_request cherokee_request_t;
/* Public methods
*/
-ret_t cherokee_request_set_cork (cherokee_request_t *conn, cherokee_boolean_t enable);
-ret_t cherokee_request_parse_args (cherokee_request_t *conn);
+ret_t cherokee_request_set_cork (cherokee_request_t *req, cherokee_boolean_t enable);
+ret_t cherokee_request_parse_args (cherokee_request_t *req);
/* Handy functions
*/
-const char *cherokee_request_get_phase_str (cherokee_request_t *conn);
+const char *cherokee_request_get_phase_str (cherokee_request_t *req);
CHEROKEE_END_DECLS
View
158 cherokee/thread.c
@@ -145,10 +145,9 @@ cherokee_thread_new (cherokee_thread_t **thd,
/* Init
*/
- INIT_LIST_HEAD (LIST(&n->base));
- INIT_LIST_HEAD (LIST(&n->active_list));
- INIT_LIST_HEAD (LIST(&n->reuse_list));
- INIT_LIST_HEAD (LIST(&n->polling_list));
+ INIT_LIST_HEAD (&n->base);
+ INIT_LIST_HEAD (&n->active_list);
+ INIT_LIST_HEAD (&n->polling_list);
n->exit = false;
n->ended = false;
@@ -161,11 +160,16 @@ cherokee_thread_new (cherokee_thread_t **thd,
n->conns_max = conns_max;
n->conns_keepalive_max = keepalive_max;
- n->reuse_list_num = 0;
-
n->fastcgi_servers = NULL;
n->fastcgi_free_func = NULL;
+ /* Reuse */
+ INIT_LIST_HEAD (&n->reuse.reqs);
+ INIT_LIST_HEAD (&n->reuse.conns);
+
+ n->reuse.reqs_num = 0;
+ n->reuse.conns_num = 0;
+
/* Thread Local Storage
*/
CHEROKEE_THREAD_PROP_SET (thread_error_writer_ptr, NULL);
@@ -288,23 +292,80 @@ del_connection_polling (cherokee_thread_t *thd, cherokee_request_t *req)
}
-static ret_t
-connection_reuse_or_free (cherokee_thread_t *thread, cherokee_request_t *req)
+ret_t
+cherokee_thread_get_new_connection (cherokee_thread_t *thd,
+ cherokee_connection_t **conn)
+{
+ /* New obj */
+ if (thread->reuse.conns_num <= 0) {
+ return cherokee_connection_new (conn);
+ }
+
+ /* Reuse obj */
+ *conn = CONN(thd->reuse.conns.prev);
+ cherokee_list_del (LIST(*conn));
+ INIT_LIST_HEAD (LIST(*conn));
+ thread->reuse.conns_num--;
+
+ return ret_ok;
+}
+
+ret_t
+cherokee_thread_recycle_connection (cherokee_thread_t *thd,
+ cherokee_connection_t *conn)
{
/* Disable keepalive in the connection
*/
- req->keepalive = 0;
+ conn->keepalive = 0;
/* Check the max connection reuse number
*/
- if (thread->reuse_list_num >= THREAD_SRV(thread)->conns_reuse_max) {
+ if (thread->reuse.conns_num >= THREAD_SRV(thread)->conns_reuse_max) {
+ return cherokee_connection_free (conn);
+ }
+
+ /* Add it to the reusable connection list
+ */
+ cherokee_list_add (LIST(conn), &thread->reuse.conns);
+ thread->reuse.conns_num++;
+
+ return ret_ok;
+}
+
+
+ret_t
+cherokee_thread_get_new_request (cherokee_thread_t *thd,
+ cherokee_request_t **req)
+{
+ /* New obj */
+ if (thread->reuse.reqs_num <= 0) {
+ return cherokee_request_new (req);
+ }
+
+ /* Reuse obj */
+ *req = REQ(thd->reuse.reqs.prev);
+ cherokee_list_del (LIST(*req));
+ INIT_LIST_HEAD (LIST(*req));
+ thread->reuse.reqs_num--;
+
+ return ret_ok;
+}
+
+
+ret_t
+cherokee_thread_recycle_request (cherokee_thread_t *thd,
+ cherokee_connection_t *req)
+{
+ /* Check the max connection reuse number
+ */
+ if (thread->reuse.reqs_num >= THREAD_SRV(thread)->conns_reuse_max) {
return cherokee_request_free (req);
}
/* Add it to the reusable connection list
*/
- cherokee_list_add (LIST(req), &thread->reuse_list);
- thread->reuse_list_num++;
+ cherokee_list_add (LIST(req), &thread->reuse.reqs);
+ thread->reuse.reqs_num++;
return ret_ok;
}
@@ -328,7 +389,7 @@ purge_connection (cherokee_thread_t *thread, cherokee_request_t *req)
/* Add it to the reusable list
*/
- connection_reuse_or_free (thread, req);
+ cherokee_thread_recycle_connection (thread, conn);
}
@@ -454,13 +515,13 @@ send_hardcoded_error (cherokee_socket_t *sock,
static void
process_polling_connections (cherokee_thread_t *thd)
{
- int re;
- ret_t ret;
- cherokee_list_t *tmp, *i;
- cherokee_request_t *req;
+ int re;
+ ret_t ret;
+ cherokee_connection_t *conn;
+ cherokee_list_t *tmp, *i;
list_for_each_safe (i, tmp, LIST(&thd->polling_list)) {
- req = REQ(i);
+ conn = CONN(i);
/* Thread's properties
*/
@@ -1472,38 +1533,25 @@ thread_full_handler (cherokee_thread_t *thd,
static ret_t
-get_new_connection (cherokee_thread_t *thd, cherokee_request_t **req)
+get_new_connection (cherokee_thread_t *thd,
+ cherokee_connection_t **conn)
{
- cherokee_request_t *new_connection;
- cherokee_server_t *server;
- static cuint_t last_conn_id = 0;
+ cherokee_connection_t *new_connection;
+ cherokee_server_t *server = SRV(thd->server);
+ static cuint_t last_conn_id = 0;
- server = SRV(thd->server);
-
- if (cherokee_list_empty (&thd->reuse_list)) {
- ret_t ret;
-
- /* Create new connection object
- */
- ret = cherokee_request_new (&new_connection);
- if (unlikely(ret < ret_ok)) return ret;
- } else {
- /* Reuse an old one
- */
- new_connection = REQ(thd->reuse_list.prev);
- cherokee_list_del (LIST(new_connection));
- thd->reuse_list_num--;
-
- INIT_LIST_HEAD (LIST(new_connection));
+ /* New connection object */
+ ret = cherokee_thread_get_new_connection (thd, &new_connection);
+ if (unlikely(ret < ret_ok)) {
+ return ret;
}
/* Set the basic information to the connection
*/
- new_connection->id = last_conn_id++;
- new_connection->thread = thd;
- new_connection->server = server;
- new_connection->vserver = VSERVER(server->vservers.prev);
-
+ new_connection->id = last_conn_id++;
+ new_connection->thread = thd;
+ new_connection->server = server;
+ new_connection->vserver = VSERVER(server->vservers.prev);
new_connection->traffic_next = cherokee_bogonow_now + DEFAULT_TRAFFIC_UPDATE;
/* Set the default server timeout
@@ -1512,7 +1560,7 @@ get_new_connection (cherokee_thread_t *thd, cherokee_request_t **req)
new_connection->timeout_lapse = server->timeout;
new_connection->timeout_header = &server->timeout_header;
- *req = new_connection;
+ *conn = new_connection;
return ret_ok;
}
@@ -1527,6 +1575,7 @@ accept_new_connection (cherokee_thread_t *thd,
cherokee_request_t *new_conn = NULL;
int new_fd = -1;
cherokee_server_t *srv = THREAD_SRV(thd);
+ cherokee_boolean_t lock_set = false;
/* Check whether there are connections waiting
*/
@@ -1556,21 +1605,21 @@ accept_new_connection (cherokee_thread_t *thd,
goto error;
}
- /* We got a new_conn object, on error we can goto error.
+ /* Set the actual fd info in the connection
*/
ret = cherokee_socket_set_sockaddr (&new_conn->socket, new_fd, &new_sa);
+ if (unlikely(ret < ret_ok)) {
+ LOG_ERROR_S (CHEROKEE_ERROR_THREAD_SET_SOCKADDR);
+ goto error;
+ }
/* It is about to add a new connection to the thread,
* so it MUST adquire the thread ownership
* (do it now to better handle error cases).
*/
+ lock_set = true;
CHEROKEE_MUTEX_LOCK (&thd->ownership);
- if (unlikely(ret < ret_ok)) {
- LOG_ERROR_S (CHEROKEE_ERROR_THREAD_SET_SOCKADDR);
- goto error;
- }
-
/* TLS support, set initial connection phase.
*/
if (bind->socket.is_tls == TLS) {
@@ -1593,7 +1642,6 @@ accept_new_connection (cherokee_thread_t *thd,
/* Lets add the new connection
*/
add_connection (thd, new_conn);
-
thd->conns_num++;
/* Release the thread ownership
@@ -1615,12 +1663,14 @@ accept_new_connection (cherokee_thread_t *thd,
*/
if (new_conn) {
S_SOCKET_FD(new_conn->socket) = -1;
- connection_reuse_or_free (thd, new_conn);
+ cherokee_thread_recycle_connection (thd, new_conn);
}
/* Release the thread ownership
*/
- CHEROKEE_MUTEX_UNLOCK (&thd->ownership);
+ if (lock_set) {
+ CHEROKEE_MUTEX_UNLOCK (&thd->ownership);
+ }
return ret_deny;
}
View
18 cherokee/thread.h
@@ -76,8 +76,17 @@ typedef struct {
cherokee_list_t active_list;
cherokee_list_t polling_list;
- cherokee_list_t reuse_list;
- int reuse_list_num; /* reusable connections objs */
+
+ struct {
+ cherokee_list_t conns;
+ int conns_num;
+ cherokee_list_t reqs;
+ int reqs_num;
+ } reuse;
+
+
+// cherokee_list_t reuse_list;
+// int reuse_list_num; /* reusable connections objs */
cherokee_limiter_t limiter; /* Traffic shaping */
cherokee_boolean_t is_full;
@@ -124,4 +133,9 @@ ret_t cherokee_thread_inject_active_connection (cherokee_thread_t *thd, cherok
ret_t cherokee_thread_close_all_connections (cherokee_thread_t *thd);
ret_t cherokee_thread_close_polling_connections (cherokee_thread_t *thd, int fd, cuint_t *num);
+ret_t cherokee_thread_get_new_request (cherokee_thread_t *thd, cherokee_request_t **req);
+ret_t cherokee_thread_recycle_request (cherokee_thread_t *thd, cherokee_request_t *req);
+ret_t cherokee_thread_get_new_connection (cherokee_thread_t *thd, cherokee_connection_t **conn);
+ret_t cherokee_thread_recycle_connection (cherokee_thread_t *thd, cherokee_connection_t *conn);
+
#endif /* CHEROKEE_THREAD_H */
View
4 cherokee/trace.c
@@ -149,7 +149,7 @@ cherokee_trace_do_trace (const char *entry, const char *file, int line, const ch
char *lentry;
char *lentry_end;
va_list args;
- cherokee_request_t *conn;
+ cherokee_connection_t *conn;
cherokee_buffer_t *trace_modules = &trace.modules;
cherokee_boolean_t do_log = false;
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
/* Check the connection source, if possible
*/
if (trace.from_filter != NULL) {
- conn = REQ (CHEROKEE_THREAD_PROP_GET (thread_connection_ptr));
+ conn = CONN(CHEROKEE_THREAD_PROP_GET (thread_connection_ptr));
/* No conn, no trace entry
*/
Please sign in to comment.
Something went wrong with that request. Please try again.