static char * ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_event_conf_t *ecf = conf; #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL) int fd; #endif #if (NGX_HAVE_RTSIG) ngx_uint_t rtsig; ngx_core_conf_t *ccf; #endif ngx_int_t i; ngx_module_t *module; ngx_event_module_t *event_module; module = NULL; #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL) fd = epoll_create(100); if (fd != -1) { (void) close(fd); module = &ngx_epoll_module; } else if (ngx_errno != NGX_ENOSYS) { module = &ngx_epoll_module; } #endif #if (NGX_HAVE_RTSIG) if (module == NULL) { module = &ngx_rtsig_module; rtsig = 1; } else { rtsig = 0; } #endif #if (NGX_HAVE_DEVPOLL) module = &ngx_devpoll_module; #endif #if (NGX_HAVE_KQUEUE) module = &ngx_kqueue_module; #endif #if (NGX_HAVE_SELECT) if (module == NULL) { module = &ngx_select_module; } #endif if (module == NULL) { for (i = 0; ngx_modules[i]; i++) { if (ngx_modules[i]->type != NGX_EVENT_MODULE) { continue; } event_module = ngx_modules[i]->ctx; if (ngx_strcmp(event_module->name->data, event_core_name.data) == 0) { continue; } module = ngx_modules[i]; break; } } if (module == NULL) { ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found"); return NGX_CONF_ERROR; } ngx_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS); cycle->connection_n = ecf->connections; ngx_conf_init_uint_value(ecf->use, module->ctx_index); event_module = module->ctx; ngx_conf_init_ptr_value(ecf->name, event_module->name->data); ngx_conf_init_value(ecf->multi_accept, 0); ngx_conf_init_value(ecf->accept_mutex, 1); ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500); #if (NGX_HAVE_RTSIG) if (!rtsig) { return NGX_CONF_OK; } if (ecf->accept_mutex) { return NGX_CONF_OK; } ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); if (ccf->worker_processes == 0) { return NGX_CONF_OK; } ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "the \"rtsig\" method requires \"accept_mutex\" to be on"); return NGX_CONF_ERROR; #else return NGX_CONF_OK; #endif }
static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_event_conf_t *ecf = conf; #if (HAVE_RTSIG) ngx_core_conf_t *ccf; #endif #if (HAVE_KQUEUE) ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS); ngx_conf_init_unsigned_value(ecf->use, ngx_kqueue_module.ctx_index); ngx_conf_init_ptr_value(ecf->name, ngx_kqueue_module_ctx.name->data); #elif (HAVE_DEVPOLL) ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS); ngx_conf_init_unsigned_value(ecf->use, ngx_devpoll_module.ctx_index); ngx_conf_init_ptr_value(ecf->name, ngx_devpoll_module_ctx.name->data); #elif (HAVE_EPOLL) ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS); ngx_conf_init_unsigned_value(ecf->use, ngx_epoll_module.ctx_index); ngx_conf_init_ptr_value(ecf->name, ngx_epoll_module_ctx.name->data); #elif (HAVE_RTSIG) ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS); ngx_conf_init_unsigned_value(ecf->use, ngx_rtsig_module.ctx_index); ngx_conf_init_ptr_value(ecf->name, ngx_rtsig_module_ctx.name->data); #elif (HAVE_SELECT) #if (WIN32) ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS); #else ngx_conf_init_unsigned_value(ecf->connections, FD_SETSIZE < DEFAULT_CONNECTIONS ? FD_SETSIZE : DEFAULT_CONNECTIONS); #endif ngx_conf_init_unsigned_value(ecf->use, ngx_select_module.ctx_index); ngx_conf_init_ptr_value(ecf->name, ngx_select_module_ctx.name->data); #else ngx_int_t i, m; ngx_event_module_t *module; m = -1; module = NULL; for (i = 0; ngx_modules[i]; i++) { if (ngx_modules[i]->type == NGX_EVENT_MODULE) { module = ngx_modules[i]->ctx; if (ngx_strcmp(module->name->data, event_core_name.data) == 0) { continue; } m = ngx_modules[i]->ctx_index; break; } } if (m == -1) { ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found"); return NGX_CONF_ERROR; } ngx_conf_init_unsigned_value(ecf->connections, DEFAULT_CONNECTIONS); ngx_conf_init_unsigned_value(ecf->use, m); ngx_conf_init_ptr_value(ecf->name, module->name->data); #endif cycle->connection_n = ecf->connections; ngx_conf_init_value(ecf->multi_accept, 0); ngx_conf_init_value(ecf->accept_mutex, 1); ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500); #if (HAVE_RTSIG) if (ecf->use == ngx_rtsig_module.ctx_index && ecf->accept_mutex == 0) { ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); if (ccf->worker_processes) { ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "the \"rtsig\" method requires " "\"accept_mutex\" to be on"); return NGX_CONF_ERROR; } } #endif return NGX_CONF_OK; }
// 所有模块配置解析完毕后,对配置进行初始化 // 如果有的指令没有写,就要给正确的默认值 // 模块默认使用epoll // 默认不接受多个请求,也就是一次只accept一个连接 // 默认使用负载均衡锁 static char * ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_event_conf_t *ecf = conf; #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL) int fd; #endif // rtsig在nginx 1.9.x已经删除 ngx_int_t i; ngx_module_t *module; ngx_event_module_t *event_module; module = NULL; // 测试epoll是否可用 #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL) fd = epoll_create(100); // epoll调用可用,那么模块默认使用epoll if (fd != -1) { (void) close(fd); // epoll调用可用,那么模块默认使用epoll module = &ngx_epoll_module; } else if (ngx_errno != NGX_ENOSYS) { // epoll调用可用,那么模块默认使用epoll module = &ngx_epoll_module; } #endif // rtsig在nginx 1.9.x已经删除 #if (NGX_HAVE_DEVPOLL) && !(NGX_TEST_BUILD_DEVPOLL) module = &ngx_devpoll_module; #endif #if (NGX_HAVE_KQUEUE) module = &ngx_kqueue_module; #endif // 如果epoll不可用,那么默认使用select #if (NGX_HAVE_SELECT) if (module == NULL) { module = &ngx_select_module; } #endif // 还没有决定默认的事件模型 if (module == NULL) { // 遍历所有的事件模块 for (i = 0; cycle->modules[i]; i++) { if (cycle->modules[i]->type != NGX_EVENT_MODULE) { continue; } event_module = cycle->modules[i]->ctx; // 跳过event_core模块 if (ngx_strcmp(event_module->name->data, event_core_name.data) == 0) { continue; } // 使用数组里的第一个事件模块 module = cycle->modules[i]; break; } } // 最后还没有决定默认的事件模型,出错 if (module == NULL) { ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found"); return NGX_CONF_ERROR; } // nginx每个进程可使用的连接数量,即cycle里的连接池大小 ngx_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS); // 如果没有使用worker_connections指令,在这里设置 cycle->connection_n = ecf->connections; // 决定使用的事件模型,之前的module只作为默认值,如果已经使用了use则无效 ngx_conf_init_uint_value(ecf->use, module->ctx_index); // 初始化使用的事件模块的名字 event_module = module->ctx; ngx_conf_init_ptr_value(ecf->name, event_module->name->data); // 默认不接受多个请求,也就是一次只accept一个连接 ngx_conf_init_value(ecf->multi_accept, 0); // 默认使用负载均衡锁 ngx_conf_init_value(ecf->accept_mutex, 1); // 默认负载均衡锁的等待时间是500毫秒 ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500); return NGX_CONF_OK; }
static char * ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf) { ngx_event_conf_t *ecf = conf; #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL) int fd; #endif ngx_int_t i; ngx_module_t *module; ngx_event_module_t *event_module; module = NULL; #if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL) fd = epoll_create(100); if (fd != -1) { (void) close(fd); module = &ngx_epoll_module; } else if (ngx_errno != NGX_ENOSYS) { module = &ngx_epoll_module; } #endif #if (NGX_HAVE_DEVPOLL) module = &ngx_devpoll_module; #endif #if (NGX_HAVE_KQUEUE) module = &ngx_kqueue_module; #endif #if (NGX_HAVE_SELECT) if (module == NULL) { module = &ngx_select_module; } #endif //ngx_event_core_module后的第一个NGX_EVENT_MODULE也就是ngx_epoll_module默认作为第一个event模块 if (module == NULL) { for (i = 0; ngx_modules[i]; i++) { if (ngx_modules[i]->type != NGX_EVENT_MODULE) { continue; } event_module = ngx_modules[i]->ctx; if (ngx_strcmp(event_module->name->data, event_core_name.data) == 0) //不能为ngx_event_core_module { continue; } module = ngx_modules[i]; break; } } if (module == NULL) { ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found"); return NGX_CONF_ERROR; } ngx_conf_init_uint_value(ecf->connections, DEFAULT_CONNECTIONS); cycle->connection_n = ecf->connections; ngx_conf_init_uint_value(ecf->use, module->ctx_index); event_module = module->ctx; ngx_conf_init_ptr_value(ecf->name, event_module->name->data); ngx_conf_init_value(ecf->multi_accept, 0); ngx_conf_init_value(ecf->accept_mutex, 1); ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500); return NGX_CONF_OK; }