Skip to content

Commit

Permalink
HttpServerConnection: Don't spawn useless coroutines
Browse files Browse the repository at this point in the history
Currently, for each `Disconnect()` call, we spawn a coroutine, but every
one of them is just usesless, except the first one. Thus, we should not
spawn a coroutine if someone has already triggered a disconnect.
  • Loading branch information
yhabteab committed Nov 4, 2024
1 parent d894792 commit 03cac8d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
26 changes: 8 additions & 18 deletions lib/remote/httpserverconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,10 @@ void HttpServerConnection::Disconnect()
{
namespace asio = boost::asio;

HttpServerConnection::Ptr keepAlive (this);

IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) {
if (!m_ShuttingDown) {
m_ShuttingDown = true;
if (!m_ShuttingDown.exchange(true)) {
HttpServerConnection::Ptr keepAlive (this);

IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) {
Log(LogInformation, "HttpServerConnection")
<< "HTTP client disconnected (from " << m_PeerAddress << ")";

Expand All @@ -104,8 +102,8 @@ void HttpServerConnection::Disconnect()
if (listener) {
listener->RemoveHttpClient(this);
}
}
});
});
}
}

void HttpServerConnection::StartStreaming()
Expand All @@ -131,7 +129,7 @@ void HttpServerConnection::StartStreaming()
});
}

bool HttpServerConnection::Disconnected()
bool HttpServerConnection::Disconnected() const
{
return m_ShuttingDown;
}
Expand All @@ -142,15 +140,11 @@ bool EnsureValidHeaders(
boost::beast::flat_buffer& buf,
boost::beast::http::parser<true, boost::beast::http::string_body>& parser,
boost::beast::http::response<boost::beast::http::string_body>& response,
bool& shuttingDown,
boost::asio::yield_context& yc
)
{
namespace http = boost::beast::http;

if (shuttingDown)
return false;

bool httpError = false;
String errorMsg;

Expand Down Expand Up @@ -337,7 +331,6 @@ bool EnsureValidBody(
boost::beast::http::parser<true, boost::beast::http::string_body>& parser,
ApiUser::Ptr& authenticatedUser,
boost::beast::http::response<boost::beast::http::string_body>& response,
bool& shuttingDown,
boost::asio::yield_context& yc
)
{
Expand Down Expand Up @@ -378,9 +371,6 @@ bool EnsureValidBody(
parser.body_limit(maxSize);
}

if (shuttingDown)
return false;

boost::system::error_code ec;

http::async_read(stream, buf, parser, yc[ec]);
Expand Down Expand Up @@ -493,7 +483,7 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)

response.set(http::field::server, l_ServerHeader);

if (!EnsureValidHeaders(*m_Stream, buf, parser, response, m_ShuttingDown, yc)) {
if (m_ShuttingDown || !EnsureValidHeaders(*m_Stream, buf, parser, response, yc)) {
break;
}

Expand Down Expand Up @@ -542,7 +532,7 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
break;
}

if (!EnsureValidBody(*m_Stream, buf, parser, authenticatedUser, response, m_ShuttingDown, yc)) {
if (m_ShuttingDown || !EnsureValidBody(*m_Stream, buf, parser, authenticatedUser, response, yc)) {
break;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/remote/httpserverconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class HttpServerConnection final : public Object
void Disconnect();
void StartStreaming();

bool Disconnected();
bool Disconnected() const;

private:
ApiUser::Ptr m_ApiUser;
Shared<AsioTlsStream>::Ptr m_Stream;
double m_Seen;
String m_PeerAddress;
boost::asio::io_context::strand m_IoStrand;
bool m_ShuttingDown;
std::atomic<bool> m_ShuttingDown;
bool m_HasStartedStreaming;
boost::asio::deadline_timer m_CheckLivenessTimer;

Expand Down

0 comments on commit 03cac8d

Please sign in to comment.