From 87b0c452db83eb2413415b6cb4d90ee251cc114c Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 20 Feb 2019 15:27:15 +0100 Subject: [PATCH] HttpServerConnection: re-add automatic disconnect --- lib/remote/httpserverconnection.cpp | 35 +++++++++++++++++++++++++++-- lib/remote/httpserverconnection.hpp | 2 ++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/remote/httpserverconnection.cpp b/lib/remote/httpserverconnection.cpp index 2419eb595..198a472b9 100644 --- a/lib/remote/httpserverconnection.cpp +++ b/lib/remote/httpserverconnection.cpp @@ -17,6 +17,7 @@ #include "base/timer.hpp" #include "base/tlsstream.hpp" #include "base/utility.hpp" +#include #include #include #include @@ -30,7 +31,7 @@ using namespace icinga; auto const l_ServerHeader ("Icinga/" + Application::GetAppVersion()); HttpServerConnection::HttpServerConnection(const String& identity, bool authenticated, const std::shared_ptr& stream) - : m_Stream(stream), m_IoStrand(stream->get_io_service()), m_ShuttingDown(false) + : m_Stream(stream), m_Seen(Utility::GetTime()), m_IoStrand(stream->get_io_service()), m_ShuttingDown(false) { if (authenticated) { m_ApiUser = ApiUser::GetByClientCN(identity); @@ -53,6 +54,7 @@ void HttpServerConnection::Start() HttpServerConnection::Ptr preventGc (this); asio::spawn(m_IoStrand, [this, preventGc](asio::yield_context yc) { ProcessMessages(yc); }); + asio::spawn(m_IoStrand, [this, preventGc](asio::yield_context yc) { CheckLiveness(yc); }); } void HttpServerConnection::Disconnect() @@ -351,6 +353,7 @@ bool ProcessRequest( boost::beast::http::request& request, ApiUser::Ptr& authenticatedUser, boost::beast::http::response& response, + double& seen, boost::asio::yield_context& yc ) { @@ -361,6 +364,8 @@ bool ProcessRequest( try { CpuBoundWork handlingRequest (yc); + Defer updateSeen ([&seen]() { seen = Utility::GetTime(); }); + HttpHandler::ProcessRequest(stream, authenticatedUser, request, response, yc, hasStartedStreaming); } catch (const std::exception& ex) { if (hasStartedStreaming) { @@ -409,6 +414,8 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc) break; } + m_Seen = Utility::GetTime(); + auto& request (parser.get()); { @@ -450,7 +457,9 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc) break; } - if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, yc)) { + m_Seen = std::numeric_limits::max(); + + if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, m_Seen, yc)) { break; } @@ -465,3 +474,25 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc) } } } + +void HttpServerConnection::CheckLiveness(boost::asio::yield_context yc) +{ + boost::asio::deadline_timer timer (m_Stream->get_io_service()); + + for (;;) { + timer.expires_from_now(boost::posix_time::seconds(5)); + timer.async_wait(yc); + + if (m_ShuttingDown) { + break; + } + + if (m_Seen < Utility::GetTime() - 10) { + Log(LogInformation, "HttpServerConnection") + << "No messages for HTTP connection have been received in the last 10 seconds."; + + Disconnect(); + break; + } + } +} diff --git a/lib/remote/httpserverconnection.hpp b/lib/remote/httpserverconnection.hpp index bb3291991..7db97ed68 100644 --- a/lib/remote/httpserverconnection.hpp +++ b/lib/remote/httpserverconnection.hpp @@ -31,11 +31,13 @@ public: private: ApiUser::Ptr m_ApiUser; std::shared_ptr m_Stream; + double m_Seen; String m_PeerAddress; boost::asio::io_service::strand m_IoStrand; bool m_ShuttingDown; void ProcessMessages(boost::asio::yield_context yc); + void CheckLiveness(boost::asio::yield_context yc); }; } -- 2.40.0