ngx_event_core_module模块分析
event_core模块是第一个事件类型模块,它的主要功能是负责解析事件类配置项,选择事件处理机制(select、poll、epoll还是kqueue机制),创建连接池,预分配读写事件池等。
event_core模块需处理那些事件类配置项呢?下面是其ngx_command_t结构体:
static ngx_command_t ngx_event_core_commands[] = {
{
/***控制worker进程连接数大小,即每worker进程中支持的TCP最大连接数,默认为512***/
ngx_string("worker_connections"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_event_connections,
0,
0,
NULL },
/***该配置项与worker_connections相同,用于控制连接数,此配置项已过时***/
{ ngx_string("connections"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_event_connections,
0,
0,
NULL },
/***指定使用那个事件模型(epoll、select、kqueue等等)模块来处理事件,默认由nginx在configure时根据配置选项决定***/
{ ngx_string("use"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_event_use,
0,
0,
NULL },
/***用于设置尽可能多的接收连接,即在某监听套接字上接收到新连接时循环调用accept接收
所有新连接,默认关闭***/
{ ngx_string("multi_accept"),
NGX_EVENT_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
0,
offsetof(ngx_event_conf_t, multi_accept),
NULL },
/***是否开启worker进程接收连接的负载均衡锁,默认开启***/
{ ngx_string("accept_mutex"),
NGX_EVENT_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
0,
offsetof(ngx_event_conf_t, accept_mutex),
NULL },
/***启用accept_mutex负载均衡锁后,延迟accept_mutex_delay秒后再次试图接收连接,默认500ms***/
{ ngx_string("accept_mutex_delay"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
0,
offsetof(ngx_event_conf_t, accept_mutex_delay),
NULL },
/***对指定IP地址的连接打开debug级别日志,此项功能需在configure时增加--with-debug选项***/
{ ngx_string("debug_connection"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_event_debug_connection,
0,
0,
NULL },
ngx_null_command
};
event_core模块配置项的结构体如下:
typedef struct {
ngx_uint_t connections; //对应worker_connections或connections配置项
ngx_uint_t use; //对应use配置项值
ngx_flag_t multi_accept; //对应multi_accept配置项
ngx_flag_t accept_mutex; //对应accept_mutex配置项
ngx_msec_t accept_mutex_delay; //对应accept_mutex_delay配置项
u_char *name; //与use配置项一起使用,保存use配置项指定模块的名称
#if (NGX_DEBUG)
ngx_array_t debug_connection; //对应debug_connection配置项
#endif
} ngx_event_conf_t;
event_core模块上下文结构体:
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_create_conf函数在配置项解析前调用分配保存配置项的内存空间,ngx_event_core_init_conf函数在配置项解析完后调用对配置文件中不存在的事件配置项进行初始化。
event_core模块的定义:
ngx_module_t ngx_event_core_module = {
NGX_MODULE_V1,
&ngx_event_core_module_ctx, /* module context */
ngx_event_core_commands, /* module directives */
NGX_EVENT_MODULE, /* module type */
NULL, /* init master */
ngx_event_module_init, /* init module */
ngx_event_process_init,