Changeset 1759

Show
Ignore:
Timestamp:
08/07/08 13:38:21 (5 months ago)
Author:
alo
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • cherokee/trunk/ChangeLog

    r1754 r1759  
     12008-08-07  Alvaro Lopez Ortega  <alvaro@alobbs.com> 
     2 
     3        * cherokee/macros.h, cherokee/socket.c, cherokee/thread.c, 
     4        cherokee/thread.h, cherokee/util.c, cherokee/util.h, 
     5        cherokee/fdpoll-port.c, cherokee/server.c: This patch makes the 
     6        server to reply a quickly built 503 error page when it is 
     7        overloaded. This is a very special case because the page is not 
     8        sent by using the server infrastructure but by punching a 
     9        pre-built string into the socket and closing it right after it. 
     10        Additionally, the patch also do some clean-up along the way. 
     11 
    1122008-08-06  Alvaro Lopez Ortega  <alvaro@alobbs.com> 
    213 
  • cherokee/trunk/cherokee/fdpoll-port.c

    r1439 r1759  
    236236_reset (cherokee_fdpoll_port_t *fdp, int fd) 
    237237{ 
     238        UNUSED (fdp); 
     239        UNUSED (fd); 
     240 
    238241        return ret_ok; 
    239242} 
  • cherokee/trunk/cherokee/macros.h

    r1674 r1759  
    164164 
    165165#define SHOULDNT_HAPPEN \ 
    166         do { fprintf (stderr, "file %s:%d (%s): this shouldn't happen\n",  \ 
     166        do { fprintf (stderr, "file %s:%d (%s): this should not happen\n",  \ 
    167167                      __FILE__, __LINE__, __cherokee_func__);               \ 
    168168        } while (0) 
  • cherokee/trunk/cherokee/server.c

    r1754 r1759  
    763763         * and thus already open connection. 
    764764         */ 
    765         conns_per_thread = fds_per_thread1 / 2; 
    766         srv->conns_max += conns_per_thread; 
    767         fds_per_thread1 += MAX_LISTEN_FDS; 
     765        conns_per_thread = fds_per_thread1 / 2; 
     766        srv->conns_max   += conns_per_thread; 
     767        fds_per_thread1 += MAX_LISTEN_FDS; 
    768768 
    769769        /* Set mean fds per thread. 
     
    785785         */ 
    786786        if (srv->thread_num == 1) { 
    787                 ret = cherokee_thread_accept_on (srv->main_thread); 
    788                 if (unlikely(ret < ret_ok)) { 
    789                         PRINT_ERROR("cherokee_thread_accept_on failed %d\n", ret); 
     787                ret = cherokee_fdpoll_add (srv->main_thread->fdpoll, S_SOCKET_FD(srv->socket), 0); 
     788                if (ret < ret_ok) 
    790789                        return ret; 
     790 
     791                if (srv->tls_enabled) { 
     792                        ret = cherokee_fdpoll_add (srv->main_thread->fdpoll, S_SOCKET_FD(srv->socket_tls), 0); 
     793                        if (ret < ret_ok) 
     794                                return ret; 
    791795                } 
    792796        } 
     
    809813                        fds_per_thread1 += 2; 
    810814                } 
    811                 conns_per_thread = fds_per_thread1 / 2; 
    812                 srv->conns_max += conns_per_thread; 
    813                 fds_per_thread1 += MAX_LISTEN_FDS; 
     815                conns_per_thread = fds_per_thread1 / 2; 
     816                srv->conns_max   += conns_per_thread; 
     817                fds_per_thread1 += MAX_LISTEN_FDS; 
    814818 
    815819                /* NOTE: mean fds per thread has already been set above. 
     
    840844                srv->conns_keepalive_max = srv->conns_max - 6; 
    841845 
    842         /* OK, return. 
    843          */ 
    844846        return ret_ok; 
    845847} 
  • cherokee/trunk/cherokee/socket.c

    r1754 r1759  
    501501        ret_t ret; 
    502502 
     503        /* Sanity check 
     504         */ 
    503505        if (socket->socket < 0) { 
    504506                return ret_error; 
    505507        } 
    506508 
     509        /* SSL/TLS shutdown 
     510         */ 
    507511#ifdef HAVE_TLS 
    508512        if (socket->is_tls == TLS && socket->session != NULL) { 
    509 #if   defined (HAVE_GNUTLS) 
     513# ifdef HAVE_GNUTLS 
    510514                gnutls_bye (socket->session, GNUTLS_SHUT_WR); 
    511515                gnutls_deinit (socket->session); 
    512516                socket->session = NULL; 
    513517 
    514 #elif defined (HAVE_OPENSSL) 
     518# elif defined(HAVE_OPENSSL) 
    515519                SSL_shutdown (socket->session); 
    516520 
    517 #endif 
    518         } 
    519 #endif  /* HAVE_TLS */ 
    520  
    521         ret = cherokee_close_fd (socket->socket); 
    522  
     521# endif 
     522        } 
     523#endif 
     524         
     525        /* Close the socket 
     526         */ 
     527#ifdef _WIN32 
     528        re = closesocket (socket->socket); 
     529#else 
     530        ret = cherokee_fd_close (socket->socket); 
     531#endif 
     532 
     533        /* Clean up 
     534         */ 
    523535        TRACE (ENTRIES",close", "fd=%d is_tls=%d re=%d\n",  
    524536               socket->socket, socket->is_tls, (int) ret); 
     
    607619        switch (SOCKET_AF(socket)) { 
    608620        case AF_INET: 
    609 #if defined(HAVE_INET_PTON) 
     621#ifdef HAVE_INET_PTON 
    610622                re = inet_pton (AF_INET, host->buf, &SOCKET_SIN_ADDR(socket)); 
    611623                if (re <= 0) return ret_error; 
     
    643655        ret = cherokee_socket_set_sockaddr (socket, fd, &sa); 
    644656        if (unlikely(ret < ret_ok)) { 
    645                 cherokee_close_fd (fd); 
     657                cherokee_fd_close (fd); 
    646658                SOCKET_FD(socket) = -1; 
    647659                return ret; 
     
    692704         */ 
    693705        len = sizeof (cherokee_sockaddr_t); 
     706 
    694707        new_socket = accept (server_socket, &sa->sa, &len); 
    695708        if (new_socket < 0) { 
  • cherokee/trunk/cherokee/thread.c

    r1754 r1759  
    156156        n->reuse_list_num     = 0; 
    157157        n->pending_conns_num  = 0; 
    158         n->is_accepting_conns = false; 
    159158 
    160159        n->fastcgi_servers    = NULL; 
     
    176175        if (fd_num < conns_max) { 
    177176                cherokee_fdpoll_free (n->fdpoll); 
    178                 CHEROKEE_FREE(n); 
     177                CHEROKEE_FREE (n); 
    179178                return ret_error; 
    180179        } 
    181  
    182         /* Number of connections that the thread can accept. 
    183          */ 
    184         n->conns_accept = (90 * conns_max / 100); 
    185         if (conns_max > n->conns_accept + 5) 
    186                 n->conns_accept = conns_max - 5; 
    187180 
    188181        /* Bogo now stuff 
     
    12631256} 
    12641257 
     1258static void 
     1259thread_full_handler (cherokee_thread_t *thd, int srv_socket) 
     1260{ 
     1261        ret_t                ret; 
     1262        cherokee_list_t     *i; 
     1263        cherokee_socket_t    sock; 
     1264        size_t               read  = 0; 
     1265        cherokee_boolean_t   done  = false; 
     1266        cherokee_server_t   *srv   = THREAD_SRV(thd); 
     1267        cherokee_buffer_t   *tmp   = THREAD_TMP_BUF1(thd); 
     1268 
     1269        /* Short path: nothing to accept 
     1270         */ 
     1271        if (cherokee_fdpoll_check (thd->fdpoll, srv_socket, FDPOLL_MODE_READ) <= 0) { 
     1272                return; 
     1273        } 
     1274 
     1275        /* Check all the threads 
     1276         */ 
     1277        list_for_each (i, &srv->thread_list) { 
     1278                if (THREAD(i)->conns_num < THREAD(i)->conns_max) 
     1279                        return; 
     1280        } 
     1281 
     1282        if (srv->main_thread->conns_num < srv->main_thread->conns_max) 
     1283                return; 
     1284 
     1285        /* In case there is no room in the entire server. The best 
     1286         * thing we can do reached this point is to get rid of the 
     1287         * connection as soon and quickly as possible. 
     1288         */ 
     1289        ret = cherokee_socket_init (&sock); 
     1290        if (unlikely (ret != ret_ok)) 
     1291                goto out; 
     1292 
     1293        /* Accept a connection 
     1294         */ 
     1295        do { 
     1296                ret = cherokee_socket_accept (&sock, srv_socket); 
     1297        } while (ret == ret_deny); 
     1298 
     1299        if (ret != ret_ok) 
     1300                goto out; 
     1301         
     1302        /* Read the request 
     1303         */ 
     1304#if 0 
     1305        do { 
     1306                ret = cherokee_socket_read (&sock, tmp->buf, tmp->size, &read); 
     1307        } while (ret == ret_eagain); 
     1308#endif 
     1309         
     1310        /* Write the error response 
     1311         */ 
     1312        cherokee_buffer_clean (tmp); 
     1313        cherokee_buffer_add_str ( 
     1314                tmp, 
     1315                "HTTP/1.0 " http_service_unavailable_string CRLF_CRLF       \ 
     1316                "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">" CRLF \ 
     1317                "<html><head><title>" http_service_unavailable_string       \ 
     1318                "</title></head><body><h1>" http_service_unavailable_string \ 
     1319                "</h1><p>Server run out of resources.</p></body></html>"); 
     1320        do { 
     1321                read = 0; 
     1322 
     1323                ret = cherokee_socket_bufwrite (&sock, tmp, &read); 
     1324                switch (ret) { 
     1325                case ret_ok: 
     1326                        if (read > 0) { 
     1327                                cherokee_buffer_move_to_begin (tmp, read); 
     1328                        } 
     1329                default: 
     1330                        done = true; 
     1331                } 
     1332                 
     1333                if (cherokee_buffer_is_empty (tmp)) 
     1334                        done = true; 
     1335        } while (!done); 
     1336 
     1337out: 
     1338        cherokee_socket_close (&sock); 
     1339        cherokee_socket_mrproper (&sock); 
     1340} 
    12651341 
    12661342static int 
    1267 __accept_from_server (cherokee_thread_t *thd, int srv_socket, cherokee_socket_type_t tls) 
     1343accept_new_connection (cherokee_thread_t *thd, int srv_socket, cherokee_socket_type_t tls) 
    12681344{ 
    12691345        ret_t                  ret; 
     
    12721348        cherokee_connection_t *new_conn; 
    12731349 
    1274         /* Return if there're too many or no new connections 
    1275          */ 
    1276         if (thd->conns_num >= thd->conns_max) 
    1277                 return 0; 
    1278  
     1350        /* Check whether there are connections waiting 
     1351         */ 
    12791352        if (cherokee_fdpoll_check (thd->fdpoll, srv_socket, FDPOLL_MODE_READ) <= 0) { 
    12801353                return 0; 
     
    12951368        if (unlikely(ret < ret_ok)) { 
    12961369                PRINT_ERROR_S ("ERROR: Trying to get a new connection object\n"); 
    1297                 cherokee_close_fd (new_fd); 
     1370                cherokee_fd_close (new_fd); 
    12981371                return 0; 
    12991372        } 
     
    13411414        /* Close new socket and reset its socket fd to default value. 
    13421415         */ 
    1343         cherokee_close_fd (new_fd); 
     1416        cherokee_fd_close (new_fd); 
    13441417        SOCKET_FD(&new_conn->socket) = -1; 
    13451418 
     
    13561429 
    13571430static int 
    1358 __should_accept_more_from_server (cherokee_thread_t *thd, int re) 
     1431should_accept_more (cherokee_thread_t *thd, int re) 
    13591432{ 
    13601433        const uint32_t recalculate_steps = 10; 
     
    13641437        if (unlikely (thd->conns_num >= thd->conns_max)) 
    13651438                return 0; 
    1366  
    13671439#if 0 
    13681440        if (unlikely (cherokee_fdpoll_is_full(thd->fdpoll))) { 
     
    13991471 
    14001472ret_t  
    1401 cherokee_thread_accept_on (cherokee_thread_t *thd) 
    1402 { 
    1403         ret_t ret; 
    1404  
    1405         if (thd->is_accepting_conns) 
    1406                 return ret_ok; 
    1407  
    1408         /* Add server sockets to polling set. 
    1409          */ 
    1410         ret = cherokee_fdpoll_add (thd->fdpoll, 
    1411                 S_SOCKET_FD(THREAD_SRV(thd)->socket), FDPOLL_MODE_READ); 
    1412         if (unlikely(ret < ret_ok)) 
    1413                 return ret; 
    1414  
    1415 #ifdef HAVE_TLS 
    1416         if (THREAD_SRV(thd)->tls_enabled) { 
    1417                 ret = cherokee_fdpoll_add (thd->fdpoll, 
    1418                         S_SOCKET_FD(THREAD_SRV(thd)->socket_tls), FDPOLL_MODE_READ); 
    1419                 if (unlikely(ret < ret_ok)) 
    1420                         return ret; 
    1421         } 
    1422 #endif /* HAVE_TLS */ 
    1423  
    1424         /* Set flag and return. 
    1425          */ 
    1426         thd->is_accepting_conns = true; 
    1427  
    1428         return ret_ok; 
    1429 } 
    1430  
    1431  
    1432 ret_t  
    1433 cherokee_thread_accept_off (cherokee_thread_t *thd) 
    1434 { 
    1435         ret_t ret; 
    1436  
    1437         if (!thd->is_accepting_conns) 
    1438                 return ret_ok; 
    1439  
    1440         /* Remove server sockets from polling set. 
    1441          */ 
    1442         ret = cherokee_fdpoll_del (thd->fdpoll, 
    1443                 S_SOCKET_FD(THREAD_SRV(thd)->socket)); 
    1444         if (unlikely(ret < ret_ok)) 
    1445                 return ret; 
    1446  
    1447 #ifdef HAVE_TLS 
    1448         if (THREAD_SRV(thd)->tls_enabled) { 
    1449                 ret = cherokee_fdpoll_del (thd->fdpoll, 
    1450                         S_SOCKET_FD(THREAD_SRV(thd)->socket_tls)); 
    1451                 if (unlikely(ret < ret_ok)) 
    1452                         return ret; 
    1453         } 
    1454 #endif /* HAVE_TLS */ 
    1455  
    1456         /* Set flag and return. 
    1457          */ 
    1458         thd->is_accepting_conns = false; 
    1459  
    1460         return ret_ok; 
    1461 } 
    1462  
    1463  
    1464 ret_t  
    14651473cherokee_thread_step_SINGLE_THREAD (cherokee_thread_t *thd) 
    14661474{ 
    14671475        int                re; 
    1468         ret_t              ret
     1476        cherokee_boolean_t accepting
    14691477        cherokee_server_t *srv           = THREAD_SRV(thd); 
    14701478        int                fdwatch_msecs = srv->fdwatch_msecs; 
     
    14751483 
    14761484        /* Reset the server socket. 
    1477          * cherokee_fdpoll_reset (thd->fdpoll, S_SOCKET_FD(srv->socket)); 
    1478          */ 
    1479  
    1480         /* If the thread is full of connections, it should not 
    1481          * get new connections. 
    1482          */ 
    1483         if (thd->conns_num >= thd->conns_max) { 
    1484                 if (thd->is_accepting_conns) 
    1485                         ret = cherokee_thread_accept_off (thd); 
    1486         } 
    1487         else if (thd->conns_num < thd->conns_accept) { 
    1488                 if (!thd->is_accepting_conns) 
    1489                         ret = cherokee_thread_accept_on (thd); 
    1490         } 
    1491  
     1485         * // cherokee_fdpoll_reset (thd->fdpoll, S_SOCKET_FD(srv->socket)); 
     1486         */ 
    14921487#if 0 
    14931488        if (unlikely (cherokee_fdpoll_is_full (thd->fdpoll))) { 
     
    15081503                goto out; 
    15091504 
    1510         do { 
    1511                 re = __accept_from_server (thd, S_SOCKET_FD(srv->socket), non_TLS); 
    1512         } while (__should_accept_more_from_server (thd, re)); 
    1513  
     1505        /* If the thread is full of connections, it should not 
     1506         * get new connections. 
     1507         */ 
     1508        accepting = (thd->conns_num < thd->conns_max); 
     1509        if (accepting) { 
     1510                do { 
     1511                        re = accept_new_connection (thd, S_SOCKET_FD(srv->socket), non_TLS); 
     1512                } while (should_accept_more (thd, re)); 
     1513                 
    15141514#ifdef HAVE_TLS 
    1515         if (srv->tls_enabled) { 
    1516                 do { 
    1517                         re = __accept_from_server (thd, S_SOCKET_FD(srv->socket_tls), TLS); 
    1518                 } while (__should_accept_more_from_server (thd, re)); 
    1519         } 
    1520 #endif /* HAVE_TLS */ 
     1515                if (srv->tls_enabled) { 
     1516                        do { 
     1517                                re = accept_new_connection (thd, S_SOCKET_FD(srv->socket_tls), TLS); 
     1518                        } while (should_accept_more (thd, re)); 
     1519                } 
     1520#endif 
     1521        } else { 
     1522                thread_full_handler (thd, S_SOCKET_FD(srv->socket)); 
     1523                if (srv->tls_enabled) { 
     1524                        thread_full_handler (thd, S_SOCKET_FD(srv->socket_tls)); 
     1525                } 
     1526        } 
    15211527 
    15221528out: 
     
    15361542step_MULTI_THREAD_block (cherokee_thread_t *thd, int socket, pthread_mutex_t *mutex, int fdwatch_msecs) 
    15371543{ 
    1538         int   re; 
    1539         ret_t ret; 
    1540  
    1541         /* In this case, thd->is_accepting_conns is alwasy true. 
    1542          */ 
     1544        int                re; 
     1545        ret_t              ret; 
     1546        cherokee_boolean_t accepting; 
     1547 
    15431548        CHEROKEE_MUTEX_LOCK (mutex); 
    15441549         
     
    15601565        update_bogo_now (thd); 
    15611566 
    1562         do { 
    1563                 re = __accept_from_server (thd, socket, non_TLS); 
    1564         } while (__should_accept_more_from_server (thd, re)); 
    1565  
     1567        /* Accept a new connection 
     1568         */ 
     1569        accepting = (thd->conns_num < thd->conns_max); 
     1570        if (accepting) { 
     1571                do { 
     1572                        re = accept_new_connection (thd, socket, non_TLS); 
     1573                } while (should_accept_more (thd, re)); 
     1574        } else { 
     1575                thread_full_handler (thd, socket); 
     1576        } 
     1577 
     1578        /* Release the socket 
     1579         */ 
    15661580        ret = cherokee_fdpoll_del (thd->fdpoll, socket); 
    15671581        if (ret != ret_ok) 
    15681582                SHOULDNT_HAPPEN; 
    15691583 
    1570         CHEROKEE_MUTEX_UNLOCK (mutex); 
    1571          
     1584        CHEROKEE_MUTEX_UNLOCK (mutex);   
    15721585        return ret_ok; 
    15731586} 
     
    15771590step_MULTI_THREAD_nonblock (cherokee_thread_t *thd, int socket, pthread_mutex_t *mutex, int fdwatch_msecs) 
    15781591{ 
    1579         int   re; 
    1580         ret_t ret; 
    1581         int   unlocked = 1; 
    1582          
    1583         if (thd->is_accepting_conns) { 
    1584                 /* Try to lock 
    1585                  */ 
    1586                 unlocked = CHEROKEE_MUTEX_TRY_LOCK (mutex); 
    1587         } 
    1588  
     1592        ret_t              ret; 
     1593        int                re; 
     1594        cherokee_boolean_t accepting; 
     1595        int                unlocked  = 1; 
     1596 
     1597        /* Try to lock. Not success: wait for an event to happen 
     1598         */ 
     1599        unlocked = CHEROKEE_MUTEX_TRY_LOCK (mutex); 
    15891600        if (unlocked) { 
    15901601                cherokee_fdpoll_watch (thd->fdpoll, fdwatch_msecs); 
     
    15921603        } 
    15931604 
    1594         /* Now it owns the socket. 
     1605        /* Now it owns the socket 
    15951606         */ 
    15961607        ret = cherokee_fdpoll_add (thd->fdpoll, socket, FDPOLL_MODE_READ); 
    15971608        if (unlikely (ret < ret_ok)) { 
    1598                 CHEROKEE_MUTEX_UNLOCK (mutex); 
    1599                 return ret_error; 
    1600         } 
    1601         /* cherokee_fdpoll_reset (thd->fdpoll, socket); 
    1602          */ 
     1609                ret = ret_error; 
     1610                goto out; 
     1611        } 
     1612 
    16031613        cherokee_fdpoll_watch (thd->fdpoll, fdwatch_msecs); 
    16041614 
    1605         do { 
    1606                 re = __accept_from_server (thd, socket, non_TLS); 
    1607         } while (__should_accept_more_from_server (thd, re)); 
    1608  
     1615        /* It should either accept o discard a connection 
     1616         */ 
     1617        accepting = (thd->conns_num < thd->conns_max); 
     1618        if (accepting) { 
     1619                do { 
     1620                        re = accept_new_connection (thd, socket, non_TLS); 
     1621                } while (should_accept_more (thd, re)); 
     1622 
     1623        } else { 
     1624                thread_full_handler (thd, socket); 
     1625        } 
     1626 
     1627        /* Release the server socket 
     1628         */ 
    16091629        ret = cherokee_fdpoll_del (thd->fdpoll, socket); 
    16101630        if (ret != ret_ok) 
    16111631                SHOULDNT_HAPPEN; 
    16121632 
     1633        ret = ret_ok; 
     1634 
     1635out: 
    16131636        CHEROKEE_MUTEX_UNLOCK (mutex); 
    1614  
    1615         return ret_ok; 
     1637        return ret; 
     1638 
    16161639} 
    16171640 
     
    16231646                                int socket_tls, pthread_mutex_t *mutex_tls) 
    16241647{ 
    1625         int   re; 
    1626         ret_t ret; 
    1627         int   unlock     = 1; 
    1628         int   unlock_tls = 1; 
    1629  
    1630         if (thd->is_accepting_conns) { 
    1631                 /* Try to lock the mutex 
    1632                  */ 
    1633                 unlock = CHEROKEE_MUTEX_TRY_LOCK (mutex); 
    1634         } 
    1635         if (!unlock) { 
     1648        ret_t              ret; 
     1649        int                re; 
     1650        cherokee_boolean_t accepting; 
     1651        int                unlocked     = 1; 
     1652        int                unlocked_tls = 1; 
     1653 
     1654        /* Try to lock both mutexes 
     1655         */ 
     1656        unlocked = CHEROKEE_MUTEX_TRY_LOCK (mutex); 
     1657        if (! unlocked) { 
    16361658                ret = cherokee_fdpoll_add (thd->fdpoll, socket, FDPOLL_MODE_READ); 
    1637                 if (ret < ret_ok) { 
    1638                         goto error; 
    1639                 } 
    1640                 /* cherokee_fdpoll_reset (thd->fdpoll, socket); 
    1641                  */ 
    1642         } 
    1643  
    1644         if (thd->is_accepting_conns) { 
    1645                 /* Try to lock the TLS mutex 
    1646                  */ 
    1647                 unlock_tls = CHEROKEE_MUTEX_TRY_LOCK (mutex_tls); 
    1648         } 
    1649         if (!unlock_tls) { 
     1659                if (unlikely (ret < ret_ok)) { 
     1660                        ret = ret_error; 
     1661                        goto out; 
     1662                } 
     1663        } 
     1664         
     1665        /* Try to lock the TLS mutex 
     1666         */ 
     1667        unlocked_tls = CHEROKEE_MUTEX_TRY_LOCK (mutex_tls); 
     1668        if (!unlocked_tls) { 
    16501669                ret = cherokee_fdpoll_add (thd->fdpoll, socket_tls, FDPOLL_MODE_READ); 
    1651                 if (unlikely (ret < ret_ok))  
    1652                         goto error; 
    1653  
    1654                 /* cherokee_fdpoll_reset (thd->fdpoll, socket_tls); 
    1655                  */ 
    1656         } 
    1657  
    1658         /* Inspect the fds and maybe sleep 
     1670                if (unlikely (ret < ret_ok)) { 
     1671                        ret = ret_error; 
     1672                        goto out; 
     1673                } 
     1674        } 
     1675 
     1676        /* Inspect the fds. It may sleep if nothing happens 
    16591677         */ 
    16601678        cherokee_fdpoll_watch (thd->fdpoll, fdwatch_msecs); 
    16611679                 
    1662         /* Restore.. 
    1663          */ 
    1664         if (!unlock) { 
    1665                 do { 
    1666                         re = __accept_from_server (thd, socket, non_TLS); 
    1667                 } while (__should_accept_more_from_server (thd, re)); 
     1680        /* accept o discard a connections 
     1681         */ 
     1682        accepting = (thd->conns_num < thd->conns_max); 
     1683        if (accepting) { 
     1684                if (!unlocked) { 
     1685                        do { 
     1686                                re = accept_new_connection (thd, socket, non_TLS); 
     1687                        } while (should_accept_more (thd, re)); 
    16681688                 
    1669                 ret = cherokee_fdpoll_del (thd->fdpoll, socket); 
    1670                 if (ret != ret_ok) 
    1671                         SHOULDNT_HAPPEN; 
     1689                        ret = cherokee_fdpoll_del (thd->fdpoll, socket); 
     1690                        if (ret != ret_ok) 
     1691                                SHOULDNT_HAPPEN; 
     1692                } 
     1693         
     1694                if (!unlocked_tls) { 
     1695                        do { 
     1696                                re = accept_new_connection (thd, socket_tls, TLS); 
     1697                        } while (should_accept_more (thd, re)); 
     1698                         
     1699                        ret = cherokee_fdpoll_del (thd->fdpoll, socket_tls); 
     1700                        if (ret != ret_ok) 
     1701                                SHOULDNT_HAPPEN; 
     1702                } 
     1703 
     1704        } else { 
     1705                if (!unlocked) 
     1706                        thread_full_handler (thd, socket); 
     1707 
     1708                if (!unlocked_tls)  
     1709                        thread_full_handler (thd, socket_tls); 
     1710        } 
     1711         
     1712        ret = ret_ok; 
     1713 
     1714out: 
     1715        if (!unlocked) 
    16721716                CHEROKEE_MUTEX_UNLOCK (mutex); 
    1673         } 
    1674          
    1675         if (!unlock_tls) { 
    1676                 do { 
    1677                         re = __accept_from_server (thd, socket_tls, TLS); 
    1678                 } while (__should_accept_more_from_server (thd, re)); 
    1679                  
    1680                 ret = cherokee_fdpoll_del (thd->fdpoll, socket_tls); 
    1681                 if (ret != ret_ok) 
    1682                         SHOULDNT_HAPPEN; 
     1717        if (!unlocked_tls) 
    16831718                CHEROKEE_MUTEX_UNLOCK (mutex_tls); 
    1684         } 
    1685          
    1686         return ret_ok; 
    1687  
    1688  
    1689 error: 
    1690         if (!unlock)     CHEROKEE_MUTEX_UNLOCK (mutex); 
    1691         if (!unlock_tls) CHEROKEE_MUTEX_UNLOCK (mutex_tls); 
    1692  
    1693         return ret_error; 
     1719 
     1720        return ret; 
    16941721} 
    16951722 
     
    17071734        cherokee_socket_type_t  type1; 
    17081735        cherokee_socket_type_t  type2; 
     1736        cherokee_boolean_t      accepting; 
    17091737 
    17101738        if (thd->thread_pref == thread_tls_normal) { 
     
    17261754        } 
    17271755 
    1728         /* In this case, thd->is_accepting_conns is alwasy true. 
    1729          */ 
    1730  
    17311756        /* Lock the main mutex 
    17321757         */ 
     
    17381763                return ret_error; 
    17391764        } 
    1740         /* cherokee_fdpoll_reset (thd->fdpoll, socket1); 
    1741          */ 
     1765 
     1766        /* cherokee_fdpoll_reset (thd->fdpoll, socket1); */ 
     1767          
    17421768 
    17431769        /* Try to lock the optional groups 
     
    17621788 
    17631789        /* Update the thread time values before accept new connections. 
    1764          * This ensure a good timeout value for it. 
     1790         * This ensures an updated timeout value for it. 
    17651791         */ 
    17661792        update_bogo_now (thd); 
    17671793                 
    1768         do { 
    1769                 re = __accept_from_server (thd, socket1, type1); 
    1770         } while (__should_accept_more_from_server (thd, re)); 
     1794        /* Accept / Discard connection 
     1795         */ 
     1796        accepting = (thd->conns_num < thd->conns_max); 
     1797        if (accepting) { 
     1798                do { 
     1799                        re = accept_new_connection (thd, socket1, type1); 
     1800                } while (should_accept_more (thd, re)); 
     1801        } else { 
     1802                thread_full_handler (thd, socket1);              
     1803        } 
    17711804 
    17721805        /* Unlock the mail lock 
     
    17831816        if (!unlock2) { 
    17841817                do { 
    1785                         re = __accept_from_server (thd, socket2, type2); 
    1786                 } while (__should_accept_more_from_server (thd, re)); 
     1818                        re = accept_new_connection (thd, socket2, type2); 
     1819                } while (should_accept_more (thd, re)); 
    17871820                 
    17881821                ret = cherokee_fdpoll_del (thd->fdpoll, socket2); 
     
    18061839        int                fdwatch_msecs = srv->fdwatch_msecs; 
    18071840 
    1808  
    18091841        /* Try to update bogo_now 
    18101842         */ 
    18111843        cherokee_bogotime_try_update(); 
    1812  
    1813         /* If the thread is full of connections, it should not 
    1814          * get new connections. 
    1815          */ 
    1816         if (thd->conns_num >= thd->conns_max) { 
    1817                 if (thd->is_accepting_conns) 
    1818                         thd->is_accepting_conns = false; 
    1819         } 
    1820         else if (thd->conns_num < thd->conns_accept) { 
    1821                 if (!thd->is_accepting_conns) 
    1822                         thd->is_accepting_conns = true; 
    1823         } 
    18241844 
    18251845#if 0 
     
    18411861         */ 
    18421862        if (srv->tls_enabled) { 
    1843                 if ((thd->exit == false) && 
     1863                if ((!dont_block) && 
     1864                    (thd->exit == false) && 
    18441865                    (thd->active_list_num == 0) && 
    1845                     (thd->polling_list_num == 0) && (!dont_block)) { 
     1866                    (thd->polling_list_num == 0)) 
     1867                { 
    18461868                        step_MULTI_THREAD_TLS_block (thd, fdwatch_msecs,  
    1847                             S_SOCKET_FD(srv->socket), &THREAD_SRV(thd)->accept_mutex,          
     1869                            S_SOCKET_FD(srv->socket),     &THREAD_SRV(thd)->accept_mutex,      
    18481870                            S_SOCKET_FD(srv->socket_tls), &THREAD_SRV(thd)->accept_tls_mutex); 
    18491871                } else { 
    18501872                        step_MULTI_THREAD_TLS_nonblock (thd, fdwatch_msecs,  
    1851                             S_SOCKET_FD(srv->socket), &THREAD_SRV(thd)->accept_mutex,          
     1873                            S_SOCKET_FD(srv->socket),     &THREAD_SRV(thd)->accept_mutex,      
    18521874                            S_SOCKET_FD(srv->socket_tls), &THREAD_SRV(thd)->accept_tls_mutex); 
    18531875                } 
     
    18591881        /* Try to get new connections from http 
    18601882         */ 
    1861         if ((thd->exit == false) && 
     1883        if ((!dont_block) && 
     1884            (thd->exit == false) && 
    18621885            (thd->active_list_num == 0) &&  
    1863             (thd->polling_list_num == 0) && (!dont_block)) { 
    1864                 step_MULTI_THREAD_block (thd, S_SOCKET_FD(srv->socket), &THREAD_SRV(thd)->accept_mutex, fdwatch_msecs); 
     1886            (thd->polling_list_num == 0)) 
     1887        { 
     1888                step_MULTI_THREAD_block (thd, S_SOCKET_FD(srv->socket),  
     1889                                         &THREAD_SRV(thd)->accept_mutex, fdwatch_msecs); 
    18651890        } else { 
    1866                 step_MULTI_THREAD_nonblock (thd, S_SOCKET_FD(srv->socket), &THREAD_SRV(thd)->accept_mutex, fdwatch_msecs); 
     1891                step_MULTI_THREAD_nonblock (thd, S_SOCKET_FD(srv->socket), 
     1892                                            &THREAD_SRV(thd)->accept_mutex, fdwatch_msecs); 
    18671893        } 
    18681894         
  • cherokee/trunk/cherokee/thread.h

    r1673 r1759  
    7575        cherokee_boolean_t      exit; 
    7676 
    77         cherokee_boolean_t      is_accepting_conns; /* true=accepts new conns */ 
    78  
    7977        int                     conns_num;          /* open connections */ 
    80         int                     conns_max;          /* max. connections */ 
    81         int                     conns_accept;       /* accept limit */ 
     78        int                     conns_max;          /* max opened conns */ 
    8279 
    8380        int                     active_list_num;    /* active connections */ 
  • cherokee/trunk/cherokee/util.c

    r1754 r1759  
    12421242 
    12431243ret_t  
    1244 cherokee_close_fd (cint_t fd) 
     1244cherokee_fd_close (int fd) 
    12451245{ 
    12461246        int re; 
    12471247         
    1248         if (fd < 0) { 
     1248        if (unlikely (fd < 0)) { 
    12491249                return ret_error; 
    12501250        } 
  • cherokee/trunk/cherokee/util.h

    r1754 r1759  
    111111ret_t cherokee_fd_set_nodelay     (int fd, cherokee_boolean_t enable); 
    112112ret_t cherokee_fd_set_closexec    (int fd); 
     113ret_t cherokee_fd_close           (int fd); 
    113114 
    114115ret_t cherokee_sys_fdlimit_get (cuint_t *limit); 
    115116ret_t cherokee_sys_fdlimit_set (cuint_t  limit); 
    116 ret_t cherokee_close_fd        (cint_t fd); 
    117117ret_t cherokee_get_shell       (const char **shell, const char **binary); 
    118118void  cherokee_print_wrapped   (cherokee_buffer_t *buffer);