icinga2/lib/remote/httpserverconnection.hpp
Yonas Habteab 97ad0fc552 Make HTTP livness timout configurable for unittests
It's annoying to have to wait 10 seconds for the `liveness_disconnect`
test to complete, so make the timeout configurable and set it to a way
lower value to test the functionality.
2025-09-12 12:54:18 +02:00

71 lines
1.9 KiB
C++

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#ifndef HTTPSERVERCONNECTION_H
#define HTTPSERVERCONNECTION_H
#include "remote/apiuser.hpp"
#include "base/string.hpp"
#include "base/tlsstream.hpp"
#include "base/wait-group.hpp"
#include <memory>
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/io_context_strand.hpp>
#include <boost/asio/spawn.hpp>
namespace icinga
{
/**
* An API client connection.
*
* @ingroup remote
*/
class HttpServerConnection final : public Object
{
public:
DECLARE_PTR_TYPEDEFS(HttpServerConnection);
HttpServerConnection(const WaitGroup::Ptr& waitGroup, const String& identity, bool authenticated,
const Shared<AsioTlsStream>::Ptr& stream);
void Start();
void StartDetectClientSideShutdown();
bool Disconnected();
/**
* Sets the liveness timeout in seconds.
*
* If we don't receive any data from the client in this time frame, we consider the connection
* dead and close it. The default is 10 seconds. This function should only be used for unit tests
* to speed them up.
*
* @param seconds The timeout in seconds.
*/
void SetLivenessTimeout(double seconds);
private:
WaitGroup::Ptr m_WaitGroup;
ApiUser::Ptr m_ApiUser;
Shared<AsioTlsStream>::Ptr m_Stream;
double m_Seen;
double m_LivenessTimeout{10.0}; // The liveness timeout in seconds. @see SetLivenessTimeout() for details.
String m_PeerAddress;
boost::asio::io_context::strand m_IoStrand;
bool m_ShuttingDown;
bool m_ConnectionReusable;
boost::asio::deadline_timer m_CheckLivenessTimer;
HttpServerConnection(const WaitGroup::Ptr& waitGroup, 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);
};
}
#endif /* HTTPSERVERCONNECTION_H */