[alibaba/tengine]dyups模块中的ngx_dyups_parse_upstream函数中为什么要对变量进行重新初始化?

2024-05-15 461 views
5
Question

在ngx_dyups_parse_upstream函数里,在之行完ngx_conf_parse函数之后,会调用一段对变量的重新初始化,但是我觉得这段逻辑毫无意义。因为这里一开始给cmcf->variablescmcf->variables_hash等几个变量赋值,完成下面的逻辑之后,最后又给cmcf->variablescmcf->variables_hash赋为原来的值,相当于没变过任何东西。(ps:我也没看懂这里for循环里面的逻辑是为了啥)

代码片段如下:

    cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

    va_prev = cmcf->variables;
    vh_prev = cmcf->variables_hash;

    ngx_memzero(&va, sizeof(va));
    ngx_memzero(&vh, sizeof(vh));
    ngx_memzero(&vk, sizeof(vk));

    cmcf->variables      = va;
    cmcf->variables_hash = vh;
    cmcf->variables_keys = &vk;

    v = va_prev.elts;
    for (i = 0; i < va_prev.nelts; i++) {

        if (v[i].get_handler) {
            continue;
        }

        s.len = v[i].name.len;
        s.data = ngx_pstrdup(ngx_cycle->pool, &v[i].name);
        if (!s.data) {
            rc = NGX_CONF_ERROR;
            break;
        }

        /*
         * variable name will be assign to cmcf->variables[idx].name directly
         * so the lifetime of v[i].name should be the same as cmcf
         */
        v[i].name = s;

        cmcf->variables.elts = &v[i];
        cmcf->variables.nelts = 1;
        if (ngx_http_variables_init_vars(cf) != NGX_OK) {
            rc = NGX_CONF_ERROR;
            break;
        }
    }

    cmcf->variables      = va_prev;
    cmcf->variables_hash = vh_prev;
    cmcf->variables_keys = NULL;

回答

3

@chobits @wangfakang 大佬能解答一下疑惑吗?