From e241906ba28113f5b34d0ed04637dfd0cfd6bbd7 Mon Sep 17 00:00:00 2001 From: robs Date: Thu, 28 Feb 2002 22:52:50 +0000 Subject: [PATCH] Add support for backing off attempts to start applications that continuously fail to start. Three new macros defined in mod_fastcgi.h control this behaviour: MAX_FAILED_STARTS, RUNTIME_SUCCESS_INTERVAL, FAILED_STARTS_DELAY --- CHANGES | 4 ++++ fcgi.h | 4 ++-- fcgi_pm.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- mod_fastcgi.h | 20 ++++++++++++++++++- 4 files changed, 84 insertions(+), 8 deletions(-) diff --git a/CHANGES b/CHANGES index 9f71522..66e361e 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,9 @@ 2.2.13 + *) Add support for backing off attempts to start applications that continuously + fail to start. Three new macros defined in mod_fastcgi.h control this + behaviour: MAX_FAILED_STARTS, RUNTIME_SUCCESS_INTERVAL, FAILED_STARTS_DELAY + *) [WIN32] Add (back) support for use of TerminateProcess() to accomodate applications that do not (properly) support the shutdown event (this feature was introduced in fcgi2 2.2.2 and improved in 2.2.4). The diff --git a/fcgi.h b/fcgi.h index 484ab22..0d3a640 100644 --- a/fcgi.h +++ b/fcgi.h @@ -1,5 +1,5 @@ /* - * $Id: fcgi.h,v 1.34 2002/01/30 20:40:14 robs Exp $ + * $Id: fcgi.h,v 1.35 2002/02/28 22:52:50 robs Exp $ */ #ifndef FCGI_H @@ -116,8 +116,8 @@ typedef struct _FastCgiServerInfo { u_int restartDelay; /* number of seconds to wait between * restarts after failure. Can be zero. */ int restartOnExit; /* = TRUE = restart. else terminate/free */ - u_int numRestarts; /* Total number of restarts */ u_int numFailures; /* num restarts due to exit failure */ + int bad; /* is [not] having start problems */ struct sockaddr *socket_addr; /* Socket Address of FCGI app server class */ #ifdef WIN32 struct sockaddr *dest_addr; /* for local apps on NT need socket address */ diff --git a/fcgi_pm.c b/fcgi_pm.c index e024e9a..d2050a9 100644 --- a/fcgi_pm.c +++ b/fcgi_pm.c @@ -1,5 +1,5 @@ /* - * $Id: fcgi_pm.c,v 1.69 2002/02/28 15:58:11 robs Exp $ + * $Id: fcgi_pm.c,v 1.70 2002/02/28 22:52:50 robs Exp $ */ @@ -1534,10 +1534,67 @@ void fcgi_pm_main(void *dummy) int restart = (s->procs[i].pid < 0); time_t restartTime = s->restartTime; - restartTime += (restart) ? s->restartDelay : s->initStartDelay; + if (s->bad) + { + /* we've gone to using the badDelay, the only thing that + resets bad is when badDelay has expired. but numFailures + is only just set below its threshold. the proc's + start_times are all reset when the bad is. the numFailures + is reset when we see an app run for a period */ + + s->procs[i].start_time = 0; + } + + if (s->numFailures > MAX_FAILED_STARTS) + { + time_t last_start_time = s->procs[i].start_time; + + if (last_start_time && now - last_start_time > RUNTIME_SUCCESS_INTERVAL) + { + s->bad = 0; + s->numFailures = 0; + } + else + { + unsigned int j; + + for (j = 0; j < numChildren; ++j) + { + if (s->procs[j].pid <= 0) continue; + if (s->procs[j].state != FCGI_RUNNING_STATE) continue; + if (s->procs[j].start_time == 0) continue; + if (now - s->procs[j].start_time > RUNTIME_SUCCESS_INTERVAL) break; + } + + if (j >= numChildren) + { + s->bad = 1; + } + else + { + s->bad = 0; + s->numFailures = 0; + } + } + } + + if (s->bad) + { + restartTime += FAILED_STARTS_DELAY; + } + else + { + restartTime += (restart) ? s->restartDelay : s->initStartDelay; + } if (restartTime <= now) { + if (s->bad) + { + s->bad = 0; + s->numFailures = MAX_FAILED_STARTS; + } + if (s->listenFd < 0 && init_listen_sock(s)) { if (sleepSeconds > s->initStartDelay) @@ -1574,9 +1631,6 @@ void fcgi_pm_main(void *dummy) s->procs[i].state = FCGI_RUNNING_STATE; - if (restart) - s->numRestarts++; - if (fcgi_wrapper) { ap_log_error(FCGI_LOG_WARN_NOERRNO, fcgi_apache_main_server, "FastCGI:%s server \"%s\" (uid %ld, gid %ld) %sstarted (pid %ld)", diff --git a/mod_fastcgi.h b/mod_fastcgi.h index 5b16854..0c1df10 100644 --- a/mod_fastcgi.h +++ b/mod_fastcgi.h @@ -1,5 +1,5 @@ /* - * $Id: mod_fastcgi.h,v 1.40 2002/02/28 15:58:11 robs Exp $ + * $Id: mod_fastcgi.h,v 1.41 2002/02/28 22:52:50 robs Exp $ */ #ifndef MOD_FASTCGI_H @@ -33,6 +33,24 @@ */ #define WIN32_SHUTDOWN_GRACEFUL_WAIT 1000 +/* + * The number of failed starts that can occur before the application is + * considered broken and start attempts fall back to FAILED_STARTS_DELAY. + */ +#define MAX_FAILED_STARTS 3 + +/* + * The number of seconds an application has to have run without exiting + * for the application to be considered no longer broken. + */ +#define RUNTIME_SUCCESS_INTERVAL 30 + +/* + * The number of seconds between attempts to start an application that + * has been declared broken (see MAX_FAILED_STARTS). + */ +#define FAILED_STARTS_DELAY 600 + #define FCGI_DEFAULT_LISTEN_Q 100 /* listen queue (backlog) depth */ #define FCGI_DEFAULT_RESTART_DELAY 5 /* delay between restarts */ #define DEFAULT_INIT_START_DELAY 1 /* delay between starts */ -- 2.11.4.GIT