nginx事件模块 -- 第一篇

微信公众号:郑尔多斯
关注可了解更多的Nginx知识。任何问题或建议,请公众号留言;
关注公众号,有趣有内涵的文章第一时间送达!

事件机制

下面是我们对nginx事件相关的配置,如下:

events {
    worker_connections  1024;
    use epoll;
}

我们明确的使用了epoll机制,在nginx中,和事件相关的模块一共有三个,分别为ngx_events_modulengx_event_core_modulengx_epoll_module
本篇文章介绍ngx_events_module模块。

ngx_events_module

该模块是nginx中引入事件机制的模块,我们可以从ngx_events.c文件中找到与ngx_events_module相关的配置,如下:

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
};


static ngx_core_module_t  ngx_events_module_ctx = {
    ngx_string("events"),
    NULL,
    ngx_event_init_conf
};


ngx_module_t  ngx_events_module = {
    NGX_MODULE_V1,
    &ngx_events_module_ctx,                /* module context */
    ngx_events_commands,                   /* module directives */
    NGX_CORE_MODULE,                       /* module type */
    NULL,                                  /* init master */
    NULL,                                  /* init module */
    NULL,                                  /* init process */
    NULL,                                  /* init thread */
    NULL,                                  /* exit thread */
    NULL,                                  /* exit process */
    NULL,                                  /* exit master */
    NGX_MODULE_V1_PADDING
};

typedef struct {
    ngx_str_t             name;
    void               *(*create_conf)(ngx_cycle_t *cycle);
    char               *(*init_conf)(ngx_cycle_t *cycle, void *conf);
} ngx_core_module_t;

从上面的配置中我们可以得到如下信息:

  • ngx_events_module 是一个核心模块 (NGX_CORE_MODULE类型)
  • ngx_events_module 只解析一个命令,即events这个NGX_BLOCK命令,并且不带参数
  • ngx_events_modulecreate_conf()函数为空,它只有init_conf()函数
  • 当遇到events指令的时候,调用 ngx_event_block()函数进行解析处理

解析events指令

我们在前面的文章中介绍过,配置文件的解析是在ngx_init_cycle()函数中完成的。我们再次把这部分代码摘出来,如下:

cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));
    if (cycle->conf_ctx == NULL) {
        ngx_destroy_pool(pool);
        return NULL;
    }

这里分配conf_ctx的内存空间,然后执行如下的代码:

for (i = 0; cycle->modules[i]; i++) {
        if (cycle->modules[i]->type != NGX_CORE_MODULE) {
            continue;
        }

        module = cycle->modules[i]->ctx;

        if (module->create_conf) {
            rv = module->create_conf(cycle);
            if (rv == NULL) {
                ngx_destroy_pool(pool);
                return NULL;
            }
            cycle->conf_ctx[cycle->modules[i]->index] = rv;
        }
    }

遍历所有的NGX_CORE_MODULE类型的模块,调用他们的create_conf()方法,并且赋值给cycle->conf_ctx,上面分析过,ngx_event_module并没有create_conf()方法,所以这部分代码对ngx_event_module没有影响。

ngx_events_block

下面我么分析一下ngx_events_block()函数,这个函数的作用就是解析events指令,代码如下:

static char *
ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    char                 *rv;
    void               ***ctx;
    ngx_uint_t            i;
    ngx_conf_t            pcf;
    ngx_event_module_t   *m;

    if (*(void **) conf) {
        return "is duplicate";
    }

    /* count the number of the event modules and set up their indices */

    ngx_event_max_module = ngx_count_modules(cf->cycle, NGX_EVENT_MODULE);

    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;
            }
        }
    }

    pcf = *cf;
    cf->ctx = ctx;
    cf->module_type = NGX_EVENT_MODULE;
    cf->cmd_type = NGX_EVENT_CONF;

    rv = ngx_conf_parse(cf, NULL);

    *cf = pcf;

    if (rv != NGX_CONF_OK) {
        return rv;
    }

    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->init_conf) {
            rv = m->init_conf(cf->cycle,
                              (*ctx)[cf->cycle->modules[i]->ctx_index]);
            if (rv != NGX_CONF_OK) {
                return rv;
            }
        }
    }

    return NGX_CONF_OK;
}

该函数的执行流程如下:

  • 计算当前有多少个NGX_EVENT_MODULE类型的模块,我们这里的例子中有两个该类型的模块
  • 分配内存空间
  • 调用所有NGX_EVENT_MODULE类型模块的create_conf()方法
  • 递归解析events块指令的内部指令,比如use,worker_connections等指令
  • 调用所有 NGX_EVENT_MODULE类型模块的init_conf()方法

上面就是ngx_events_block()方法的执行流程。这个方法很简单,因为牵涉到ngx_event_core_modulengx_epoll_module,所以下一节我们详细介绍一下这两个事件模块。


喜欢本文的朋友们,欢迎长按下图关注订阅号郑尔多斯,更多精彩内容第一时间送达


郑尔多斯
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容