comparison src/os/unix/ngx_process_cycle.c @ 6220:5e6142609e48

Core: idle connections now closed only once on exiting. Iterating through all connections takes a lot of CPU time, especially with large number of worker connections configured. As a result nginx processes used to consume CPU time during graceful shutdown. To mitigate this we now only do a full scan for idle connections when shutdown signal is received. Transitions of connections to idle ones are now expected to be avoided if the ngx_exiting flag is set. The upstream keepalive module was modified to follow this.
author Valentin Bartenev <vbart@nginx.com>
date Tue, 11 Aug 2015 16:28:55 +0300
parents b4cc553aafeb
children cf5e822cf470
comparison
equal deleted inserted replaced
6219:808fd1f0b94b 6220:5e6142609e48
726 static void 726 static void
727 ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data) 727 ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
728 { 728 {
729 ngx_int_t worker = (intptr_t) data; 729 ngx_int_t worker = (intptr_t) data;
730 730
731 ngx_uint_t i;
732 ngx_connection_t *c;
733
734 ngx_process = NGX_PROCESS_WORKER; 731 ngx_process = NGX_PROCESS_WORKER;
735 ngx_worker = worker; 732 ngx_worker = worker;
736 733
737 ngx_worker_process_init(cycle, worker); 734 ngx_worker_process_init(cycle, worker);
738 735
739 ngx_setproctitle("worker process"); 736 ngx_setproctitle("worker process");
740 737
741 for ( ;; ) { 738 for ( ;; ) {
742 739
743 if (ngx_exiting) { 740 if (ngx_exiting) {
744
745 c = cycle->connections;
746
747 for (i = 0; i < cycle->connection_n; i++) {
748
749 /* THREAD: lock */
750
751 if (c[i].fd != -1 && c[i].idle) {
752 c[i].close = 1;
753 c[i].read->handler(c[i].read);
754 }
755 }
756
757 ngx_event_cancel_timers(); 741 ngx_event_cancel_timers();
758 742
759 if (ngx_event_timer_rbtree.root == ngx_event_timer_rbtree.sentinel) 743 if (ngx_event_timer_rbtree.root == ngx_event_timer_rbtree.sentinel)
760 { 744 {
761 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting"); 745 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
779 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, 763 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
780 "gracefully shutting down"); 764 "gracefully shutting down");
781 ngx_setproctitle("worker process is shutting down"); 765 ngx_setproctitle("worker process is shutting down");
782 766
783 if (!ngx_exiting) { 767 if (!ngx_exiting) {
768 ngx_exiting = 1;
784 ngx_close_listening_sockets(cycle); 769 ngx_close_listening_sockets(cycle);
785 ngx_exiting = 1; 770 ngx_close_idle_connections(cycle);
786 } 771 }
787 } 772 }
788 773
789 if (ngx_reopen) { 774 if (ngx_reopen) {
790 ngx_reopen = 0; 775 ngx_reopen = 0;