Skip to content
Open
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
10 changes: 5 additions & 5 deletions lib/base/io-engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,18 @@ void IoEngine::RunEventLoop()
AsioEvent::AsioEvent(boost::asio::io_context& io, bool init)
: m_Timer(io)
{
m_Timer.expires_at(init ? boost::posix_time::neg_infin : boost::posix_time::pos_infin);
using boost::asio::steady_timer;
m_Timer.expires_at(init ? steady_timer::time_point::min() : steady_timer::time_point::max());
}

void AsioEvent::Set()
{
m_Timer.expires_at(boost::posix_time::neg_infin);
m_Timer.expires_at(boost::asio::steady_timer::time_point::min());
}

void AsioEvent::Clear()
{
m_Timer.expires_at(boost::posix_time::pos_infin);
m_Timer.expires_at(boost::asio::steady_timer::time_point::max());
}

void AsioEvent::Wait(boost::asio::yield_context yc)
Expand Down Expand Up @@ -258,6 +259,5 @@ void Timeout::Cancel()
{
m_Cancelled->store(true);

boost::system::error_code ec;
m_Timer.cancel(ec);
m_Timer.cancel();
Comment thread
Al2Klimov marked this conversation as resolved.
}
9 changes: 4 additions & 5 deletions lib/base/io-engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <stdexcept>
#include <boost/context/fixedsize_stack.hpp>
#include <boost/exception/all.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/io_context_strand.hpp>
#include <boost/asio/spawn.hpp>
Expand Down Expand Up @@ -197,7 +196,7 @@ class AsioEvent
void Wait(boost::asio::yield_context yc);

private:
boost::asio::deadline_timer m_Timer;
boost::asio::steady_timer m_Timer;
};

/**
Expand Down Expand Up @@ -225,7 +224,7 @@ class AsioDualEvent
*
* This class provides a workaround for Boost.ASIO's lack of built-in timeout support.
* While Boost.ASIO handles asynchronous operations, it does not natively support timeouts for these operations.
* This class uses a boost::asio::deadline_timer to emulate a timeout by scheduling a callback to be triggered
* This class uses a boost::asio::steady_timer to emulate a timeout by scheduling a callback to be triggered
* after a specified duration, effectively adding timeout behavior where none exists.
* The callback is executed within the provided strand, ensuring thread-safety.
*
Expand All @@ -244,7 +243,7 @@ class AsioDualEvent
class Timeout
{
public:
using Timer = boost::asio::deadline_timer;
using Timer = boost::asio::steady_timer;

/**
* Schedules onTimeout to be triggered after timeoutFromNow on strand.
Expand All @@ -255,7 +254,7 @@ class Timeout
* @param onTimeout The callback to invoke when the timeout occurs.
*/
template<class OnTimeout>
Timeout(boost::asio::io_context::strand& strand, const Timer::duration_type& timeoutFromNow, OnTimeout onTimeout)
Timeout(boost::asio::io_context::strand& strand, const Timer::duration& timeoutFromNow, OnTimeout onTimeout)
: m_Timer(strand.context(), timeoutFromNow), m_Cancelled(Shared<Atomic<bool>>::Make(false))
{
ASSERT(IoEngine::IsStrandRunningOnThisThread(strand));
Expand Down
2 changes: 1 addition & 1 deletion lib/base/tlsstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void AsioTlsStream::GracefulDisconnect(boost::asio::io_context::strand& strand,
}

{
Timeout shutdownTimeout (strand, boost::posix_time::seconds(10),
Timeout shutdownTimeout (strand, 10s,
[this] {
// Forcefully terminate the connection if async_shutdown() blocked more than 10 seconds.
ForceDisconnect();
Expand Down
8 changes: 4 additions & 4 deletions lib/icingadb/redisconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ void RedisConnection::Connect(asio::yield_context& yc)
{
Defer notConnecting ([this]() { m_Connecting.store(m_Connected.load()); });

boost::asio::deadline_timer timer (m_Strand.context());
boost::asio::steady_timer timer (m_Strand.context());

for (;;) {
try {
Expand Down Expand Up @@ -363,7 +363,7 @@ void RedisConnection::Connect(asio::yield_context& yc)
<< "'): " << ex.what();
}

timer.expires_from_now(boost::posix_time::seconds(5));
timer.expires_after(5s);
timer.async_wait(yc);
}

Expand Down Expand Up @@ -476,11 +476,11 @@ void RedisConnection::LogStats(asio::yield_context& yc)
{
double lastMessage = 0;

m_LogStatsTimer.expires_from_now(boost::posix_time::seconds(10));
m_LogStatsTimer.expires_after(10s);

for (;;) {
m_LogStatsTimer.async_wait(yc);
m_LogStatsTimer.expires_from_now(boost::posix_time::seconds(10));
m_LogStatsTimer.expires_after(10s);

if (!IsConnected())
continue;
Expand Down
7 changes: 4 additions & 3 deletions lib/icingadb/redisconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "base/value.hpp"
#include <boost/asio/buffer.hpp>
#include <boost/asio/buffered_stream.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/io_context_strand.hpp>
#include <boost/asio/ip/tcp.hpp>
Expand Down Expand Up @@ -347,7 +347,7 @@ struct RedisConnInfo final : SharedObject
// Number of pending Redis queries, always 0 if m_Parent is set unless m_TrackOwnPendingQueries is true.
std::atomic_size_t m_PendingQueries{0};
bool m_TrackOwnPendingQueries; // Whether to track pending queries even if m_Parent is set.
boost::asio::deadline_timer m_LogStatsTimer;
boost::asio::steady_timer m_LogStatsTimer;
Ptr m_Parent;
};

Expand Down Expand Up @@ -568,9 +568,10 @@ void RedisConnection::Handshake(StreamPtr& strm, boost::asio::yield_context& yc)
template<class StreamPtr>
Timeout RedisConnection::MakeTimeout(StreamPtr& stream)
{
std::chrono::duration<float> connectTimeout{m_ConnInfo->ConnectTimeout};
return Timeout(
m_Strand,
boost::posix_time::microseconds(intmax_t(m_ConnInfo->ConnectTimeout * 1000000)),
std::chrono::duration_cast<std::chrono::microseconds>(connectTimeout),
[stream] {
boost::system::error_code ec;
stream->lowest_layer().cancel(ec);
Expand Down
6 changes: 3 additions & 3 deletions lib/methods/ifwapichecktask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,11 @@ void IfwApiCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
}
}

auto checkTimeout (command->GetTimeout());
std::chrono::duration<float> checkTimeout{command->GetTimeout()};
auto checkableTimeout (checkable->GetCheckTimeout());

if (!checkableTimeout.IsEmpty())
checkTimeout = checkableTimeout;
checkTimeout = std::chrono::duration<float>{checkableTimeout.Get<double>()};

if (resolvedMacros && !useResolvedMacros)
return;
Expand Down Expand Up @@ -457,7 +457,7 @@ void IfwApiCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
IoEngine::SpawnCoroutine(
*strand,
[strand, checkable, cr, psCommand, psHost, expectedSan, psPort, conn, req, checkTimeout, reportResult = std::move(reportResult)](asio::yield_context yc) {
Timeout timeout (*strand, boost::posix_time::microseconds(int64_t(checkTimeout * 1e6)),
Timeout timeout (*strand, std::chrono::duration_cast<std::chrono::microseconds>(checkTimeout),
[&conn, &checkable] {
Log(LogNotice, "IfwApiCheckTask")
<< "Timeout while checking " << checkable->GetReflectionType()->GetName()
Expand Down
4 changes: 2 additions & 2 deletions lib/otel/otel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void OTel::Stop()
// below, and we would end up blocking indefinitely, so we have to check the exporting
// state here first.
if (Exporting()) {
Timeout writerTimeout(m_Strand, boost::posix_time::seconds(5), [this] {
Timeout writerTimeout(m_Strand, 5s, [this] {
boost::system::error_code ec;
std::visit([&ec](auto& stream) { stream->lowest_layer().cancel(ec); }, *m_Stream);
});
Expand Down Expand Up @@ -246,7 +246,7 @@ void OTel::Connect(boost::asio::yield_context& yc)
stream = Shared<AsioTcpStream>::Make(m_Strand.context());
}

Timeout timeout{m_Strand, boost::posix_time::seconds(10), [this, stream] {
Timeout timeout{m_Strand, 10s, [this, stream] {
Log(LogCritical, "OTelExporter")
<< "Timeout while connecting to OpenTelemetry backend '" << m_ConnInfo.Host << ":" << m_ConnInfo.Port << "', cancelling attempt.";

Expand Down
9 changes: 6 additions & 3 deletions lib/remote/apilistener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ void ApiListener::ListenerCoroutineProc(boost::asio::yield_context yc, const Sha
auto strand (Shared<asio::io_context::strand>::Make(io));

IoEngine::SpawnCoroutine(*strand, [this, strand, sslConn, remoteEndpoint](asio::yield_context yc) {
Timeout timeout (*strand, boost::posix_time::microseconds(int64_t(GetConnectTimeout() * 1e6)),
std::chrono::duration<float> connectTo{GetConnectTimeout()};
Timeout timeout (*strand, std::chrono::duration_cast<std::chrono::microseconds>(connectTo),
[sslConn, remoteEndpoint] {
Log(LogWarning, "ApiListener")
<< "Timeout while processing incoming connection from " << remoteEndpoint;
Expand Down Expand Up @@ -628,7 +629,8 @@ void ApiListener::AddConnection(const Endpoint::Ptr& endpoint)

lock.unlock();

Timeout timeout (*strand, boost::posix_time::microseconds(int64_t(GetConnectTimeout() * 1e6)),
std::chrono::duration<float> connectTimeout{GetConnectTimeout()};
Timeout timeout (*strand, std::chrono::duration_cast<std::chrono::microseconds>(connectTimeout),
[sslConn, endpoint, host, port] {
Log(LogCritical, "ApiListener")
<< "Timeout while reconnecting to endpoint '" << endpoint->GetName() << "' via host '" << host
Expand Down Expand Up @@ -721,9 +723,10 @@ void ApiListener::NewClientHandlerInternal(
boost::system::error_code ec;

{
std::chrono::duration<float> tlsHandShakeTimeout{Configuration::TlsHandshakeTimeout};
Timeout handshakeTimeout (
*strand,
boost::posix_time::microseconds(intmax_t(Configuration::TlsHandshakeTimeout * 1000000)),
std::chrono::duration_cast<std::chrono::microseconds>(tlsHandShakeTimeout),
[client] {
boost::system::error_code ec;
client->lowest_layer().cancel(ec);
Expand Down
8 changes: 4 additions & 4 deletions lib/remote/eventqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ void EventsInbox::Push(Dictionary::Ptr event)
std::unique_lock<std::mutex> lock (m_Mutex);

m_Queue.emplace(std::move(event));
m_Timer.expires_at(boost::posix_time::neg_infin);
m_Timer.expires_at(boost::asio::steady_timer::time_point::min());
}

Dictionary::Ptr EventsInbox::Shift(boost::asio::yield_context yc, double timeout)
Dictionary::Ptr EventsInbox::Shift(boost::asio::yield_context yc, std::chrono::milliseconds timeout)
{
std::unique_lock<std::mutex> lock (m_Mutex, std::defer_lock);

m_Timer.expires_at(boost::posix_time::neg_infin);
m_Timer.expires_at(boost::asio::steady_timer::time_point::min());

{
boost::system::error_code ec;
Expand All @@ -192,7 +192,7 @@ Dictionary::Ptr EventsInbox::Shift(boost::asio::yield_context yc, double timeout
}

if (m_Queue.empty()) {
m_Timer.expires_from_now(boost::posix_time::milliseconds((unsigned long)(timeout * 1000.0)));
m_Timer.expires_after(timeout);
lock.unlock();

{
Expand Down
6 changes: 3 additions & 3 deletions lib/remote/eventqueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "remote/httphandler.hpp"
#include "base/object.hpp"
#include "config/expression.hpp"
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/spawn.hpp>
#include <condition_variable>
#include <cstddef>
Expand Down Expand Up @@ -96,7 +96,7 @@ class EventsInbox : public Object
const Expression::Ptr& GetFilter();

void Push(Dictionary::Ptr event);
Dictionary::Ptr Shift(boost::asio::yield_context yc, double timeout = 5);
Dictionary::Ptr Shift(boost::asio::yield_context yc, std::chrono::milliseconds timeout = 5s);

private:
struct Filter
Expand All @@ -111,7 +111,7 @@ class EventsInbox : public Object
std::mutex m_Mutex;
decltype(m_Filters.begin()) m_Filter;
std::queue<Dictionary::Ptr> m_Queue;
boost::asio::deadline_timer m_Timer;
boost::asio::steady_timer m_Timer;
};

class EventsSubscriber
Expand Down
2 changes: 1 addition & 1 deletion lib/remote/httpserverconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ void HttpServerConnection::CheckLiveness(boost::asio::yield_context yc)
// Wait for the half of the liveness timeout to give the connection some leeway to do other work.
// But never wait longer than 5 seconds to ensure timely shutdowns.
auto sleepTime = std::min(5000ms, m_LivenessTimeout / 2);
m_CheckLivenessTimer.expires_from_now(boost::posix_time::milliseconds(sleepTime.count()));
m_CheckLivenessTimer.expires_after(sleepTime);
m_CheckLivenessTimer.async_wait(yc[ec]);

if (m_ShuttingDown) {
Expand Down
4 changes: 2 additions & 2 deletions lib/remote/httpserverconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "base/tlsstream.hpp"
#include "base/wait-group.hpp"
#include <memory>
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/io_context_strand.hpp>
#include <boost/asio/spawn.hpp>
Expand Down Expand Up @@ -58,7 +58,7 @@ class HttpServerConnection final : public Object
boost::asio::io_context::strand m_IoStrand;
bool m_ShuttingDown;
bool m_ConnectionReusable;
boost::asio::deadline_timer m_CheckLivenessTimer;
boost::asio::steady_timer m_CheckLivenessTimer;

HttpServerConnection(const WaitGroup::Ptr& waitGroup, const String& identity, bool authenticated,
const Shared<AsioTlsStream>::Ptr& stream, boost::asio::io_context& io);
Expand Down
2 changes: 1 addition & 1 deletion lib/remote/jsonrpcconnection-heartbeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void JsonRpcConnection::HandleAndWriteHeartbeats(boost::asio::yield_context yc)
boost::system::error_code ec;

for (;;) {
m_HeartbeatTimer.expires_from_now(boost::posix_time::seconds(20));
m_HeartbeatTimer.expires_after(20s);
m_HeartbeatTimer.async_wait(yc[ec]);

if (m_ShuttingDown) {
Expand Down
6 changes: 3 additions & 3 deletions lib/remote/jsonrpcconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void JsonRpcConnection::Disconnect()
{
Timeout writerTimeout(
m_IoStrand,
boost::posix_time::seconds(5),
5s,
[this]() {
// The writer coroutine could not finish soon enough to unblock the waiter down blow,
// so we have to do this on our own, and the coroutine will be terminated forcibly when
Expand Down Expand Up @@ -412,7 +412,7 @@ void JsonRpcConnection::CheckLiveness(boost::asio::yield_context yc)
* leaking the connection. Therefore close it after a timeout.
*/

m_CheckLivenessTimer.expires_from_now(boost::posix_time::seconds(10));
m_CheckLivenessTimer.expires_after(10s);
m_CheckLivenessTimer.async_wait(yc[ec]);

if (m_ShuttingDown) {
Expand All @@ -427,7 +427,7 @@ void JsonRpcConnection::CheckLiveness(boost::asio::yield_context yc)
Disconnect();
} else {
for (;;) {
m_CheckLivenessTimer.expires_from_now(boost::posix_time::seconds(30));
m_CheckLivenessTimer.expires_after(30s);
m_CheckLivenessTimer.async_wait(yc[ec]);

if (m_ShuttingDown) {
Expand Down
2 changes: 1 addition & 1 deletion lib/remote/jsonrpcconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class JsonRpcConnection final : public Object
AsioEvent m_WriterDone;
Atomic<bool> m_ShuttingDown;
WaitGroup::Ptr m_WaitGroup;
boost::asio::deadline_timer m_CheckLivenessTimer, m_HeartbeatTimer;
boost::asio::steady_timer m_CheckLivenessTimer, m_HeartbeatTimer;

JsonRpcConnection(const WaitGroup::Ptr& waitgroup, const String& identity, bool authenticated,
const Shared<AsioTlsStream>::Ptr& stream, ConnectionRole role, boost::asio::io_context& io);
Expand Down
Loading
Loading