Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:13966 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 43699 invoked by uid 1010); 1 Dec 2004 18:11:12 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 43674 invoked from network); 1 Dec 2004 18:11:11 -0000 Received: from unknown (HELO foo.ardendo.se) (212.32.189.9) by pb1.pair.com with SMTP; 1 Dec 2004 18:11:11 -0000 Received: by foo.ardendo.se (Postfix, from userid 10001) id 767B0103B7; Wed, 1 Dec 2004 19:11:10 +0100 (CET) Date: Wed, 1 Dec 2004 19:11:10 +0100 To: internals@lists.php.net Message-ID: <20041201181110.GA25044@foo.ardendo.se> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.2.1i Subject: [PATCH] PHP_FCGI_TIMEOUT From: ada@ardendo.se (Christer Holgersson) Included is a patch that adds a PHP_FCGI_TIMEOUT that works somewhat akin to PHP_FCGI_MAX_REQUESTS. With PHP_FCGI_TIMEOUT set to 300 the whole fcgi process will terminate after 300 sec with no request to any of the worker children - great for those seldom used dynamically created fcgi scripts. Cheers, /Chris PS. The patch works just as well in 5.0.2 as in 4.3.9. diff -rub php-4.3.9/sapi/cgi/cgi_main.c php-4.3.9-ada/sapi/cgi/cgi_main.c --- php-4.3.9/sapi/cgi/cgi_main.c 2004-07-15 00:38:18.000000000 +0200 +++ php-4.3.9-ada/sapi/cgi/cgi_main.c 2004-12-01 09:00:58.595259999 +0100 @@ -946,6 +946,24 @@ /* We should exit at this point, but MacOSX doesn't seem to */ exit( 0 ); } + + +static int php_fcgi_timeout = 0; + +static void reset_timeout() +{ + alarm(php_fcgi_timeout); + /* fprintf(stderr, "FastCGI: reset timeout\n"); */ + signal(SIGUSR1, reset_timeout); +} + +static void handle_timeout() +{ + /* fprintf(stderr, "FastCGI: timeout\n"); */ + fastcgi_cleanup(SIGALRM); + exit(0); +} + #endif /* {{{ main @@ -980,6 +998,7 @@ #endif #if PHP_FASTCGI + int php_fcgi_pid = getpid(); int max_requests = 500; int requests = 0; int fastcgi = !FCGX_IsCGI(); @@ -1178,6 +1197,20 @@ } } + /* How long all children can be idle before terminating */ + if( getenv( "PHP_FCGI_TIMEOUT" )) { + php_fcgi_timeout = atoi( getenv( "PHP_FCGI_TIMEOUT" )); + if( !php_fcgi_timeout ) { + fprintf( stderr, + "PHP_FCGI_TIMEOUT is not valid\n" ); + return FAILURE; + } + /* ada@riksnet.se: children signal parent when they do work, */ + /* which resets the timeout value */ + signal(SIGALRM, handle_timeout); + signal(SIGUSR1, reset_timeout); + } + /* make php call us to get _ENV vars */ php_php_import_environment_variables = php_import_environment_variables; php_import_environment_variables = cgi_php_import_environment_variables; @@ -1305,6 +1338,8 @@ while (!fastcgi || FCGX_Accept_r( &request ) >= 0) { + /* ada@riksnet.se: let parent know we have work to do */ + if (php_fcgi_timeout) kill(php_fcgi_pid, SIGUSR1); #endif #if PHP_FASTCGI