Nginx事件模块init

首先在nginx里面最重的是下述三个模块

ngx_events_module,
ngx_event_core_module,
ngx_epoll_module,
ngx_events_module

这个模块属于core_module在程序起来的时候就init, 它的commands成员只有一个,也就是说只关注配置文件中events这一标签。

static ngx_command_t  ngx_events_commands[] = {
      { ngx_string("events"),
      NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
      ngx_events_block,
      0,
      0,
      NULL },

      ngx_null_command
};

nginx在关注了这一标签之后会执行ngx_events_block函数的代码设置,ngx_events_block主要的工作有

ngx_event_max_module = ngx_count_modules(cf->cycle, NGX_EVENT_MODULE);
//遍历nginx里面的全局数组modules,如果类型为NGX_EVENT_MODULE,则设置ctx_index, 
//计算出当前有多 少个event module

下面分别调用event module的create conf和init conf
值得注意的是(*ctx)[x] 指向的是具体的模块

    void               ***ctx;
    ctx = ngx_pcalloc(cf->pool, sizeof(void *));

    if (ctx == NULL) {
        return NGX_CONF_ERROR;
    }
    *ctx = ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *));
    if (*ctx == NULL) {
        return NGX_CONF_ERROR;
    }

  *(void **) conf = ctx;
    for (i = 0; cf->cycle->modules[i]; i++) {
        if (cf->cycle->modules[i]->type != NGX_EVENT_MODULE) {
            continue;
        }

        m = cf->cycle->modules[i]->ctx;

        if (m->create_conf) {
            (*ctx)[cf->cycle->modules[i]->ctx_index] =
                                                     m->create_conf(cf->cycle);
            if ((*ctx)[cf->cycle->modules[i]->ctx_index] == NULL) {
                return NGX_CONF_ERROR;
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章