如何在 libcurl C++ 库中正确使用 Keep-Alive 标头

问题描述 投票:0回答:0

我有一个应用程序,它对后端服务器进行了数百万次调用以从数据库中获取一些数据。它使用带有 TCP 的 HTTP API,执行请求所需的平均时间为 150-200 毫秒。在后端服务器上花费的时间仅为1-2ms。大量时间花在了 DNS 查找和三向 SSL 握手上。 我想利用 TCP

Keep-Alive
功能不关闭与后端的 TCP 连接。 我注意到仅仅为请求添加标头减少了执行请求所花费的时间。这是通过 POSTMAN 注意到的。

但是,当通过我们的 C++ 应用程序执行时,我没有看到类似的数据。

下面是我们使用的代码。

INT CCurlHTTP::HTTPSPost(const CString& endPointUrl, const CString& urlparam,const CString& cookie){
    
    CURL *curl;
    CURLcode res;
    struct curl_slist *headers=NULL;
    char errbuf[CURL_ERROR_SIZE];

    curl = curl_easy_init();

    CString KeepAlive = "Connection: keep-alive";
    CString KeepAliveSettings = "Keep-Alive: timeout=100, max=100";

    get_request req;
    req.buffer =0;
    req.len =0;
    req.buflen =0;

    if(curl) 
    {
        //add url, headers, and paramaters to the request
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_easy_setopt(curl, CURLOPT_URL, endPointUrl);
        curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
        headers = curl_slist_append(headers, m_httpHeadAccept);
        headers = curl_slist_append(headers, m_httpContentType);
        headers = curl_slist_append(headers, KeepAlive);
        headers = curl_slist_append(headers, KeepAliveSettings);
        //callback function used to save response
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_String);
        curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
        curl_easy_setopt(curl, CURLOPT_MAXCONNECTS, 5L);
        req.buffer = (unsigned char*) malloc(CHUNK_SIZE);
        req.buflen = CHUNK_SIZE;
        req.len = 0;
        curl_easy_setopt(curl,CURLOPT_WRITEDATA, (void *)&req);
        
        if (!cookie.IsEmpty())
        {
            headers = curl_slist_append(headers, m_DBAuthCertficate); //What is difference between this and line no 118?
            CString pCookie = "DBAuthTicket=" + cookie;
            curl_easy_setopt(curl,CURLOPT_COOKIE, pCookie);
        }
        else
        {
            headers = curl_slist_append(headers, m_OAuthToken);
        }
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, urlparam);
        curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
        errbuf[0] = 0;
        
        curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 512000);
        CFileTime start, end;
                CFileTimeSpan duration;

        start = CFileTime::GetCurrentTime();

        res = curl_easy_perform(curl);

        end = CFileTime::GetCurrentTime();
        duration = (end - start);

        CString elapsed, logMsg;
        elapsed.Format("%ld",duration.GetTimeSpan()/10000);
        logMsg.Format("[CCurlHTTP::HTTPSPost::%d]Respone time = %s ms",__LINE__, elapsed);
        UTHelper::Get().WriteLog(logMsg);

        long res_code;
        
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res_code);

        if(res_code != 200 && res_code != 201)
        {
            res = CURLE_HTTP_RETURNED_ERROR;
        }

        m_response = (char*)req.buffer;
        m_errDescription.Format("%ld", res_code);
        len = req.len;
        buflen = req.buflen;

        //curl_easy_cleanup(curl);
        free(req.buffer);
    }
    return res;
}

这段代码用于单线程应用程序和多线程 Web 应用程序(VBScript + C++ COM DLL)这是一个非常遗留的代码库。

问题

  1. 每次我们想要执行新请求时,我们都会创建一个新的 CurlHTTP 类。 curl_easy_init 每次都会给我们新的卷曲句柄,还是会给出相同的句柄。
  2. 如何使用
    Keep-Alive
    标头进行这项工作。相同的代码根本不会影响这些标头。响应时间仍然是 150-200 毫秒。
  3. 请以任何可能的方式提供帮助。我无法制作 CurlHTTP 单例,因为这段代码也在多线程环境中使用,如果我尝试同步,它可能会导致瓶颈。
  4. 我在哪里初始化 curl_global_init 以及我应该在那里传递什么参数。

遗留代码 - C++ 98 + VC7.

visual-c++ libcurl c++98
© www.soinside.com 2019 - 2024. All rights reserved.