Changeset 764
- Timestamp:
- 05/29/07 22:42:50 (2 years ago)
- Files:
-
- cherokee/trunk/ChangeLog (modified) (1 diff)
- cherokee/trunk/cherokee/handler_server_info.c (modified) (1 diff)
- cherokee/trunk/cherokee/macros.h (modified) (3 diffs)
- cherokee/trunk/cherokee/server-protected.h (modified) (1 diff)
- cherokee/trunk/cherokee/server.c (modified) (17 diffs)
- cherokee/trunk/cherokee/server.h (modified) (1 diff)
- cherokee/trunk/cherokee/socket.c (modified) (2 diffs)
- cherokee/trunk/cherokee/thread.c (modified) (36 diffs)
- cherokee/trunk/cherokee/thread.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
cherokee/trunk/ChangeLog
r745 r764 1 2007-05-22 A.D.F <adefacc@tin.it> 2 3 * cherokee/macros.h: 4 - use saner values in defines; 5 6 * cherokee/thread.h: 7 - added new fields to struct cherokee_thread_t 8 in order to keep exact numbers of connections per thread; 9 - added new parameter to cherokee_thread_new(); 10 - added new functions (for SINGLE THREAD): 11 cherokee_thread_accept_on(); 12 cherokee_thread_accept_off(); 13 14 * cherokee/thread.c: 15 - cherokee_thread_new(): 16 - added new test conditions, calculation 17 of conns_accept limit, etc.; 18 19 -cherokee_thread_step_SINGLE_THREAD() and 20 cherokee_thread_step_MULTI_THREAD(): 21 - added code to disable accepting new connections 22 when there are no more free slots (100% full) 23 and to reenable accepting new connections 24 when the number of connections is below 95% - 99%; 25 - removed calls to cherokee_fdpoll_is_full() 26 (by using #ifdef 0 ... #endif) because now 27 we test the number of open connections and 28 because cherokee_fdpoll_add() tests upper limit 29 and returns error if it is reached; 30 - now cherokee_fdpoll_watch() is always called 31 in order to avoid a busy wait loop when 32 there are no more free connection slots. 33 34 - process_active_connections(): 35 - added tests on total server number of open connections, 36 if the number is > max_keepalive limit 37 then keepalive is disabled until that number 38 goes down (high water principle); 39 this is done because if the server reaches the max number 40 of connections and open connections are kept alive, 41 then the server is no more able to accept new connections 42 (for minutes or even for hours); 43 44 - other minor space cleanups. 45 46 * cherokee/server.c: 47 - removed local define MIN_SPARE_FDS; 48 - added local define MIN_LISTEN_FDS; 49 - better distribution of spare fds for each thread; 50 - forced a minimum number of fds per thread by 51 lowering the number of requested threads to reach 52 a minimum number as set in macros.h; 53 - now if srv->thread_num == 1 then 54 cherokee_thread_step_SINGLE_THREAD() is called 55 even if the server has been compiled with HAVE_PTHREAD 1 56 (this speedup connection handling by 2% - 3%); 57 - set max_keepalive limit to 93% of max. number of connections 58 so that server tries to close connections before 59 it reaches 100% of opened connections (this should prevent 60 the scenario where server is not able to accept new connections 61 for minutes or even hours); 62 - print_banner(), 63 added new information about max. server connections, etc.; 64 - minor space cleanups; 65 66 * cherokee/handler_server_info.c: 67 - added new info about current number of server connections 68 (which is different from the number of active connections); 69 70 * cherokee/socket.c: 71 - cherokee_socket_accept_fd(), 72 now ECONNABORTED returns ret_deny, 73 so callers should call this function again (retry loop) 74 instead of giving up and execute the process phase 75 (this should speed up a lot the server when clients 76 try to connect and abort connections before they 77 are accepted by server). 78 1 79 2007-05-06 Alvaro Lopez Ortega <alvaro@alobbs.com> 2 80 cherokee/trunk/cherokee/handler_server_info.c
r655 r764 254 254 build_connections_table_content (cherokee_buffer_t *buf, cherokee_server_t *srv) 255 255 { 256 cuint_t active; 257 cuint_t reusable; 258 256 cuint_t conns_num = 0; 257 cuint_t active = 0; 258 cuint_t reusable = 0; 259 260 cherokee_server_get_conns_num (srv, &conns_num); 259 261 cherokee_server_get_active_conns (srv, &active); 260 262 cherokee_server_get_reusable_conns (srv, &reusable); 261 263 264 table_add_row_int (buf, "Open connections", conns_num); 262 265 table_add_row_int (buf, "Active connections", active); 263 266 table_add_row_int (buf, "Reusable connections", reusable); cherokee/trunk/cherokee/macros.h
r731 r764 102 102 #define MIN_SYSTEM_FD_NUM 20 /* range: 16 - 64 */ 103 103 #define MIN_SPARE_FDS 10 /* range: 8 - 20 */ 104 #define MIN_MAX_FDS 4 /* range: 4 ... 32000 */105 #define MIN_THR_FDS 4 /* range: 4 ... 32000 */104 #define MIN_MAX_FDS 8 /* range: 8 ... 65000 */ 105 #define MIN_THR_FDS 8 /* range: 8 ... 65000 */ 106 106 107 107 #if (MIN_SYSTEM_FD_NUM < 16) … … 111 111 #error MIN_SPARE_FDS too low, < 8 ! 112 112 #endif 113 #if (MIN_MAX_FDS < 4)114 #error MIN_MAX_FDS too low, < 4!115 #endif 116 #if (MIN_THR_FDS < 4)117 #error MIN_THR_FDS too low, < 4!113 #if (MIN_MAX_FDS < 8) 114 #error MIN_MAX_FDS too low, < 8 ! 115 #endif 116 #if (MIN_THR_FDS < 8) 117 #error MIN_THR_FDS too low, < 8 ! 118 118 #endif 119 119 #if (MIN_THR_FDS > MIN_MAX_FDS) 120 120 #error MIN_THR_FDS too high, > MIN_MAX_FDS ! 121 121 #endif 122 #if (( (MIN_SYSTEM_FD_NUM - MIN_SPARE_FDS) / 2) < MIN_MAX_FDS)122 #if ((MIN_SYSTEM_FD_NUM - MIN_SPARE_FDS) < MIN_MAX_FDS) 123 123 #error MIN_SYSTEM_FD_NUM too low or MIN_SPARE FDS too high ! 124 124 #endif … … 199 199 type * obj = (type *) malloc(sizeof(type)); \ 200 200 return_if_fail (obj != NULL, ret_nomem) 201 202 /* We assume to have a C ANSI free(). 203 */ 204 #define CHEROKEE_FREE(obj) \ 205 do { \ 206 free (obj); \ 207 (obj) = NULL; \ 208 } while (0) 201 209 202 210 #define CHEROKEE_TEMP(obj, size) \ cherokee/trunk/cherokee/server-protected.h
r597 r764 122 122 cherokee_poll_type_t fdpoll_method; 123 123 124 /* Connection related. 125 */ 126 int max_conns; 127 int max_keepalive_conns; 128 124 129 /* Networking config 125 130 */ cherokee/trunk/cherokee/server.c
r731 r764 91 91 #define ENTRIES "core,server" 92 92 93 /* Number of spare fds, used for stdin, stdout, stderr, etc. 94 * Set it between 5 and 20. 93 /* Number of listen fds (HTTP + HTTPS) 95 94 */ 96 #define M IN_SPARE_FDS 1095 #define MAX_LISTEN_FDS 2 97 96 98 97 ret_t … … 156 155 n->max_fds = -1; 157 156 n->system_fd_limit = -1; 157 n->max_conns = 0; 158 n->max_keepalive_conns = 0; 158 159 n->max_conn_reuse = -1; 159 160 … … 397 398 int error; 398 399 399 /* /\* Get user information */ 400 /* *\/ */ 401 /* ent = getpwuid (srv->user); */ 402 /* if (ent == NULL) { */ 403 /* PRINT_ERROR ("Can't get username for UID %d\n", srv->user); */ 404 /* return ret_error; */ 405 /* } */ 406 400 #if 0 401 /* Get user information 402 */ 403 ent = getpwuid (srv->user); 404 if (ent == NULL) { 405 PRINT_ERROR ("Can't get username for UID %d\n", srv->user); 406 return ret_error; 407 } 408 #endif 409 407 410 /* Reset `groups' attributes. 408 411 */ … … 641 644 /* File descriptor limit 642 645 */ 643 cherokee_buffer_add_va (&n, ", %d fds system limit, max. %d connections", srv->system_fd_limit, srv->max_ fds);646 cherokee_buffer_add_va (&n, ", %d fds system limit, max. %d connections", srv->system_fd_limit, srv->max_conns); 644 647 645 648 /* Threading stuff … … 647 650 if (srv->thread_num <= 1) { 648 651 cherokee_buffer_add_str (&n, ", single thread"); 652 cherokee_buffer_add_va (&n, ", %d fds per thread", srv->max_fds); 649 653 } else { 650 654 cherokee_buffer_add_va (&n, ", %d threads", srv->thread_num); … … 738 742 { 739 743 ret_t ret; 740 int i, fds_per_thread, fds_per_thread1 ;744 int i, fds_per_thread, fds_per_thread1, conns_per_thread; 741 745 #ifdef HAVE_PTHREAD 742 746 int thr_fds, spare_fds; 743 747 #endif 748 749 /* Reset max. conns value 750 */ 751 srv->max_conns = 0; 752 srv->max_keepalive_conns = 0; 744 753 745 754 /* Verify max_fds value … … 759 768 srv->thread_num = 1; 760 769 #endif 761 fds_per_thread = srv->max_fds / srv->thread_num; 770 771 fds_per_thread1 = fds_per_thread = srv->max_fds / srv->thread_num; 772 762 773 #ifdef HAVE_PTHREAD 763 774 thr_fds = fds_per_thread * srv->thread_num; 764 775 spare_fds = srv->max_fds - thr_fds; 765 #endif 766 fds_per_thread1 = fds_per_thread; 767 if (spare_fds > 0) { 768 spare_fds--; 769 fds_per_thread1++; 770 } 776 777 /* Add a couple more fds to this thread, 778 * this is useful if we are using a huge number of threads 779 * (i.e. 1000 or more) and we don't want to leave lots of 780 * unused fds. 781 */ 782 if (spare_fds >= 2) { 783 spare_fds -= 2; 784 fds_per_thread1 += 2; 785 } 786 #else 787 fds_per_thread1 -= MAX_LISTEN_FDS; 788 #endif 789 /* Max. number of connections is halved because opening a new file 790 * or using a socket to connect to an external helper 791 * might be required to satisfy a request coming from an accepted 792 * and thus already open connection. 793 */ 794 conns_per_thread = fds_per_thread1 / 2; 795 srv->max_conns += conns_per_thread; 796 fds_per_thread1 += MAX_LISTEN_FDS; 771 797 772 798 /* Create the main thread 773 799 */ 774 800 ret = cherokee_thread_new (&srv->main_thread, srv, thread_sync, 775 srv->fdpoll_method, srv->system_fd_limit, fds_per_thread1); 801 srv->fdpoll_method, srv->system_fd_limit, 802 fds_per_thread1, conns_per_thread); 776 803 if (unlikely(ret < ret_ok)) 777 804 return ret; … … 780 807 * add the server socket to the fdpoll of the sync thread 781 808 */ 782 #ifndef HAVE_PTHREAD 783 ret = cherokee_fdpoll_add (srv->main_thread->fdpoll, S_SOCKET_FD(srv->socket), FDPOLL_MODE_READ); 784 if (unlikely(ret < ret_ok)) return ret; 785 786 if (srv->tls_enabled) { 787 ret = cherokee_fdpoll_add (srv->main_thread->fdpoll, S_SOCKET_FD(srv->socket_tls), FDPOLL_MODE_READ); 788 if (unlikely(ret < ret_ok)) return ret; 789 } 790 #endif 809 if (srv->thread_num == 1) { 810 ret = cherokee_thread_accept_on (srv->main_thread); 811 if (unlikely(ret < ret_ok)) 812 return ret; 813 } 791 814 792 815 /* If Cherokee has been compiled in multi-thread mode, … … 797 820 cherokee_thread_t *thread; 798 821 799 /* Add one more fdto this thread,822 /* Add a couple more fds to this thread, 800 823 * this is useful if we are using a huge number of threads 801 824 * (i.e. 1000 or more) and we don't want to leave lots of … … 803 826 */ 804 827 fds_per_thread1 = fds_per_thread; 805 if (spare_fds > 0) {806 spare_fds --;807 fds_per_thread1 ++;828 if (spare_fds >= 2) { 829 spare_fds -= 2; 830 fds_per_thread1 += 2; 808 831 } 832 conns_per_thread = fds_per_thread1 / 2; 833 srv->max_conns += conns_per_thread; 834 fds_per_thread1 += MAX_LISTEN_FDS; 809 835 810 836 ret = cherokee_thread_new (&thread, srv, thread_async, 811 srv->fdpoll_method, srv->system_fd_limit, fds_per_thread1); 837 srv->fdpoll_method, srv->system_fd_limit, 838 fds_per_thread1, conns_per_thread); 812 839 if (unlikely(ret < ret_ok)) 813 840 return ret; … … 819 846 #endif 820 847 848 /* Set keepalive limit for open connections. 849 * NOTE: this limit has to be lower (93%) 850 * than conns_accept limit (95% - 99%) used for threads. 851 */ 852 srv->max_keepalive_conns = srv->max_conns - (srv->max_conns / 16); 853 if (srv->max_keepalive_conns + 6 > srv->max_conns) 854 srv->max_keepalive_conns = srv->max_conns - 6; 855 856 /* OK, return. 857 */ 821 858 return ret_ok; 822 859 } … … 977 1014 * (i.e. FastCGI, SCGI, mirror, etc.). 978 1015 */ 979 srv->max_fds = (srv->system_fd_limit - MIN_SPARE_FDS) / 2;1016 srv->max_fds = (srv->system_fd_limit - MIN_SPARE_FDS); 980 1017 if (srv->max_fds < MIN_MAX_FDS) { 981 PRINT_ERROR("Number of max. connectiontoo low %d < %d !\n", srv->max_fds, MIN_MAX_FDS);1018 PRINT_ERROR("Number of max. fds too low %d < %d !\n", srv->max_fds, MIN_MAX_FDS); 982 1019 return ret_error; 983 1020 } … … 1220 1257 ret_t ret; 1221 1258 1222 /* Get the time1259 /* Get the server time. 1223 1260 */ 1224 1261 update_bogo_now (srv); 1225 ret = cherokee_thread_step (srv->main_thread, true); 1226 1262 1263 /* Execute thread step. 1264 */ 1265 #ifndef HAVE_PTHREAD 1266 ret = cherokee_thread_step_SINGLE_THREAD (srv->main_thread); 1267 #else 1268 if (srv->thread_num == 1) 1269 ret = cherokee_thread_step_SINGLE_THREAD (srv->main_thread); 1270 else 1271 ret = cherokee_thread_step_MULTI_THREAD (srv->main_thread, true); 1272 #endif 1227 1273 /* Logger flush 1228 1274 */ … … 1244 1290 */ 1245 1291 if (unlikely (srv->wanna_reinit)) { 1246 cherokee_server_reinit (srv); 1292 ret = cherokee_server_reinit (srv); 1293 if (ret != ret_ok) 1294 return ret; 1247 1295 } 1248 1296 … … 1252 1300 return ret_eagain; 1253 1301 } 1302 1254 1303 1255 1304 static ret_t … … 1681 1730 1682 1731 ret_t 1732 cherokee_server_get_conns_num (cherokee_server_t *srv, cuint_t *num) 1733 { 1734 cuint_t conns_num = 0; 1735 cherokee_list_t *thread; 1736 1737 /* Open HTTP connections number 1738 */ 1739 list_for_each (thread, &srv->thread_list) { 1740 conns_num += THREAD(thread)->conns_num; 1741 } 1742 1743 conns_num += srv->main_thread->conns_num; 1744 1745 /* Return out parameters 1746 */ 1747 *num = conns_num; 1748 1749 return ret_ok; 1750 } 1751 1752 1753 ret_t 1683 1754 cherokee_server_get_active_conns (cherokee_server_t *srv, cuint_t *num) 1684 1755 { … … 1711 1782 */ 1712 1783 list_for_each (thread, &srv->thread_list) { 1713 list_for_each (i, &THREAD(thread)->reuse_list) { 1714 reusable++; 1715 } 1716 } 1717 list_for_each (i, &THREAD(srv->main_thread)->reuse_list) { 1718 reusable++; 1719 } 1784 reusable += THREAD(thread)->reuse_list_num; 1785 } 1786 reusable += srv->main_thread->reuse_list_num; 1720 1787 1721 1788 /* Return out parameters cherokee/trunk/cherokee/server.h
r690 r764 57 57 ret_t cherokee_server_write_pidfile (cherokee_server_t *srv); 58 58 59 ret_t cherokee_server_get_conns_num (cherokee_server_t *srv, cuint_t *num); 59 60 ret_t cherokee_server_get_active_conns (cherokee_server_t *srv, cuint_t *num); 60 61 ret_t cherokee_server_get_reusable_conns (cherokee_server_t *srv, cuint_t *num); cherokee/trunk/cherokee/socket.c
r706 r764 632 632 new_socket = accept (server_socket, &sa->sa, &len); 633 633 if (new_socket < 0) { 634 return ret_error; 634 int err = SOCK_ERRNO(); 635 /* Caller has to retry the call on ret_deny. 636 */ 637 switch (err) { 638 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN) 639 case EWOULDBLOCK: 640 #endif 641 case EAGAIN: 642 case EINTR: 643 /* no data or error. 644 */ 645 return ret_eagain; 646 #ifdef ECONNABORTED 647 case ECONNABORTED: 648 /* aborted connection, retry immediately 649 */ 650 return ret_deny; 651 #endif 652 default: 653 /* error 654 */ 655 return ret_error; 656 } 657 /* NOTREACHED */ 635 658 } 636 659 637 660 /* Disable Nagle's algorithm for this connection 638 661 * so that there is no delay involved when sending data … … 1181 1204 if (re < 0) { 1182 1205 int err = SOCK_ERRNO(); 1183 1206 1184 1207 switch (err) { 1185 1208 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN) cherokee/trunk/cherokee/thread.c
r732 r764 142 142 */ 143 143 while (thread->exit == false) { 144 cherokee_thread_step (thread, false);144 cherokee_thread_step_MULTI_THREAD (thread, false); 145 145 } 146 146 … … 173 173 174 174 ret_t 175 cherokee_thread_new (cherokee_thread_t **thd, void *server, cherokee_thread_type_t type, 176 cherokee_poll_type_t fdpoll_type, int system_fd_num, int fd_num) 175 cherokee_thread_new ( 176 cherokee_thread_t **thd, void *server, cherokee_thread_type_t type, 177 cherokee_poll_type_t fdpoll_type, int system_fd_num, int fd_num, 178 int conns_max) 177 179 { 178 180 ret_t ret; 181 int conns_accept; 179 182 cherokee_server_t *srv = SRV(server); 180 CHEROKEE_ NEW_STRUCT (n, thread);183 CHEROKEE_CNEW_STRUCT (1, n, thread); 181 184 182 185 /* Init … … 192 195 ret = cherokee_fdpoll_new (&n->fdpoll, fdpoll_type, system_fd_num, fd_num); 193 196 194 if (unlikely (ret != ret_ok)) 197 if (unlikely (ret != ret_ok)) { 198 CHEROKEE_FREE(n); 195 199 return ret; 196 200 } 201 202 n->exit = false; 203 n->is_accepting_conns= false; 204 205 if (fd_num < conns_max || 206 conns_max < 4) { 207 cherokee_fdpoll_free (n->fdpoll); 208 CHEROKEE_FREE(n); 209 return ret_error; 210 } 211 212 /* Set upper accept limit to 95% - 99% of conns_max. 213 */ 214 if (conns_max > 40) { 215 conns_accept = conns_max - (conns_max / 20); 216 217 if (srv->thread_num > 16) { 218 if (conns_accept < (conns_max - 4)) 219 conns_accept = (conns_max - 4); 220 } else 221 if (srv->thread_num > 4) { 222 if (conns_accept < (conns_max - 8)) 223 conns_accept = (conns_max - 8); 224 } else 225 if (srv->thread_num > 1) { 226 if (conns_accept < (conns_max - 10)) 227 conns_accept = (conns_max - 10); 228 } else { 229 if (conns_accept < (conns_max - 50)) 230 conns_accept = (conns_max - 50); 231 } 232 if (conns_accept == conns_max) 233 conns_accept -= 2; 234 } else { 235 conns_accept = conns_max - 2; 236 } 237 238 n->conns_num = 0; 239 n->conns_max = conns_max; 240 n->conns_accept = conns_accept; 241 197 242 n->active_list_num = 0; 198 243 n->polling_list_num = 0; 199 244 n->reuse_list_num = 0; 200 245 201 n->exit = false;202 246 n->pending_conns_num = 0; 247 203 248 n->server = server; 204 249 n->thread_type = type; … … 214 259 215 260 /* Temporary buffer used by utility functions 216 */261 */ 217 262 cherokee_buffer_init (&n->tmp_buf1); 218 263 cherokee_buffer_init (&n->tmp_buf2); … … 257 302 /* Finally, create the system thread 258 303 */ 259 pthread_create (&n->thread, &attr, thread_routine, n); 304 if (pthread_create (&n->thread, &attr, thread_routine, n) != 0) { 305 cherokee_thread_free (n); 306 return ret_error; 307 } 260 308 #else 261 309 SHOULDNT_HAPPEN; … … 346 394 */ 347 395 cherokee_connection_mrproper (conn); 396 397 if (thread->conns_num > 0) 398 thread->conns_num--; 348 399 349 400 /* Add it to the reusable list … … 576 627 { 577 628 ret_t ret; 629 cuint_t srv_conns_num = 0; 578 630 off_t len; 579 631 cherokee_list_t *i, *tmp; … … 581 633 cherokee_server_t *srv = SRV(thd->server); 582 634 635 /* Get total connection count. 636 */ 637 cherokee_server_get_conns_num (srv, &srv_conns_num); 638 583 639 /* Process active connections 584 640 */ … … 591 647 */ 592 648 if (conn->timeout < thd->bogo_now) { 649 srv_conns_num--; 593 650 purge_closed_connection (thd, conn); 594 651 continue; … … 617 674 switch (num) { 618 675 case -1: 676 srv_conns_num--; 619 677 purge_closed_connection(thd, conn); 620 678 continue; … … 643 701 case ret_eof: 644 702 case ret_error: 703 srv_conns_num--; 645 704 purge_closed_connection (thd, conn); 646 705 continue; … … 648 707 default: 649 708 RET_UNKNOWN(ret); 709 srv_conns_num--; 650 710 purge_closed_connection (thd, conn); 651 711 continue; … … 674 734 675 735 case ret_error: 736 srv_conns_num--; 676 737 purge_closed_connection (thd, conn); 677 738 continue; … … 679 740 default: 680 741 RET_UNKNOWN(ret); 742 srv_conns_num--; 681 743 purge_closed_connection (thd, conn); 682 744 break; … … 711 773 712 774 case ret_error: 775 srv_conns_num--; 713 776 purge_closed_connection (thd, conn); 714 777 continue; … … 716 779 default: 717 780 RET_UNKNOWN(ret); 781 srv_conns_num--; 718 782 purge_closed_connection (thd, conn); 719 783 continue; … … 740 804 break; 741 805 case ret_error: 806 srv_conns_num--; 742 807 purge_closed_connection (thd, conn); 743 808 continue; 744 809 default: 745 810 RET_UNKNOWN(ret); 811 srv_conns_num--; 746 812 purge_closed_connection (thd, conn); 747 813 continue; … … 812 878 case ret_eagain: 813 879 continue; 814 880 815 881 default: 816 882 cherokee_connection_setup_error_handler (conn); … … 1013 1079 /* Server's "Keep-Alive" could be turned "Off" 1014 1080 */ 1015 if (srv->keepalive == false) { 1081 if (conn->keepalive != 0 && 1082 (srv->keepalive == false || 1083 srv_conns_num >= (cuint_t) srv->max_keepalive_conns)) { 1016 1084 conn->keepalive = 0; 1017 1085 } … … 1129 1197 case ret_eof: 1130 1198 case ret_error: 1199 srv_conns_num--; 1131 1200 purge_closed_connection (thd, conn); 1132 1201 continue; … … 1192 1261 case ret_error: 1193 1262 default: 1263 srv_conns_num--; 1194 1264 purge_closed_connection (thd, conn); 1195 1265 /* purge_maybe_lingering (thd, conn); */ … … 1318 1388 cherokee_connection_t *new_conn; 1319 1389 1320 /* Return if there're no new connections1321 */ 1322 if ( cherokee_fdpoll_check (thd->fdpoll, srv_socket, FDPOLL_MODE_READ) == 0) {1390 /* Return if there're too many or no new connections 1391 */ 1392 if (thd->conns_num >= thd->conns_max) 1323 1393 return 0; 1394 1395 if (cherokee_fdpoll_check (thd->fdpoll, srv_socket, FDPOLL_MODE_READ) <= 0) { 1396 return 0; 1324 1397 } 1325 1398 1326 1399 /* Try to get a new connection 1327 1400 */ 1328 ret = cherokee_socket_accept_fd (srv_socket, &new_fd, &new_sa); 1329 if (ret < ret_ok) return 0; 1401 do { 1402 ret = cherokee_socket_accept_fd (srv_socket, &new_fd, &new_sa); 1403 } while (ret == ret_deny); 1404 1405 if (ret != ret_ok) 1406 return 0; 1330 1407 1331 1408 /* We got the new socket, now set it up in a new connection object … … 1369 1446 } 1370 1447 1448 thd->conns_num++; 1449 1371 1450 TRACE (ENTRIES, "new conn %p, fd %d\n", new_conn, new_fd); 1372 1451 … … 1392 1471 /* If it is full, do not accept more! 1393 1472 */ 1473 if (unlikely (thd->conns_num >= thd->conns_max)) 1474 return 0; 1475 1476 #if 0 1394 1477 if (unlikely (cherokee_fdpoll_is_full(thd->fdpoll))) { 1395 1478 return 0; 1396 1479 } 1480 #endif 1397 1481 1398 1482 /* Got new connection … … 1424 1508 1425 1509 ret_t 1426 cherokee_thread_step_SINGLE_THREAD (cherokee_thread_t *thd, cherokee_boolean_t dont_block) 1510 cherokee_thread_accept_on (cherokee_thread_t *thd) 1511 { 1512 ret_t ret; 1513 1514 if (thd->is_accepting_conns) 1515 return ret_ok; 1516 1517 /* Add server sockets to polling set. 1518 */ 1519 ret = cherokee_fdpoll_add (thd->fdpoll, 1520 S_SOCKET_FD(THREAD_SRV(thd)->socket), FDPOLL_MODE_READ); 1521 if (unlikely(ret < ret_ok)) 1522 return ret; 1523 1524 #ifdef HAVE_TLS 1525 if (THREAD_SRV(thd)->tls_enabled) { 1526 ret = cherokee_fdpoll_add (thd->fdpoll, 1527 S_SOCKET_FD(THREAD_SRV(thd)->socket_tls), FDPOLL_MODE_READ); 1528 if (unlikely(ret < ret_ok)) 1529 return ret; 1530 } 1531 #endif /* HAVE_TLS */ 1532 1533 /* Set flag and return. 1534 */ 1535 thd->is_accepting_conns = true; 1536 1537 return ret_ok; 1538 } 1539 1540 1541 ret_t 1542 cherokee_thread_accept_off (cherokee_thread_t *thd) 1543 { 1544 ret_t ret; 1545 1546 if (!thd->is_accepting_conns) 1547 return ret_ok; 1548 1549 /* Remove server sockets from polling set. 1550 */ 1551 ret = cherokee_fdpoll_del (thd->fdpoll, 1552 S_SOCKET_FD(THREAD_SRV(thd)->socket)); 1553 if (unlikely(ret < ret_ok)) 1554 return ret; 1555 1556 #ifdef HAVE_TLS 1557 if (THREAD_SRV(thd)->tls_enabled) { 1558 ret = cherokee_fdpoll_del (thd->fdpoll, 1559 S_SOCKET_FD(THREAD_SRV(thd)->socket_tls)); 1560 if (unlikely(ret < ret_ok)) 1561 return ret; 1562 } 1563 #endif /* HAVE_TLS */ 1564 1565 /* Set flag and return. 1566 */ 1567 thd->is_accepting_conns = false; 1568 1569 return ret_ok; 1570 } 1571 1572 1573 ret_t 1574 cherokee_thread_step_SINGLE_THREAD (cherokee_thread_t *thd) 1427 1575 { 1428 1576 int re; 1577 ret_t ret; 1429 1578 cherokee_server_t *srv = THREAD_SRV(thd); 1430 1579 int fdwatch_msecs = srv->fdwatch_msecs; … … 1434 1583 try_to_update_bogo_now (thd); 1435 1584 1436 /* Reset the server socket 1585 /* Reset the server socket. 1437 1586 * cherokee_fdpoll_reset (thd->fdpoll, S_SOCKET_FD(srv->socket)); 1438 1587 */ 1439 1588 1440 1589 /* If the thread is full of connections, it should not 1441 * get new connections 1442 */ 1590 * get new connections. 1591 */ 1592 if (thd->conns_num >= thd->conns_max) { 1593 if (thd->is_accepting_conns) 1594 ret = cherokee_thread_accept_off (thd); 1595 } 1596 else 1597 if (thd->conns_num < thd->conns_accept) { 1598 if (!thd->is_accepting_conns) 1599 ret = cherokee_thread_accept_on (thd); 1600 } 1601 1602 #if 0 1443 1603 if (unlikely (cherokee_fdpoll_is_full (thd->fdpoll))) { 1444 1604 goto out; 1445 1605 } 1606 #endif 1446 1607 1447 1608 /* If thread has pending connections, it should do a … … 1454 1615 1455 1616 re = cherokee_fdpoll_watch (thd->fdpoll, fdwatch_msecs); 1456 if (re <= 0) goto out; 1617 if (re <= 0) 1618 goto out; 1457 1619 1458 1620 do { … … 1460 1622 } while (__should_accept_more_from_server (thd, re)); 1461 1623 1624 #ifdef HAVE_TLS 1462 1625 if (srv->tls_enabled) { 1463 1626 do { … … 1465 1628 } while (__should_accept_more_from_server (thd, re)); 1466 1629 } 1630 #endif /* HAVE_TLS */ 1467 1631 1468 1632 out: … … 1485 1649 ret_t ret; 1486 1650 1651 /* In this case, thd->is_accepting_conns is alwasy true. 1652 */ 1487 1653 CHEROKEE_MUTEX_LOCK (mutex); 1488 1654 … … 1523 1689 int re; 1524 1690 ret_t ret; 1525 int unlocked ;1691 int unlocked = 1; 1526 1692