Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpServerConnection: Don't spawn useless coroutines #10247

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 35 additions & 30 deletions lib/remote/httpserverconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,44 +68,49 @@ void HttpServerConnection::Start()
IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) { CheckLiveness(yc); });
}

void HttpServerConnection::Disconnect()
/**
* Tries to asynchronously shut down the SSL stream and underlying socket.
*
* It is important to note that this method should only be called from within a coroutine that uses `m_IoStrand`.
*
* @param yc boost::asio::yield_context The coroutine yield context which you are calling this method from.
*/
void HttpServerConnection::Disconnect(boost::asio::yield_context yc)
{
namespace asio = boost::asio;

HttpServerConnection::Ptr keepAlive (this);
if (m_ShuttingDown) {
return;
}

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

Log(LogInformation, "HttpServerConnection")
<< "HTTP client disconnected (from " << m_PeerAddress << ")";

/*
* Do not swallow exceptions in a coroutine.
* https://github.com/Icinga/icinga2/issues/7351
* We must not catch `detail::forced_unwind exception` as
* this is used for unwinding the stack.
*
* Just use the error_code dummy here.
*/
boost::system::error_code ec;
Log(LogInformation, "HttpServerConnection")
<< "HTTP client disconnected (from " << m_PeerAddress << ")";

/*
* Do not swallow exceptions in a coroutine.
* https://github.com/Icinga/icinga2/issues/7351
* We must not catch `detail::forced_unwind exception` as
* this is used for unwinding the stack.
*
* Just use the error_code dummy here.
*/
boost::system::error_code ec;

m_CheckLivenessTimer.cancel();
m_CheckLivenessTimer.cancel();

m_Stream->lowest_layer().cancel(ec);
m_Stream->lowest_layer().cancel(ec);

m_Stream->next_layer().async_shutdown(yc[ec]);
m_Stream->next_layer().async_shutdown(yc[ec]);

m_Stream->lowest_layer().shutdown(m_Stream->lowest_layer().shutdown_both, ec);
m_Stream->lowest_layer().shutdown(m_Stream->lowest_layer().shutdown_both, ec);

auto listener (ApiListener::GetInstance());
auto listener (ApiListener::GetInstance());

if (listener) {
listener->RemoveHttpClient(this);
}
}
});
if (listener) {
listener->RemoveHttpClient(this);
}
}

void HttpServerConnection::StartStreaming()
Expand All @@ -126,7 +131,7 @@ void HttpServerConnection::StartStreaming()
m_Stream->async_read_some(readBuf, yc[ec]);
} while (!ec);

Disconnect();
Disconnect(yc);
}
});
}
Expand Down Expand Up @@ -563,7 +568,7 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
}
}

Disconnect();
Disconnect(yc);
}

void HttpServerConnection::CheckLiveness(boost::asio::yield_context yc)
Expand All @@ -582,7 +587,7 @@ void HttpServerConnection::CheckLiveness(boost::asio::yield_context yc)
Log(LogInformation, "HttpServerConnection")
<< "No messages for HTTP connection have been received in the last 10 seconds.";

Disconnect();
Disconnect(yc);
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/remote/httpserverconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class HttpServerConnection final : public Object
HttpServerConnection(const String& identity, bool authenticated, const Shared<AsioTlsStream>::Ptr& stream);

void Start();
void Disconnect();
void StartStreaming();

bool Disconnected();
Expand All @@ -45,6 +44,8 @@ class HttpServerConnection final : public Object

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

void Disconnect(boost::asio::yield_context yc);

void ProcessMessages(boost::asio::yield_context yc);
void CheckLiveness(boost::asio::yield_context yc);
};
Expand Down
Loading