3

Nginx 工作进程运行示意图

 2 years ago
source link: http://wangxuemin.github.io/2016/01/25/Nginx%20%E5%B7%A5%E4%BD%9C%E8%BF%9B%E7%A8%8B%E8%BF%90%E8%A1%8C%E7%A4%BA%E6%84%8F%E5%9B%BE/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nginx 工作进程运行示意图

发表于 2016-01-25

  |   分类于 nginx

(旧存档丢了, 只剩下一张图 ⊙﹏⊙)

nginx 工作进程运行示意图:

void
ngx_process_events_and_timers(ngx_cycle_t *cycle)
{
ngx_uint_t flags;
ngx_msec_t timer, delta;

if (ngx_timer_resolution) {
timer = NGX_TIMER_INFINITE;
flags = 0;

} else {
timer = ngx_event_find_timer();
flags = NGX_UPDATE_TIME;

}
// accept 互斥锁相关逻辑
if (ngx_use_accept_mutex) {
if (ngx_accept_disabled > 0) {
ngx_accept_disabled--;

} else {
if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
return;
}

if (ngx_accept_mutex_held) {
flags |= NGX_POST_EVENTS;

} else {
if (timer == NGX_TIMER_INFINITE
|| timer > ngx_accept_mutex_delay)
{
timer = ngx_accept_mutex_delay;
}
}
}
}

delta = ngx_current_msec;

// epoll wait 等待socket 读写等事件发生
(void) ngx_process_events(cycle, timer, flags);

delta = ngx_current_msec - delta;

ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "timer delta: %M", delta);

// 优先处理 accept 事件
ngx_event_process_posted(cycle, &ngx_posted_accept_events);

if (ngx_accept_mutex_held) {
ngx_shmtx_unlock(&ngx_accept_mutex);
}

// 处理定时器超时事件
if (delta) {
ngx_event_expire_timers();
}

// 最后处理普通I/O等事件
ngx_event_process_posted(cycle, &ngx_posted_events);
}

static void
ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
{
ngx_int_t worker = (intptr_t) data;

ngx_process = NGX_PROCESS_WORKER;
ngx_worker = worker;

ngx_worker_process_init(cycle, worker);

ngx_setproctitle("worker process");

for ( ;; ) {

if (ngx_exiting) {
ngx_event_cancel_timers();

if (ngx_event_timer_rbtree.root == ngx_event_timer_rbtree.sentinel)
{
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");

ngx_worker_process_exit(cycle);
}
}

ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");

// 工作进程主要处理逻辑
ngx_process_events_and_timers(cycle);

// 信号处理,重启退出等逻辑
if (ngx_terminate) {
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");

ngx_worker_process_exit(cycle);
}

if (ngx_quit) {
ngx_quit = 0;
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
"gracefully shutting down");
ngx_setproctitle("worker process is shutting down");

if (!ngx_exiting) {
ngx_exiting = 1;
ngx_close_listening_sockets(cycle);
ngx_close_idle_connections(cycle);
}
}

if (ngx_reopen) {
ngx_reopen = 0;
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
ngx_reopen_files(cycle, -1);
}
}

转载请注明出处,谢谢。。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK