Index: fpm/fpm_children.c =================================================================== --- fpm/fpm_children.c (revision 303766) +++ fpm/fpm_children.c (working copy) @@ -361,6 +361,12 @@ } else { max = wp->running_children + nb_to_spawn; } + } else if (wp->config->pm == PM_STYLE_ONDEMAND) { + if (!in_event_loop) { /* starting */ + max = 0; /* do not create any child at startup */ + } else { + max = wp->running_children + nb_to_spawn; + } } else { /* PM_STYLE_STATIC */ max = wp->config->pm_max_children; } @@ -411,6 +417,26 @@ int fpm_children_create_initial(struct fpm_worker_pool_s *wp, struct event_base *base) /* {{{ */ { + if (wp->config->pm == PM_STYLE_ONDEMAND) { + wp->socket_event = (struct event *)malloc(sizeof(struct event)); + + if (!wp->socket_event) { + zlog(ZLOG_STUFF, ZLOG_ERROR, "[pool %s] unable to malloc the socket event", wp->config->name); + return 1; + } + + memset(wp->socket_event, 0, sizeof(struct event)); + wp->base = base; + wp->last_fork.tv_sec = 0; + wp->last_fork.tv_usec = 0; + event_set(wp->socket_event, wp->listening_socket, EV_READ, fpm_pctl_on_socket_accept, wp); + event_base_set(base, wp->socket_event); + + wp->socket_event_set = 1; + event_add(wp->socket_event, 0); + + return 1; + } return fpm_children_make(wp, 0 /* not in event loop yet */, 0, 1, base); } /* }}} */ Index: fpm/fpm_conf.c =================================================================== --- fpm/fpm_conf.c (revision 303766) +++ fpm/fpm_conf.c (working copy) @@ -93,6 +93,8 @@ { "pm.start_servers", &fpm_conf_set_integer, offsetof(struct fpm_worker_pool_config_s, pm_start_servers) }, { "pm.min_spare_servers", &fpm_conf_set_integer, offsetof(struct fpm_worker_pool_config_s, pm_min_spare_servers) }, { "pm.max_spare_servers", &fpm_conf_set_integer, offsetof(struct fpm_worker_pool_config_s, pm_max_spare_servers) }, + { "pm.process_idle_timeout", &fpm_conf_set_time, offsetof(struct fpm_worker_pool_config_s, pm_process_idle_timeout) }, + { "pm.min_delay_between_fork", &fpm_conf_set_integer, offsetof(struct fpm_worker_pool_config_s, pm_min_delay_between_fork) }, { "pm.status_path", &fpm_conf_set_string, offsetof(struct fpm_worker_pool_config_s, pm_status_path) }, { "ping.path", &fpm_conf_set_string, offsetof(struct fpm_worker_pool_config_s, ping_path) }, { "ping.response", &fpm_conf_set_string, offsetof(struct fpm_worker_pool_config_s, ping_response) }, @@ -259,8 +261,10 @@ c->pm = PM_STYLE_STATIC; } else if (!strcasecmp(val, "dynamic")) { c->pm = PM_STYLE_DYNAMIC; + } else if (!strcasecmp(val, "ondemand")) { + c->pm = PM_STYLE_ONDEMAND; } else { - return "invalid process manager (static or dynamic)"; + return "invalid process manager (static, dynamic or ondemand)"; } return NULL; } @@ -323,6 +327,8 @@ memset(wp->config, 0, sizeof(struct fpm_worker_pool_config_s)); wp->config->listen_backlog = FPM_BACKLOG_DEFAULT; + wp->config->pm_process_idle_timeout = 10; /* 10s by default */ + wp->config->pm_min_delay_between_fork = 100; /* 100µs by default */ if (!fpm_worker_all_pools) { fpm_worker_all_pools = wp; @@ -430,8 +436,8 @@ return -1; } - if (wp->config->pm != PM_STYLE_STATIC && wp->config->pm != PM_STYLE_DYNAMIC) { - zlog(ZLOG_STUFF, ZLOG_ALERT, "[pool %s] the process manager is missing (static or dynamic)", wp->config->name); + if (wp->config->pm != PM_STYLE_STATIC && wp->config->pm != PM_STYLE_DYNAMIC && wp->config->pm != PM_STYLE_ONDEMAND) { + zlog(ZLOG_STUFF, ZLOG_ALERT, "[pool %s] the process manager is missing (static, dynamic or ondemand)", wp->config->name); return -1; } @@ -472,7 +478,28 @@ zlog(ZLOG_STUFF, ZLOG_ALERT, "[pool %s] pm.start_servers(%d) must not be less than pm.min_spare_servers(%d) and not greater than pm.max_spare_servers(%d)", wp->config->name, config->pm_start_servers, config->pm_min_spare_servers, config->pm_max_spare_servers); return -1; } + } else if (wp->config->pm == PM_STYLE_ONDEMAND) { + struct fpm_worker_pool_config_s *config = wp->config; + if (config->pm_process_idle_timeout < 1) { + zlog(ZLOG_STUFF, ZLOG_ALERT, "[pool %s] pm.process_idle_timeout(%ds) must be greater than 0s", wp->config->name, config->pm_process_idle_timeout); + return -1; + } + + if (config->listen_backlog < FPM_BACKLOG_DEFAULT) { + zlog(ZLOG_STUFF, ZLOG_WARNING, "[pool %s] listen.backlog(%d) was too low for the ondemand process manager. I updated it for you to %d.", wp->config->name, config->listen_backlog, FPM_BACKLOG_DEFAULT); + config->listen_backlog = FPM_BACKLOG_DEFAULT; + } + + if (config->pm_min_delay_between_fork < 1 || config->pm_min_delay_between_fork > 999999) { + zlog(ZLOG_STUFF, ZLOG_ALERT, "[pool %s] pm.min_delay_between_fork(%d) must be greater than 0µs and less than 1000000µs", wp->config->name, config->pm_min_delay_between_fork); + return -1; + } + + /* certainely useless but proper */ + config->pm_start_servers = 0; + config->pm_min_spare_servers = 0; + config->pm_max_spare_servers = 0; } Index: fpm/fpm_conf.h =================================================================== --- fpm/fpm_conf.h (revision 303766) +++ fpm/fpm_conf.h (working copy) @@ -48,6 +48,8 @@ int pm_start_servers; int pm_min_spare_servers; int pm_max_spare_servers; + int pm_process_idle_timeout; + int pm_min_delay_between_fork; char *ping_path; char *ping_response; char *listen_address; @@ -67,7 +69,7 @@ intptr_t offset; }; -enum { PM_STYLE_STATIC = 1, PM_STYLE_DYNAMIC = 2 }; +enum { PM_STYLE_STATIC = 1, PM_STYLE_DYNAMIC = 2, PM_STYLE_ONDEMAND = 3 }; int fpm_conf_init_main(); int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc); Index: fpm/fpm_process_ctl.c =================================================================== --- fpm/fpm_process_ctl.c (revision 303766) +++ fpm/fpm_process_ctl.c (working copy) @@ -351,6 +351,29 @@ /* update status structure for all PMs */ fpm_status_update_activity(wp->shm_status, idle, active, idle + active, 0); + /* this is specific to PM_STYLE_ONDEMAND */ + if (wp->config->pm == PM_STYLE_ONDEMAND) { + struct timeval last, now; + + zlog(ZLOG_STUFF, ZLOG_DEBUG, "[pool %s] currently %d active children, %d spare children", wp->config->name, active, idle); + + if (!wp->socket_event_set) { + wp->socket_event_set = 1; + event_add(wp->socket_event, 0); + } + + if (!last_idle_child) continue; + + fpm_request_last_activity(last_idle_child, &last); + fpm_clock_get(&now); + if (last.tv_sec < now.tv_sec - wp->config->pm_process_idle_timeout) { + last_idle_child->idle_kill = 1; + fpm_pctl_kill(last_idle_child->pid, FPM_PCTL_TERM); + } + + continue; + } + /* the rest is only used by PM_STYLE_DYNAMIC */ if (wp->config->pm != PM_STYLE_DYNAMIC) continue; @@ -463,3 +486,64 @@ } /* }}} */ +void fpm_pctl_on_socket_accept(int fd, short which, void *arg) /* {{{ */ +{ + struct fpm_worker_pool_s *wp = (struct fpm_worker_pool_s *)arg; + struct fpm_child_s *child; + struct timeval now, diff; + + + if (fpm_globals.parent_pid != getpid()) { + /* prevent a event race condition when child process + * have not set up its own event loop */ + return; + } + + wp->socket_event_set = 0; + + zlog(ZLOG_STUFF, ZLOG_DEBUG, "[pool %s] fpm_pctl_on_socket_accept() called", wp->config->name); + + if (wp->running_children >= wp->config->pm_max_children) { + if (!wp->warn_max_children) { + fpm_status_increment_max_children_reached(wp->shm_status); + zlog(ZLOG_STUFF, ZLOG_WARNING, "[pool %s] server reached max_children setting (%d), consider raising it", wp->config->name, wp->config->pm_max_children); + wp->warn_max_children = 1; + } + + return; + } + + fpm_clock_get(&now); + timersub(&now, &wp->last_fork, &diff); + // if we forked in the last pm_min_delay_between_fork µs, do nothing + if (diff.tv_sec == 0 && diff.tv_usec < wp->config->pm_min_delay_between_fork) { + goto setevent; + } + wp->last_fork.tv_sec = 0; + wp->last_fork.tv_usec = 0; + + for (child = wp->children; child; child = child->next) { + /* if there is at least on idle child, it will handle the connection, stop here */ + if (fpm_request_is_idle(child)) { + goto setevent; + } + } + + wp->warn_max_children = 0; + fpm_clock_get(&wp->last_fork); + fpm_children_make(wp, 1, 1, 1, wp->base); + + if (fpm_globals.is_child) { + return; + } + + zlog(ZLOG_STUFF, ZLOG_DEBUG, "[pool %s] got accept without idle child available .... I forked, now=%d.%d", wp->config->name, (int)now.tv_sec, (int)now.tv_usec); + +/* reschedule the event */ +setevent: + if (!wp->socket_event_set) { + wp->socket_event_set = 1; + event_add(wp->socket_event, 0); + } +} +/* }}} */ Index: fpm/fpm_process_ctl.h =================================================================== --- fpm/fpm_process_ctl.h (revision 303766) +++ fpm/fpm_process_ctl.h (working copy) @@ -17,6 +17,7 @@ int fpm_pctl_kill(pid_t pid, int how); void fpm_pctl_heartbeat(int fd, short which, void *arg); void fpm_pctl_perform_idle_server_maintenance_heartbeat(int fd, short which, void *arg); +void fpm_pctl_on_socket_accept(int fd, short which, void *arg); int fpm_pctl_child_exited(); int fpm_pctl_init_main(); Index: fpm/fpm_request.c =================================================================== --- fpm/fpm_request.c (revision 303766) +++ fpm/fpm_request.c (working copy) @@ -167,3 +167,23 @@ return(!slot_c.accepted.tv_sec && !slot_c.accepted.tv_usec ? 1 : 0); } /* }}} */ + +int fpm_request_last_activity(struct fpm_child_s *child, struct timeval *tv) /* {{{ */ +{ + struct fpm_shm_slot_s *slot; + struct fpm_shm_slot_s slot_c; + + if (!tv) return -1; + + slot = fpm_shm_slot(child); + if (!fpm_shm_slots_acquire(slot, 1)) { + return -1; + } + + slot_c = *slot; + fpm_shm_slots_release(slot); + *tv = slot_c.tv; + + return 1; +} +/* }}} */ Index: fpm/fpm_request.h =================================================================== --- fpm/fpm_request.h (revision 303766) +++ fpm/fpm_request.h (working copy) @@ -16,6 +16,7 @@ void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *tv, int terminate_timeout, int slowlog_timeout); int fpm_request_is_idle(struct fpm_child_s *child); +int fpm_request_last_activity(struct fpm_child_s *child, struct timeval *tv); enum fpm_request_stage_e { FPM_REQUEST_ACCEPTING = 1, Index: fpm/fpm_status.c =================================================================== --- fpm/fpm_status.c (revision 303766) +++ fpm/fpm_status.c (working copy) @@ -171,7 +171,7 @@ "active processes: %d\n" "total processes: %d\n" "max children reached: %u\n", - status->accepted_conn, fpm_status_pool, status->pm == PM_STYLE_STATIC ? "static" : "dynamic", status->idle, status->active, status->total, status->max_children_reached); + status->accepted_conn, fpm_status_pool, status->pm == PM_STYLE_STATIC ? "static" : (status->pm == PM_STYLE_DYNAMIC ? "dynamic" : "ondemand"), status->idle, status->active, status->total, status->max_children_reached); spprintf(content_type, 0, "Content-Type: text/plain"); } @@ -193,7 +193,7 @@ "