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 f953b42
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
14 changes: 6 additions & 8 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 Down
6 changes: 3 additions & 3 deletions lib/remote/httpserverconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ 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;
bool m_HasStartedStreaming;
boost::asio::deadline_timer m_CheckLivenessTimer;
std::atomic<bool> m_ShuttingDown;
bool m_HasStartedStreaming;

HttpServerConnection(const String& identity, bool authenticated, const Shared<AsioTlsStream>::Ptr& stream, boost::asio::io_context& io);

Expand Down

0 comments on commit f953b42

Please sign in to comment.