cpprestsdk请求被阻止,并在多线程情况下以“无法读取HTTP状态行”响应

时间:2019-03-19 00:47:37

标签: multithreading c++11 cpprest-sdk

我已按照CppRestSdk示例创建了客户端服务器系统服务器。

这是我的实现的示例代码部分(仅请求发送部分)。

nf.request(methods::POST, U("/"), requestBody)
    .then([=] (pplx::task<http_response> task)
    {
        LOG_ENTRY;
        // some other work ..
        resp = task.get(); // fails
        http_response resp;
        do {
            try {

                resp = task.get();
            } catch (std::exception& e) {
                higLog("EXCEPTION CAUGHT: %s", e.what());

            }

            // some other checks ..

        }while(0);

       // process resp later ..

    }

其中nfhttp_client

这是我的系统的体系结构。

architecture

thread_pool_call_flow

主线程在epoll服务器中运行front-end循环。它还创建一个显式thread-poolepoll循环从客户端接收传入的消息,并将这些消息存储在work_queue中(由我实现)。线程池中的一个线程(池大小为500)被唤醒,并从工作队列中选择一个job。该线程对该消息执行处理,并使用cpprestsdk back-end对象与http_client服务器进行对话。

对于客户端中少量的并发线程(1015),系统可以正常工作。但是,当我在客户端增加线程数(例如50个线程)时,front-end服务器开始显示不可预测的错误。通常,它会在task.get()函数中长时间阻塞。有时在捕获块中也显示“无法读取HTTP状态行”。

我是cpprestsdk的新手。我不确定到底发生了什么。但据我猜测,backlog queue sizeback-end服务器中很小。我找到了set_backlog()函数,但是由于const引用而无法使用它。 line 307

上述错误的原因可能是什么?这是常见的行为吗?是因为40内部有cpprestsdk thread pool个线程吗?

0 个答案:

没有答案