diff --git a/lib/src/HttpClientImpl.cc b/lib/src/HttpClientImpl.cc index 85529efb..451e3e44 100644 --- a/lib/src/HttpClientImpl.cc +++ b/lib/src/HttpClientImpl.cc @@ -79,8 +79,12 @@ void HttpClientImpl::createTcpClient() } auto resp = responseParser->responseImpl(); responseParser->reset(); - thisPtr->handleResponse(resp, std::move(firstReq)); - thisPtr->tcpClientPtr_.reset(); + thisPtr->handleResponse(resp, std::move(firstReq), connPtr); + if (!thisPtr->requestsBuffer_.empty()) + { + thisPtr->createTcpClient(); + } + return; } thisPtr->onError(ReqResult::NetworkFailure); } @@ -236,6 +240,7 @@ void HttpClientImpl::sendRequest(const drogon::HttpRequestPtr &req, thisPtr->sendRequestInLoop(req, std::move(callback), timeout); }); } + void HttpClientImpl::sendRequestInLoop(const HttpRequestPtr &req, HttpReqCallback &&callback, double timeout) @@ -485,18 +490,30 @@ void HttpClientImpl::handleResponse( // pipeliningCallbacks_.size(); LOG_TRACE << "requests buffer size=" // << requestsBuffer_.size(); - if (!requestsBuffer_.empty()) + if (connPtr->connected()) { - auto &reqAndCallback = requestsBuffer_.front(); - sendReq(connPtr, reqAndCallback.first); - pipeliningCallbacks_.push(std::move(reqAndCallback)); - requestsBuffer_.pop(); + if (!requestsBuffer_.empty()) + { + auto &reqAndCallback = requestsBuffer_.front(); + sendReq(connPtr, reqAndCallback.first); + pipeliningCallbacks_.push(std::move(reqAndCallback)); + requestsBuffer_.pop(); + } + else + { + if (resp->ifCloseConnection() && pipeliningCallbacks_.empty()) + { + tcpClientPtr_.reset(); + } + } } else { - if (resp->ifCloseConnection() && pipeliningCallbacks_.empty()) + while (!pipeliningCallbacks_.empty()) { - tcpClientPtr_.reset(); + auto cb = std::move(pipeliningCallbacks_.front()); + pipeliningCallbacks_.pop(); + cb.second(ReqResult::NetworkFailure, nullptr); } } } @@ -601,4 +618,4 @@ void HttpClientImpl::handleCookies(const HttpResponseImplPtr &resp) validCookies_.emplace_back(cookie); } } -} \ No newline at end of file +} diff --git a/lib/src/HttpClientImpl.h b/lib/src/HttpClientImpl.h index fd5bd889..4464d8c6 100644 --- a/lib/src/HttpClientImpl.h +++ b/lib/src/HttpClientImpl.h @@ -93,7 +93,7 @@ class HttpClientImpl : public HttpClient, void handleCookies(const HttpResponseImplPtr &resp); void handleResponse(const HttpResponseImplPtr &resp, std::pair &&reqAndCb, - const trantor::TcpConnectionPtr &connPtr = nullptr); + const trantor::TcpConnectionPtr &connPtr); void createTcpClient(); std::queue> pipeliningCallbacks_; std::queue> requestsBuffer_; diff --git a/trantor b/trantor index 26b2e53b..b7c16286 160000 --- a/trantor +++ b/trantor @@ -1 +1 @@ -Subproject commit 26b2e53b671e90d6f5416d61cff4fe162171cd05 +Subproject commit b7c16286cdd4147e6ddf6b3831dc0578c794c10b