NGINX 事件(epoll)处理框架源码分析

函数调用

int ngx_cdecl
main(int argc, char *const *argv)
    |
    |___>ngx_single_process_cycle/ngx_master_process_cycle
            |
            |___>ngx_start_worker_processes
                    |
                    |___>ngx_spawn_process
                            |
                            |___>ngx_worker_process_cycle
                                    |
                                    |__>ngx_process_events_and_timers
                                            |
                                            |__>ngx_process_events
                                                ngx_event_actions.process_events

初始化

ngx_event_actions在每个事件模块中是一个全局变量,在事件模块的初始化函数中赋值,以epoll事件为例

static ngx_event_module_t  ngx_epoll_module_ctx = {
    &epoll_name,
    ngx_epoll_create_conf,               /* create configuration */
    ngx_epoll_init_conf,                 /* init configuration */

    {
        ngx_epoll_add_event,             /* add an event */
        ngx_epoll_del_event,             /* delete an event */
        ngx_epoll_add_event,             /* enable an event */
        ngx_epoll_del_event,             /* disable an event */
        ngx_epoll_add_connection,        /* add an connection */
        ngx_epoll_del_connection,        /* delete an connection */
#if (NGX_HAVE_EVENTFD)
        ngx_epoll_notify,                /* trigger a notify */
#else
        NULL,                            /* trigger a notify */
#endif
        ngx_epoll_process_events,        /* process the events */
        ngx_epoll_init,                  /* init the events */
        ngx_epoll_done,                  /* done the events */
    }
};
//ngx_event_actions_t结构中的init函数,在ngx_module_t结构中的init_process函数中调用
static ngx_int_t
ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer){
    
    //ngx_epoll_init还完成了epoll_create与epoll_ctl
    …………
    //这里的action,就是ngx_event_actions
    ngx_event_actions = ngx_epoll_module_ctx.actions;
    …………
}

//ngx_module_t结构中的init_process函数,在模块加载时调用
static ngx_int_t
ngx_event_process_init(ngx_cycle_t *cycle){
    …………
    for (m = 0; cycle->modules[m]; m++) {
        if (cycle->modules[m]->type != NGX_EVENT_MODULE) {
            continue;
        }
        …………
        module = cycle->modules[m]->ctx;
        //ngx_epoll_init
        if (module->actions.init(cycle, ngx_timer_resolution) != NGX_OK) {
            /* fatal */
            exit(2);
        }

        break;
    }
    …………
}

nginx模块结构ngx_module_t中的ctx指针指向事件模块结构ngx_event_module_t,事件模块结构中包含了事件动作结构ngx_event_actions_t

ngx_module_t->ctx
                |
                |__>ngx_event_module_t
                            |
                            |__>actions(ngx_event_actions_t)
                                    |
                                    |__>process_events(ngx_event_actions.process_events)
                      

事件处理

上面调用的process_events(ngx_event_actions.process_events)就是ngx_epoll_process_event函数,该函数处理epoll_wait

static ngx_int_t
ngx_epoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags){
    …………
    //接收事件
    events = epoll_wait(ep, event_list, (int) nevents, timer);
    …………
    //处理事件,包含了功能模块的读和写,比如http模块的读写函数
    for (i = 0; i < events; i++) {
    
    }
}

事件添加

功能模块比如http通过ngx_handle_read_event将事件(包括事件处理函数)添加到events中,

ngx_int_t
ngx_handle_read_event(ngx_event_t *rev, ngx_uint_t flags){

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值