Minor enhancement: move some smart pointers around instead of copying them (#1954)

This commit is contained in:
itrofimow 2024-02-15 10:52:10 +04:00 committed by GitHub
parent da7f065a6f
commit 88d06684f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 27 deletions

View File

@ -6,6 +6,6 @@ void JsonCtrl::asyncHandleHttpRequest(
{ {
Json::Value ret; Json::Value ret;
ret["message"] = "Hello, World!"; ret["message"] = "Hello, World!";
auto resp = HttpResponse::newHttpJsonResponse(ret); auto resp = HttpResponse::newHttpJsonResponse(std::move(ret));
callback(resp); callback(resp);
} }

View File

@ -99,31 +99,32 @@ bool HttpRequestParser::processRequestLine(const char *begin, const char *end)
HttpRequestImplPtr HttpRequestParser::makeRequestForPool(HttpRequestImpl *ptr) HttpRequestImplPtr HttpRequestParser::makeRequestForPool(HttpRequestImpl *ptr)
{ {
std::weak_ptr<HttpRequestParser> weakPtr = shared_from_this(); return std::shared_ptr<HttpRequestImpl>(
return std::shared_ptr<HttpRequestImpl>(ptr, [weakPtr](HttpRequestImpl *p) { ptr, [weakPtr = weak_from_this()](HttpRequestImpl *p) {
auto thisPtr = weakPtr.lock(); auto thisPtr = weakPtr.lock();
if (thisPtr) if (thisPtr)
{
if (thisPtr->loop_->isInLoopThread())
{ {
p->reset(); if (thisPtr->loop_->isInLoopThread())
thisPtr->requestsPool_.emplace_back( {
thisPtr->makeRequestForPool(p));
}
else
{
thisPtr->loop_->queueInLoop([thisPtr, p]() {
p->reset(); p->reset();
thisPtr->requestsPool_.emplace_back( thisPtr->requestsPool_.emplace_back(
thisPtr->makeRequestForPool(p)); thisPtr->makeRequestForPool(p));
}); }
else
{
auto &loop = thisPtr->loop_;
loop->queueInLoop([thisPtr = std::move(thisPtr), p]() {
p->reset();
thisPtr->requestsPool_.emplace_back(
thisPtr->makeRequestForPool(p));
});
}
} }
} else
else {
{ delete p;
delete p; }
} });
});
} }
void HttpRequestParser::reset() void HttpRequestParser::reset()

View File

@ -546,11 +546,12 @@ void HttpServer::httpRequestHandling(
} }
} }
binderPtr->handleRequest( auto &binderRef = *binderPtr;
binderRef.handleRequest(
req, req,
// This is the actual callback being passed to controller // This is the actual callback being passed to controller
[req, binderPtr, callback = std::move(callback)]( [req, binderPtr = std::move(binderPtr), callback = std::move(callback)](
const HttpResponsePtr &resp) { const HttpResponsePtr &resp) mutable {
// Check if we need to cache the response // Check if we need to cache the response
if (resp->expiredTime() >= 0 && resp->statusCode() != k404NotFound) if (resp->expiredTime() >= 0 && resp->statusCode() != k404NotFound)
{ {
@ -562,9 +563,10 @@ void HttpServer::httpRequestHandling(
} }
else else
{ {
loop->queueInLoop([binderPtr, resp]() { loop->queueInLoop(
binderPtr->responseCache_.setThreadData(resp); [binderPtr = std::move(binderPtr), resp]() {
}); binderPtr->responseCache_.setThreadData(resp);
});
} }
} }
// post-handling aop // post-handling aop