Remove a mutex

This commit is contained in:
antao 2018-12-31 12:57:31 +08:00
parent 5f0dd36bd6
commit dd313ab293
3 changed files with 71 additions and 9 deletions

View File

@ -160,7 +160,7 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestPtr &r
}
HttpServerContext *context = any_cast<HttpServerContext>(conn->getMutableContext());
{
std::lock_guard<std::mutex> guard(context->getPipeLineMutex());
//std::lock_guard<std::mutex> guard(context->getPipeLineMutex());
context->pushRquestToPipeLine(req);
}
httpAsyncCallback_(req, [=](const HttpResponsePtr &response) {
@ -215,6 +215,7 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestPtr &r
}
delete[] zbuf;
}
if (conn->getLoop()->isInLoopThread())
{
/*
* A client that supports persistent connections MAY pipeline
@ -223,7 +224,7 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestPtr &r
* requests in the same order that the requests were received.
* rfc2616-8.1.1.2
*/
std::lock_guard<std::mutex> guard(context->getPipeLineMutex());
//std::lock_guard<std::mutex> guard(context->getPipeLineMutex());
if (context->getFirstRequest() == req)
{
context->popFirstRequest();
@ -246,6 +247,33 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestPtr &r
context->pushResponseToPipeLine(req, newResp);
}
}
else
{
conn->getLoop()->queueInLoop([conn, req, newResp, this]() {
HttpServerContext *context = any_cast<HttpServerContext>(conn->getMutableContext());
if (context->getFirstRequest() == req)
{
context->popFirstRequest();
sendResponse(conn, newResp);
while (1)
{
auto resp = context->getFirstResponse();
if (resp)
{
context->popFirstRequest();
sendResponse(conn, resp);
}
else
return;
}
}
else
{
//some earlier requests are waiting for responses;
context->pushResponseToPipeLine(req, newResp);
}
});
}
});
}
void HttpServer::sendResponse(const TcpConnectionPtr &conn,

View File

@ -22,7 +22,6 @@ using namespace drogon;
HttpServerContext::HttpServerContext(const trantor::TcpConnectionPtr &connPtr)
: state_(kExpectRequestLine),
request_(new HttpRequestImpl),
_pipeLineMutex(std::make_shared<std::mutex>()),
_conn(connPtr)
{
}
@ -199,12 +198,26 @@ bool HttpServerContext::parseRequest(MsgBuffer *buf)
void HttpServerContext::pushRquestToPipeLine(const HttpRequestPtr &req)
{
#ifndef NDEBUG
auto conn = _conn.lock();
if(conn)
{
conn->getLoop()->assertInLoopThread();
}
#endif
std::pair<HttpRequestPtr, HttpResponsePtr> reqPair(req, HttpResponseImplPtr());
_requestPipeLine.push_back(std::move(reqPair));
}
HttpRequestPtr HttpServerContext::getFirstRequest() const
{
#ifndef NDEBUG
auto conn = _conn.lock();
if (conn)
{
conn->getLoop()->assertInLoopThread();
}
#endif
if (!_requestPipeLine.empty())
{
return _requestPipeLine.front().first;
@ -213,6 +226,13 @@ HttpRequestPtr HttpServerContext::getFirstRequest() const
}
HttpResponsePtr HttpServerContext::getFirstResponse() const
{
#ifndef NDEBUG
auto conn = _conn.lock();
if (conn)
{
conn->getLoop()->assertInLoopThread();
}
#endif
if (!_requestPipeLine.empty())
{
return _requestPipeLine.front().second;
@ -221,11 +241,25 @@ HttpResponsePtr HttpServerContext::getFirstResponse() const
}
void HttpServerContext::popFirstRequest()
{
#ifndef NDEBUG
auto conn = _conn.lock();
if (conn)
{
conn->getLoop()->assertInLoopThread();
}
#endif
_requestPipeLine.pop_front();
}
void HttpServerContext::pushResponseToPipeLine(const HttpRequestPtr &req,
const HttpResponsePtr &resp)
{
#ifndef NDEBUG
auto conn = _conn.lock();
if (conn)
{
conn->getLoop()->assertInLoopThread();
}
#endif
for (auto &iter : _requestPipeLine)
{
if (iter.first == req)
@ -236,7 +270,7 @@ void HttpServerContext::pushResponseToPipeLine(const HttpRequestPtr &req,
}
}
std::mutex &HttpServerContext::getPipeLineMutex()
{
return *_pipeLineMutex;
}
// std::mutex &HttpServerContext::getPipeLineMutex()
// {
// return *_pipeLineMutex;
// }

View File

@ -85,7 +85,7 @@ class HttpServerContext
_websockConnPtr = conn;
}
//to support request pipelining(rfc2616-8.1.2.2)
std::mutex &getPipeLineMutex();
//std::mutex &getPipeLineMutex();
void pushRquestToPipeLine(const HttpRequestPtr &req);
HttpRequestPtr getFirstRequest() const;
HttpResponsePtr getFirstResponse() const;
@ -102,7 +102,7 @@ class HttpServerContext
WebSocketConnectionPtr _websockConnPtr;
std::list<std::pair<HttpRequestPtr, HttpResponsePtr>> _requestPipeLine;
std::shared_ptr<std::mutex> _pipeLineMutex;
//std::shared_ptr<std::mutex> _pipeLineMutex;
std::weak_ptr<trantor::TcpConnection> _conn;
};