Klaus Demo nginx / 12b9974
Introduced worker_shutdown_timeout. The directive configures a timeout to be used when gracefully shutting down worker processes. When the timer expires, nginx will try to close all the connections currently open to facilitate shutdown. Maxim Dounin 5 years ago
5 changed file(s) with 66 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
121121 ngx_conf_set_off_slot,
122122 0,
123123 offsetof(ngx_core_conf_t, rlimit_core),
124 NULL },
125
126 { ngx_string("worker_shutdown_timeout"),
127 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
128 ngx_conf_set_msec_slot,
129 0,
130 offsetof(ngx_core_conf_t, shutdown_timeout),
124131 NULL },
125132
126133 { ngx_string("working_directory"),
10131020 ccf->daemon = NGX_CONF_UNSET;
10141021 ccf->master = NGX_CONF_UNSET;
10151022 ccf->timer_resolution = NGX_CONF_UNSET_MSEC;
1023 ccf->shutdown_timeout = NGX_CONF_UNSET_MSEC;
10161024
10171025 ccf->worker_processes = NGX_CONF_UNSET;
10181026 ccf->debug_points = NGX_CONF_UNSET;
10411049 ngx_conf_init_value(ccf->daemon, 1);
10421050 ngx_conf_init_value(ccf->master, 1);
10431051 ngx_conf_init_msec_value(ccf->timer_resolution, 0);
1052 ngx_conf_init_msec_value(ccf->shutdown_timeout, 0);
10441053
10451054 ngx_conf_init_value(ccf->worker_processes, 1);
10461055 ngx_conf_init_value(ccf->debug_points, 0);
1414 ngx_shm_zone_t *shm_zone);
1515 static ngx_int_t ngx_test_lockfile(u_char *file, ngx_log_t *log);
1616 static void ngx_clean_old_cycles(ngx_event_t *ev);
17 static void ngx_shutdown_timer_handler(ngx_event_t *ev);
1718
1819
1920 volatile ngx_cycle_t *ngx_cycle;
2122
2223 static ngx_pool_t *ngx_temp_pool;
2324 static ngx_event_t ngx_cleaner_event;
25 static ngx_event_t ngx_shutdown_event;
2426
2527 ngx_uint_t ngx_test_config;
2628 ngx_uint_t ngx_dump_config;
13321334 ngx_old_cycles.nelts = 0;
13331335 }
13341336 }
1337
1338
1339 void
1340 ngx_set_shutdown_timer(ngx_cycle_t *cycle)
1341 {
1342 ngx_core_conf_t *ccf;
1343
1344 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
1345
1346 if (ccf->shutdown_timeout) {
1347 ngx_shutdown_event.handler = ngx_shutdown_timer_handler;
1348 ngx_shutdown_event.data = cycle;
1349 ngx_shutdown_event.log = cycle->log;
1350 ngx_shutdown_event.cancelable = 1;
1351
1352 ngx_add_timer(&ngx_shutdown_event, ccf->shutdown_timeout);
1353 }
1354 }
1355
1356
1357 static void
1358 ngx_shutdown_timer_handler(ngx_event_t *ev)
1359 {
1360 ngx_uint_t i;
1361 ngx_cycle_t *cycle;
1362 ngx_connection_t *c;
1363
1364 cycle = ev->data;
1365
1366 c = cycle->connections;
1367
1368 for (i = 0; i < cycle->connection_n; i++) {
1369
1370 if (c[i].fd == (ngx_socket_t) -1
1371 || c[i].read == NULL
1372 || c[i].read->accept
1373 || c[i].read->channel
1374 || c[i].read->resolver)
1375 {
1376 continue;
1377 }
1378
1379 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,
1380 "*%uA shutdown timeout", c[i].number);
1381
1382 c[i].close = 1;
1383 c[i].error = 1;
1384
1385 c[i].read->handler(c[i].read);
1386 }
1387 }
8787 ngx_flag_t master;
8888
8989 ngx_msec_t timer_resolution;
90 ngx_msec_t shutdown_timeout;
9091
9192 ngx_int_t worker_processes;
9293 ngx_int_t debug_points;
128129 ngx_cpuset_t *ngx_get_cpu_affinity(ngx_uint_t n);
129130 ngx_shm_zone_t *ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name,
130131 size_t size, void *tag);
132 void ngx_set_shutdown_timer(ngx_cycle_t *cycle);
131133
132134
133135 extern volatile ngx_cycle_t *ngx_cycle;
762762
763763 if (!ngx_exiting) {
764764 ngx_exiting = 1;
765 ngx_set_shutdown_timer(cycle);
765766 ngx_close_listening_sockets(cycle);
766767 ngx_close_idle_connections(cycle);
767768 }
799799
800800 if (!ngx_exiting) {
801801 ngx_exiting = 1;
802 ngx_set_shutdown_timer(cycle);
802803 ngx_close_listening_sockets(cycle);
803804 ngx_close_idle_connections(cycle);
804805 }