微信公众号:郑尔多斯
关注可了解更多的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
等指令设置相关的事件参数。
下面我们看一下这个模块的源码:
static ngx_event_module_t ngx_event_core_module_ctx = {
&event_core_name,
ngx_event_core_create_conf, /* create configuration */
ngx_event_core_init_conf, /* init configuration */
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
};
从源码中我们可以知道对应的钩子函数分别为ngx_event_core_core_conf()
和 ngx_event_core_init_conf()
。我们逐个分析:
static void *
ngx_event_core_create_conf(ngx_cycle_t *cycle)
{
ngx_event_conf_t *ecf;
ecf = ngx_palloc(cycle->pool, sizeof(ngx_event_conf_t));
if (ecf == NULL) {
return NULL;
}
ecf->connections = NGX_CONF_UNSET_UINT;
ecf->use = NGX_CONF_UNSET_UINT;
ecf->multi_accept = NGX_CONF_UNSET;
ecf->accept_mutex = NGX_CONF_UNSET;
ecf->accept_mutex_delay = NGX_CONF_UNSET_MSEC;
ecf->name = (void *) NGX_CONF_UNSET;
#if (NGX_DEBUG)
if (ngx_array_init(&ecf->debug_connection, cycle->pool, 4,
sizeof(ngx_cidr_t)) == NGX_ERROR)
{
return NULL;
}
#endif
return ecf;
}
这个代码真的是很简单了,没有什么可分析的,功能如下:
生成一个
ngx_event_conf_t
结构体,然后将该结构体的所有字段都赋予一个默认值
ngx_epoll_module模块
我们再看一下ngx_epoll_module
的源码,结合起来分析:
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_epoll_module
的钩子函数为ngx_epoll_create_conf()
,这个函数的源码非常非常非常简单,这里不再分析,作用也很简单,就是创建一个ngx_epoll_conf_t
结构体,并对结构体的字段进行赋初值。
解析use字段
我们观察配置文件中和event
相关的内容,如下:
events {
worker_connections 1024;
use epoll;
}
配置文件比较简单,符合我们的一贯原则,越简单越容易分析,哈哈。
我们从源码中找到处理use
字段的内容,看下面?
{ ngx_string("use"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_event_use,
0,
0,
NULL },
从中我们可以知道,处理use
字段的处理函数为ngx_event_use()
,下面我们分析一下这个处理函数,我们删除了部分用于健壮性判断语句,以及一些调试语句。
static char *
ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_event_conf_t *ecf = conf;
ngx_int_t m;
ngx_str_t *value;
ngx_event_conf_t *old_ecf;
ngx_event_module_t *module;
// 防止出现多个use配置指令
if (ecf->use != NGX_CONF_UNSET_UINT) {
return "is duplicate";
}
value = cf->args->elts;
if (cf->cycle->old_cycle->conf_ctx) {
old_ecf = ngx_event_get_conf(cf->cycle->old_cycle->conf_ctx,
ngx_event_core_module);
} else {
old_ecf = NULL;
}
//遍历所有的`NGX_EVENT_MODULE`模块
for (m = 0; cf->cycle->modules[m]; m++) {
if (cf->cycle->modules[m]->type != NGX_EVENT_MODULE) {
continue;
}
module = cf->cycle->modules[m]->ctx;
if (module->name->len == value[1].len) {
if (ngx_strcmp(module->name->data, value[1].data) == 0) {
ecf->use = cf->cycle->modules[m]->ctx_index;
ecf->name = module->name->data;
return NGX_CONF_OK;
}
}
}
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid event type \"%V\"", &value[1]);
return NGX_CONF_ERROR;
}
从代码中我们可以得到如下结论:
ngx_event_conf_t
结构体的use
字段标识我们选择的事件模块的ctx_index
,在这里就是ngx_epoll_module
的ctx_index
字段,也即是1
ngx_event_conf_t
结构体的name
字段标识我们选择的事件模块的name
,在这里就是epoll
解析worker_connections字段
同理,我们先从源码中找到worker_connections
有关的代码:
{ ngx_string("worker_connections"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_event_connections,
0,
0,
NULL }
显然,处理函数为ngx_event_connections()
,如下:
ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_event_conf_t *ecf = conf;
ngx_str_t *value;
if (ecf->connections != NGX_CONF_UNSET_UINT) {
return "is duplicate";
}
value = cf->args->elts;
ecf->connections = ngx_atoi(value[1].data, value[1].len);
if (ecf->connections == (ngx_uint_t) NGX_ERROR) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid number \"%V\"", &value[1]);
return NGX_CONF_ERROR;
}
cf->cycle->connection_n = ecf->connections;
return NGX_CONF_OK;
}
函数太简单,没法分析,就是为ngx_event_conf_t
结构体的connections
字段赋值。这里就是1024.
调用init_conf()
从上面的代码中可以知道,ngx_event_core_module
的infi_conf
钩子函数为ngx_event_core_init_conf
,并且ngx_epoll_module
的init_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()
不应该是Linux
的API
吗?如果这里有epoll_create()
的定义,那么后续调用的epoll_create()
应该是Linux
的API
还是这里的epoll_create()
?
总结
以一张图总结一下event
的初始化以及配置文件的解析过程,
喜欢本文的朋友们,欢迎长按下图关注订阅号郑尔多斯,更多精彩内容第一时间送达