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;
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章