Skip to content

Commit

Permalink
feat: Adds timeout to HttpsSocket::Connect()
Browse files Browse the repository at this point in the history
  • Loading branch information
aritosteles committed Dec 12, 2024
1 parent 62ff347 commit 83e4230
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
25 changes: 6 additions & 19 deletions src/agent/communicator/src/http_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,26 @@ namespace http_client
{
try
{
boost::asio::steady_timer timer(io_context);
bool connectionSuccess = false;
bool timer_expired = false;

// Start the asynchronous connect operation
boost::asio::async_connect(m_socket,
endpoints,
[&](const boost::system::error_code& errorCode, const auto&)
{
if (!timer_expired && !errorCode)
if (!errorCode)
{
timer.cancel(); // Cancel the timer if connect completes first
connectionSuccess = true;
ec = errorCode;
LogDebug("Connected successfully");
}
});

// Start the timer for the timeout
timer.expires_after(timeOut);
timer.async_wait(
[&](const boost::system::error_code& errorCode)
{
if (!errorCode && !connectionSuccess)
{
timer_expired = true;
m_socket.close();
ec = boost::asio::error::timed_out;
LogDebug("Connect operation timed out");
}
});

io_context.run_for(timeOut); // Run for 2 seconds();
if (!connectionSuccess)
{
ec = boost::asio::error::timed_out;
LogDebug("Connection timed out");
}
}
catch (const std::exception& e)
{
Expand Down
58 changes: 43 additions & 15 deletions src/agent/communicator/src/https_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,54 @@ namespace http_client
/// @brief Connects the socket to the given endpoints
/// @param endpoints The endpoints to connect to
/// @param ec The error code, if any occurred
void Connect([[maybe_unused]] boost::asio::io_context& io_context,
void Connect(boost::asio::io_context& io_context,
const boost::asio::ip::tcp::resolver::results_type& endpoints,
boost::system::error_code& ec,
[[maybe_unused]] const std::chrono::seconds timeout = std::chrono::seconds(2)) override
const std::chrono::seconds timeOut = std::chrono::seconds(TIMEOUT_DEFAULT)) override
{
try
{
boost::asio::connect(m_ssl_socket.next_layer(), endpoints.begin(), endpoints.end(), ec);
if (ec)
{
LogDebug("Connect failed: {}", ec.message());
return;
}

m_ssl_socket.handshake(boost::asio::ssl::stream_base::client, ec);
if (ec)
{
LogDebug("Handshake failed: {}", ec.message());
return;
}
boost::asio::steady_timer timer(io_context);
bool connectionSuccess = false;
bool timer_expired = false;

// Start the asynchronous connect operation
boost::asio::async_connect(m_ssl_socket.lowest_layer(),
endpoints,
[&](const boost::system::error_code& errorCode, const auto&)
{
if (!timer_expired && !errorCode)
{
timer.cancel(); // Cancel the timer if connect completes first
connectionSuccess = true;
ec = errorCode;

m_ssl_socket.handshake(boost::asio::ssl::stream_base::client, ec);
if (ec)
{
LogDebug("Handshake failed: {}", ec.message());
return;
}

LogDebug("Connected successfully");
}
});

// Start the timer for the timeout
timer.expires_after(timeOut);
timer.async_wait(
[&](const boost::system::error_code& errorCode)
{
if (!errorCode && !connectionSuccess)
{
timer_expired = true;
// Close();
ec = boost::asio::error::timed_out;
LogDebug("Connect operation timed out");
}
});

io_context.run_for(timeOut); // Run for 2 seconds();
}
catch (const std::exception& e)
{
Expand Down

0 comments on commit 83e4230

Please sign in to comment.