cmd->set ngx_event_connections
定义在
src\event\ngx_event.c
static char *
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_connections
是 Nginx 中处理 connections
配置指令的回调函数,主要用于:
-
解析并验证配置值 :将用户配置的字符串形式的连接数转换为整数,并检查其合法性。
-
防止重复配置 :确保
connections
指令在配置文件中仅被设置一次。 -
设置全局连接数限制 :将解析后的最大连接数保存到 Nginx 的核心结构中,用于后续资源分配
static char *
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";
}
ngx_event_conf_t-CSDN博客
NGX_CONF_UNSET_UINT
是 Nginx 的特殊标记,表示该配置未被设置。若
connections
已被设置过,直接返回错误信息"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;
}
获取 之前读取的 token, 第二个也就是value[1] 是当前指令的参数
把它从字符串转换成为 整数 然后设置给 ecf->connections
此时
ecf->connections=1024
cf->cycle->connection_n = ecf->connections;
事件模块(
ngx_event_module
)通过解析用户配置得到ecf->connections
,但其他核心模块(如连接池管理、进程管理)需要直接使用该值。通过赋值给cf->cycle->connection_n
,实现配置的全局共享
return NGX_CONF_OK;