• nginx事件模块 第二篇


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

    事件机制

    上一篇文件我们简单的介绍了ngx_event_block()函数的功能,这个函数用于解析events指令,引入事件机制。其实真正的工作是在ngx_event_core_module中完成的,这个模块可以解析use,work_connections等指令,这些指令用于控制nginx事件机制的一些参数。
    上一篇文章中我们也提到过执行ngx_event_block()函数的时候会遍历所有的NGX_EVENT_MODULE类型的模块,然后调用他们的create_conf()方法,然后解析events指令,解析完毕之后会调用所有NGX_EVENT_MODULE类型的模块的init_conf()方法。这一片文章我们就分析一下这一过程。

    ngx_event_core_module模块

    这个模块是非常重要的,它是第一个NGX_EVENT_MODULE类型的模块,它真正的引入了事件机制。我们可以通过use指令选择我们将要使用的事件模型。使用worker_connections等指令设置相关的事件参数。
    下面我们看一下这个模块的源码:

    1static ngx_event_module_t  ngx_event_core_module_ctx = {
    2    &event_core_name,
    3    ngx_event_core_create_conf,            /* create configuration */
    4    ngx_event_core_init_conf,              /* init configuration */
    5
    6    { NULLNULLNULLNULLNULLNULLNULLNULLNULLNULL }
    7};

    从源码中我们可以知道对应的钩子函数分别为ngx_event_core_core_conf()ngx_event_core_init_conf()。我们逐个分析:

     1static void *
    2ngx_event_core_create_conf(ngx_cycle_t *cycle)
    3
    {
    4    ngx_event_conf_t  *ecf;
    5
    6    ecf = ngx_palloc(cycle->pool, sizeof(ngx_event_conf_t));
    7    if (ecf == NULL) {
    8        return NULL;
    9    }
    10
    11    ecf->connections = NGX_CONF_UNSET_UINT;
    12    ecf->use = NGX_CONF_UNSET_UINT;
    13    ecf->multi_accept = NGX_CONF_UNSET;
    14    ecf->accept_mutex = NGX_CONF_UNSET;
    15    ecf->accept_mutex_delay = NGX_CONF_UNSET_MSEC;
    16    ecf->name = (void *) NGX_CONF_UNSET;
    17
    18#if (NGX_DEBUG)
    19
    20    if (ngx_array_init(&ecf->debug_connection, cycle->pool, 4,
    21                       sizeof(ngx_cidr_t)) == NGX_ERROR)
    22    {
    23        return NULL;
    24    }
    25
    26#endif
    27
    28    return ecf;
    29}

    这个代码真的是很简单了,没有什么可分析的,功能如下:

    生成一个ngx_event_conf_t结构体,然后将该结构体的所有字段都赋予一个默认值

    ngx_epoll_module模块

    我们再看一下ngx_epoll_module的源码,结合起来分析:

     1static ngx_event_module_t  ngx_epoll_module_ctx = {
    2    &epoll_name,
    3    ngx_epoll_create_conf,               /* create configuration */
    4    ngx_epoll_init_conf,                 /* init configuration */
    5
    6    {
    7        ngx_epoll_add_event,             /* add an event */
    8        ngx_epoll_del_event,             /* delete an event */
    9        ngx_epoll_add_event,             /* enable an event */
    10        ngx_epoll_del_event,             /* disable an event */
    11        ngx_epoll_add_connection,        /* add an connection */
    12        ngx_epoll_del_connection,        /* delete an connection */
    13#if (NGX_HAVE_EVENTFD)
    14        ngx_epoll_notify,                /* trigger a notify */
    15#else
    16        NULL,                            /* trigger a notify */
    17#endif
    18        ngx_epoll_process_events,        /* process the events */
    19        ngx_epoll_init,                  /* init the events */
    20        ngx_epoll_done,                  /* done the events */
    21    }
    22};

    ngx_epoll_module的钩子函数为ngx_epoll_create_conf(),这个函数的源码非常非常非常简单,这里不再分析,作用也很简单,就是创建一个ngx_epoll_conf_t结构体,并对结构体的字段进行赋初值。

    解析use字段

    我们观察配置文件中和event相关的内容,如下:

    1events {
    2    worker_connections  1024;
    3    use epoll;
    4}

    配置文件比较简单,符合我们的一贯原则,越简单越容易分析,哈哈。
    我们从源码中找到处理use字段的内容,看下面

    1 { ngx_string("use"),
    2      NGX_EVENT_CONF|NGX_CONF_TAKE1,
    3      ngx_event_use,
    4      0,
    5      0,
    6      NULL },

    从中我们可以知道,处理use字段的处理函数为ngx_event_use(),下面我们分析一下这个处理函数,我们删除了部分用于健壮性判断语句,以及一些调试语句。

     1static char *
    2ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
    3
    {
    4    ngx_event_conf_t  *ecf = conf;
    5
    6    ngx_int_t             m;
    7    ngx_str_t            *value;
    8    ngx_event_conf_t     *old_ecf;
    9    ngx_event_module_t   *module;
    10
    11// 防止出现多个use配置指令
    12    if (ecf->use != NGX_CONF_UNSET_UINT) {
    13        return "is duplicate";
    14    }
    15
    16    value = cf->args->elts;
    17
    18    if (cf->cycle->old_cycle->conf_ctx) {
    19        old_ecf = ngx_event_get_conf(cf->cycle->old_cycle->conf_ctx,
    20                                     ngx_event_core_module);
    21    } else {
    22        old_ecf = NULL;
    23    }
    24
    25//遍历所有的`NGX_EVENT_MODULE`模块
    26    for (m = 0; cf->cycle->modules[m]; m++) {
    27        if (cf->cycle->modules[m]->type != NGX_EVENT_MODULE) {
    28            continue;
    29        }
    30
    31        module = cf->cycle->modules[m]->ctx;
    32        if (module->name->len == value[1].len) {
    33            if (ngx_strcmp(module->name->data, value[1].data) == 0) {
    34                ecf->use = cf->cycle->modules[m]->ctx_index;
    35                ecf->name = module->name->data;
    36                return NGX_CONF_OK;
    37            }
    38        }
    39    }
    40
    41    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    42                       "invalid event type \"%V\"", &value[1]);
    43
    44    return NGX_CONF_ERROR;
    45}

    从代码中我们可以得到如下结论:

    • ngx_event_conf_t结构体的use字段标识我们选择的事件模块的ctx_index,在这里就是ngx_epoll_modulectx_index字段,也即是1
    • ngx_event_conf_t结构体的name字段标识我们选择的事件模块的name,在这里就是epoll

    解析worker_connections字段

    同理,我们先从源码中找到worker_connections有关的代码:

    1{ ngx_string("worker_connections"),
    2      NGX_EVENT_CONF|NGX_CONF_TAKE1,
    3      ngx_event_connections,
    4      0,
    5      0,
    6      NULL }

    显然,处理函数为ngx_event_connections(),如下:

     1ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
    2{
    3    ngx_event_conf_t  *ecf = conf;
    4
    5    ngx_str_t  *value;
    6
    7    if (ecf->connections != NGX_CONF_UNSET_UINT) {
    8        return "is duplicate";
    9    }
    10
    11    value = cf->args->elts;
    12    ecf->connections = ngx_atoi(value[1].data, value[1].len);
    13    if (ecf->connections == (ngx_uint_t) NGX_ERROR) {
    14        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    15                           "invalid number \"%V\"", &value[1]);
    16
    17        return NGX_CONF_ERROR;
    18    }
    19
    20    cf->cycle->connection_n = ecf->connections;
    21
    22    return NGX_CONF_OK;
    23}

    函数太简单,没法分析,就是为ngx_event_conf_t结构体的connections字段赋值。这里就是1024.

    调用init_conf()

    从上面的代码中可以知道,ngx_event_core_moduleinfi_conf钩子函数为ngx_event_core_init_conf,并且ngx_epoll_moduleinit_conf钩子函数为ngx_epoll_init_conf函数。这两个函数都很简单,但是ngx_event_core_init_conf函数我没有看明白是什么意思,这个可能后面还要查证一下,暂定。

    待解决问题

    在分析的过程中,有下面两个问题没有搞清楚,要待查证:

    • ngx_event_core_init_conf()函数有什么作用?为什么在配置文件已经解析之后还要对ngx_event_conf_t结构体中的一些字段重新赋值?如果这样的话,配置文件中的相同配置有什么作用?
    • ngx_epoll_module中的epoll_create()函数为什么直接return -1;?epoll_create()不应该是LinuxAPI吗?如果这里有epoll_create()的定义,那么后续调用的epoll_create()应该是LinuxAPI还是这里的epoll_create()

    总结

    以一张图总结一下event的初始化以及配置文件的解析过程,

    event解析event解析

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

    郑尔多斯郑尔多斯
  • 相关阅读:
    HTTP 和 HTTPS
    HTTP 协议
    基础查询
    python编程从入门到实践笔记
    python-32-类的组合与初识继承
    python-31-初识面向对象与自定义类
    python-30-异常处理
    python-29-模块与包导入
    python-28-序列化模块
    python-27-其他常用模块(二)
  • 原文地址:https://www.cnblogs.com/zhengerduosi/p/10123365.html
Copyright © 2020-2023  润新知