Donate "Fast Accept Loop" from Premium thttpd which speeds up processing

new connections when the connection table contains lots of entries.
This commit is contained in:
Sascha Schumann 2002-10-26 19:41:27 +00:00
parent 3a1c278361
commit 7b3bea0b41

View File

@ -858,7 +858,7 @@ diff -ur thttpd-2.21b/mmc.h thttpd-2.21b-cool/mmc.h
** If you have a stat buffer on the file, pass it in, otherwise pass 0.
diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
--- thttpd-2.21b/thttpd.c Tue Apr 24 00:41:57 2001
+++ thttpd-2.21b-cool/thttpd.c Sat Oct 26 19:54:53 2002
+++ thttpd-2.21b-cool/thttpd.c Sat Oct 26 21:36:02 2002
@@ -95,10 +95,10 @@
httpd_conn* hc;
int tnums[MAXTHROTTLENUMS]; /* throttle indexes */
@ -872,7 +872,14 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
Timer* wakeup_timer;
Timer* linger_timer;
long wouldblock_delay;
@@ -111,12 +111,15 @@
@@ -106,17 +106,21 @@
off_t bytes_sent;
off_t bytes_to_send;
} connecttab;
-static connecttab* connects;
+static connecttab* connects, **free_connects;
+static int next_free_connect;
static int numconnects, maxconnects;
static int httpd_conn_count;
/* The connection states. */
@ -894,7 +901,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
static httpd_server* hs = (httpd_server*) 0;
int terminate = 0;
@@ -140,14 +143,14 @@
@@ -140,14 +144,14 @@
static int handle_newconnect( struct timeval* tvP, int listen_fd );
static void handle_read( connecttab* c, struct timeval* tvP );
static void handle_send( connecttab* c, struct timeval* tvP );
@ -912,7 +919,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
static void wakeup_connection( ClientData client_data, struct timeval* nowP );
static void linger_clear_connection( ClientData client_data, struct timeval* nowP );
static void occasional( ClientData client_data, struct timeval* nowP );
@@ -157,6 +160,12 @@
@@ -157,6 +161,12 @@
static void logstats( struct timeval* nowP );
static void thttpd_logstats( long secs );
@ -925,7 +932,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
static void
handle_term( int sig )
@@ -198,6 +207,8 @@
@@ -198,6 +208,8 @@
}
@ -934,7 +941,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
static void
handle_usr2( int sig )
{
@@ -430,6 +441,12 @@
@@ -430,6 +442,12 @@
syslog( LOG_CRIT, "tmr_create(occasional) failed" );
exit( 1 );
}
@ -947,7 +954,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
if ( numthrottles > 0 )
{
/* Set up the throttles timer. */
@@ -454,12 +471,14 @@
@@ -454,12 +472,14 @@
/* If we're root, try to become someone else. */
if ( getuid() == 0 )
{
@ -962,7 +969,25 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
/* Set primary group. */
if ( setgid( gid ) < 0 )
{
@@ -518,6 +537,8 @@
@@ -495,13 +515,17 @@
}
maxconnects -= SPARE_FDS;
connects = NEW( connecttab, maxconnects );
+ free_connects = malloc(sizeof(connecttab *) * maxconnects);
+ next_free_connect = maxconnects;
if ( connects == (connecttab*) 0 )
{
syslog( LOG_CRIT, "out of memory allocating a connecttab" );
exit( 1 );
}
+
for ( cnum = 0; cnum < maxconnects; ++cnum )
{
+ free_connects[cnum] = &connects[maxconnects - cnum - 1];
connects[cnum].conn_state = CNST_FREE;
connects[cnum].hc = (httpd_conn*) 0;
}
@@ -518,6 +542,8 @@
/* Main loop. */
(void) gettimeofday( &tv, (struct timezone*) 0 );
@ -971,7 +996,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
while ( ( ! terminate ) || numconnects > 0 )
{
/* Do the fd watch. */
@@ -566,15 +587,17 @@
@@ -566,15 +592,17 @@
if ( c == (connecttab*) 0 )
continue;
hc = c->hc;
@ -998,7 +1023,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
}
tmr_run( &tv );
@@ -1196,8 +1219,10 @@
@@ -1196,8 +1224,10 @@
logstats( &tv );
for ( cnum = 0; cnum < maxconnects; ++cnum )
{
@ -1010,7 +1035,45 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
if ( connects[cnum].hc != (httpd_conn*) 0 )
{
httpd_destroy_conn( connects[cnum].hc );
@@ -1272,19 +1297,12 @@
@@ -1214,6 +1244,7 @@
}
mmc_destroy();
tmr_destroy();
+ free( (void*) free_connects );
free( (void*) connects );
if ( throttles != (throttletab*) 0 )
free( (void*) throttles );
@@ -1234,7 +1265,7 @@
for (;;)
{
/* Is there room in the connection table? */
- if ( numconnects >= maxconnects )
+ if ( numconnects >= maxconnects || next_free_connect == 0 )
{
/* Out of connection slots. Run the timers, then the
** existing connections, and maybe we'll free up a slot
@@ -1245,10 +1276,10 @@
return 0;
}
/* Find a free connection entry. */
- for ( cnum = 0; cnum < maxconnects; ++cnum )
- if ( connects[cnum].conn_state == CNST_FREE )
- break;
- c = &connects[cnum];
+
+ c = free_connects[--next_free_connect];
+ free_connects[next_free_connect] = NULL;
+
/* Make the httpd_conn if necessary. */
if ( c->hc == (httpd_conn*) 0 )
{
@@ -1267,24 +1298,18 @@
{
case GC_FAIL:
case GC_NO_MORE:
+ free_connects[next_free_connect++] = c;
return 1;
}
c->conn_state = CNST_READING;
++numconnects;
client_data.p = c;
@ -1032,7 +1095,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
/* Set the connection file descriptor to no-delay mode. */
httpd_set_ndelay( c->hc->conn_fd );
@@ -1297,12 +1315,28 @@
@@ -1297,12 +1322,28 @@
}
}
@ -1062,7 +1125,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
httpd_conn* hc = c->hc;
/* Is there room in our buffer to read more bytes? */
@@ -1311,7 +1345,7 @@
@@ -1311,7 +1352,7 @@
if ( hc->read_size > 5000 )
{
httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
@ -1071,7 +1134,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
return;
}
httpd_realloc_str(
@@ -1327,29 +1361,57 @@
@@ -1327,29 +1368,57 @@
** EWOULDBLOCK; however, this apparently can happen if a packet gets
** garbled.
*/
@ -1084,13 +1147,13 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
+ httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
+ }
+ clear_connection( c, tvP, 0 );
+ return;
return;
+ } else if ( sz < 0 ) {
+ if (errno != EWOULDBLOCK) {
+ httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" );
+ clear_connection( c, tvP, 0 );
+ }
return;
+ return;
+ }
+ c->last_io = httpd_time_now;
+ if (sz > 0) hc->read_idx += sz;
@ -1137,7 +1200,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
return;
}
@@ -1358,7 +1420,7 @@
@@ -1358,7 +1427,7 @@
{
httpd_send_err(
hc, 503, httpd_err503title, "", httpd_err503form, hc->encodedurl );
@ -1146,7 +1209,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
return;
}
@@ -1366,7 +1428,7 @@
@@ -1366,7 +1435,7 @@
if ( httpd_start_request( hc, tvP ) < 0 )
{
/* Something went wrong. Close down the connection. */
@ -1155,7 +1218,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
return;
}
@@ -1384,37 +1446,24 @@
@@ -1384,37 +1453,24 @@
{
/* No file address means someone else is handling it. */
c->bytes_sent = hc->bytes_sent;
@ -1200,7 +1263,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
static void
handle_send( connecttab* c, struct timeval* tvP )
{
@@ -1443,6 +1492,9 @@
@@ -1443,6 +1499,9 @@
iv[1].iov_base = &(hc->file_address[c->bytes_sent]);
iv[1].iov_len = MIN( c->bytes_to_send - c->bytes_sent, c->limit );
sz = writev( hc->conn_fd, iv, 2 );
@ -1210,7 +1273,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
}
if ( sz == 0 ||
@@ -1486,12 +1538,12 @@
@@ -1486,12 +1545,12 @@
*/
if ( errno != EPIPE && errno != EINVAL && errno != ECONNRESET )
syslog( LOG_ERR, "write - %m sending %.80s", hc->encodedurl );
@ -1225,7 +1288,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
/* Was this a headers + file writev()? */
if ( hc->responselen > 0 )
{
@@ -1500,7 +1552,7 @@
@@ -1500,7 +1559,7 @@
{
/* Yes; move the unwritten part to the front of the buffer. */
int newlen = hc->responselen - sz;
@ -1234,7 +1297,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
hc->responselen = newlen;
sz = 0;
}
@@ -1519,7 +1571,7 @@
@@ -1519,7 +1578,7 @@
if ( c->bytes_sent >= c->bytes_to_send )
{
/* This conection is finished! */
@ -1243,7 +1306,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
return;
}
@@ -1560,6 +1612,9 @@
@@ -1560,6 +1619,9 @@
char buf[1024];
int r;
@ -1253,7 +1316,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
/* In lingering-close mode we just read and ignore bytes. An error
** or EOF ends things, otherwise we go until a timeout.
*/
@@ -1569,6 +1624,37 @@
@@ -1569,6 +1631,37 @@
}
@ -1291,7 +1354,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
static int
check_throttles( connecttab* c )
{
@@ -1635,23 +1721,18 @@
@@ -1635,23 +1728,18 @@
static void
@ -1321,7 +1384,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
if ( c->wakeup_timer != (Timer*) 0 )
{
tmr_cancel( c->wakeup_timer );
@@ -1669,13 +1750,36 @@
@@ -1669,13 +1757,36 @@
** circumstances that make a lingering close necessary. If the flag
** isn't set we do the real close now.
*/
@ -1360,7 +1423,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
client_data.p = c;
c->linger_timer = tmr_create(
tvP, linger_clear_connection, client_data, LINGER_TIME * 1000L, 0 );
@@ -1684,9 +1788,19 @@
@@ -1684,9 +1795,19 @@
syslog( LOG_CRIT, "tmr_create(linger_clear_connection) failed" );
exit( 1 );
}
@ -1381,7 +1444,13 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
}
@@ -1707,40 +1821,6 @@
@@ -1702,45 +1823,12 @@
tmr_cancel( c->linger_timer );
c->linger_timer = 0;
}
+ free_connects[next_free_connect++] = c;
c->conn_state = CNST_FREE;
--numconnects;
}
@ -1422,7 +1491,7 @@ diff -ur thttpd-2.21b/thttpd.c thttpd-2.21b-cool/thttpd.c
static void
wakeup_connection( ClientData client_data, struct timeval* nowP )
@@ -1826,3 +1906,41 @@
@@ -1826,3 +1914,41 @@
stats_connections = stats_bytes = 0L;
stats_simultaneous = 0;
}