diff --git a/lib/base/io-engine.hpp b/lib/base/io-engine.hpp index 81efc1c96..a030d2044 100644 --- a/lib/base/io-engine.hpp +++ b/lib/base/io-engine.hpp @@ -3,6 +3,7 @@ #ifndef IO_ENGINE_H #define IO_ENGINE_H +#include "base/debug.hpp" #include "base/exception.hpp" #include "base/lazy-init.hpp" #include "base/logger.hpp" @@ -169,12 +170,15 @@ class Timeout : public SharedObject { public: DECLARE_PTR_TYPEDEFS(Timeout); + using Timer = boost::asio::deadline_timer; - template - Timeout(boost::asio::io_context& io, Executor& executor, TimeoutFromNow timeoutFromNow, OnTimeout onTimeout) - : m_Timer(io, timeoutFromNow) + template + Timeout(boost::asio::io_context::strand& strand, const Timer::duration_type& timeoutFromNow, OnTimeout onTimeout) + : m_Timer(strand.context(), timeoutFromNow) { - m_Timer.async_wait(boost::asio::bind_executor(executor, [onTimeout = std::move(onTimeout)](boost::system::error_code ec) { + VERIFY(strand.running_in_this_thread()); + + m_Timer.async_wait(boost::asio::bind_executor(strand, [onTimeout = std::move(onTimeout)](boost::system::error_code ec) { if (!ec) { onTimeout(); } @@ -184,7 +188,7 @@ public: void Cancel(); private: - boost::asio::deadline_timer m_Timer; + Timer m_Timer; }; } diff --git a/lib/base/tlsstream.cpp b/lib/base/tlsstream.cpp index c7f2884f4..d1153f2d4 100644 --- a/lib/base/tlsstream.cpp +++ b/lib/base/tlsstream.cpp @@ -140,7 +140,7 @@ void AsioTlsStream::GracefulDisconnect(boost::asio::io_context::strand& strand, } { - Timeout::Ptr shutdownTimeout(new Timeout(strand.context(), strand, boost::posix_time::seconds(10), + Timeout::Ptr shutdownTimeout(new Timeout(strand, boost::posix_time::seconds(10), [this] { // Forcefully terminate the connection if async_shutdown() blocked more than 10 seconds. ForceDisconnect(); diff --git a/lib/icingadb/redisconnection.hpp b/lib/icingadb/redisconnection.hpp index 8e94be26c..04b41e0b4 100644 --- a/lib/icingadb/redisconnection.hpp +++ b/lib/icingadb/redisconnection.hpp @@ -512,7 +512,6 @@ template Timeout::Ptr RedisConnection::MakeTimeout(StreamPtr& stream) { return new Timeout( - m_Strand.context(), m_Strand, boost::posix_time::microseconds(intmax_t(m_ConnectTimeout * 1000000)), [stream] { diff --git a/lib/methods/ifwapichecktask.cpp b/lib/methods/ifwapichecktask.cpp index d5148623a..ad19507e7 100644 --- a/lib/methods/ifwapichecktask.cpp +++ b/lib/methods/ifwapichecktask.cpp @@ -456,7 +456,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::Ptr timeout = new Timeout(strand->context(), *strand, boost::posix_time::microseconds(int64_t(checkTimeout * 1e6)), + Timeout::Ptr timeout = new Timeout(*strand, boost::posix_time::microseconds(int64_t(checkTimeout * 1e6)), [&conn, &checkable] { Log(LogNotice, "IfwApiCheckTask") << "Timeout while checking " << checkable->GetReflectionType()->GetName() diff --git a/lib/remote/apilistener.cpp b/lib/remote/apilistener.cpp index 913470bd4..8a18caf7e 100644 --- a/lib/remote/apilistener.cpp +++ b/lib/remote/apilistener.cpp @@ -534,7 +534,7 @@ void ApiListener::ListenerCoroutineProc(boost::asio::yield_context yc, const Sha auto strand (Shared::Make(io)); IoEngine::SpawnCoroutine(*strand, [this, strand, sslConn, remoteEndpoint](asio::yield_context yc) { - Timeout::Ptr timeout(new Timeout(strand->context(), *strand, boost::posix_time::microseconds(int64_t(GetConnectTimeout() * 1e6)), + Timeout::Ptr timeout (new Timeout(*strand, boost::posix_time::microseconds(int64_t(GetConnectTimeout() * 1e6)), [sslConn, remoteEndpoint] { Log(LogWarning, "ApiListener") << "Timeout while processing incoming connection from " << remoteEndpoint; @@ -585,7 +585,7 @@ void ApiListener::AddConnection(const Endpoint::Ptr& endpoint) lock.unlock(); - Timeout::Ptr timeout(new Timeout(strand->context(), *strand, boost::posix_time::microseconds(int64_t(GetConnectTimeout() * 1e6)), + Timeout::Ptr timeout (new Timeout(*strand, boost::posix_time::microseconds(int64_t(GetConnectTimeout() * 1e6)), [sslConn, endpoint, host, port] { Log(LogCritical, "ApiListener") << "Timeout while reconnecting to endpoint '" << endpoint->GetName() << "' via host '" << host @@ -684,7 +684,6 @@ void ApiListener::NewClientHandlerInternal( { Timeout::Ptr handshakeTimeout (new Timeout( - strand->context(), *strand, boost::posix_time::microseconds(intmax_t(Configuration::TlsHandshakeTimeout * 1000000)), [client] { diff --git a/test/base-io-engine.cpp b/test/base-io-engine.cpp index 2261bab91..74cb67319 100644 --- a/test/base-io-engine.cpp +++ b/test/base-io-engine.cpp @@ -19,7 +19,7 @@ BOOST_AUTO_TEST_CASE(timeout_run) boost::asio::spawn(strand, [&](boost::asio::yield_context yc) { boost::asio::deadline_timer timer (io); - Timeout::Ptr timeout = new Timeout(io, strand, boost::posix_time::millisec(300), [&called] { ++called; }); + Timeout::Ptr timeout = new Timeout(strand, boost::posix_time::millisec(300), [&called] { ++called; }); BOOST_CHECK_EQUAL(called, 0); timer.expires_from_now(boost::posix_time::millisec(200)); @@ -42,7 +42,7 @@ BOOST_AUTO_TEST_CASE(timeout_cancelled) boost::asio::spawn(strand, [&](boost::asio::yield_context yc) { boost::asio::deadline_timer timer (io); - Timeout::Ptr timeout = new Timeout(io, strand, boost::posix_time::millisec(300), [&called] { ++called; }); + Timeout::Ptr timeout = new Timeout(strand, boost::posix_time::millisec(300), [&called] { ++called; }); timer.expires_from_now(boost::posix_time::millisec(200)); timer.async_wait(yc); @@ -68,7 +68,7 @@ BOOST_AUTO_TEST_CASE(timeout_scope) boost::asio::deadline_timer timer (io); { - Timeout::Ptr timeout = new Timeout(io, strand, boost::posix_time::millisec(300), [&called] { ++called; }); + Timeout::Ptr timeout = new Timeout(strand, boost::posix_time::millisec(300), [&called] { ++called; }); timer.expires_from_now(boost::posix_time::millisec(200)); timer.async_wait(yc); @@ -92,7 +92,7 @@ BOOST_AUTO_TEST_CASE(timeout_due_cancelled) boost::asio::spawn(strand, [&](boost::asio::yield_context yc) { boost::asio::deadline_timer timer (io); - Timeout::Ptr timeout = new Timeout(io, strand, boost::posix_time::millisec(300), [&called] { ++called; }); + Timeout::Ptr timeout = new Timeout(strand, boost::posix_time::millisec(300), [&called] { ++called; }); // Give the timeout enough time to become due while blocking its strand to prevent it from actually running... Utility::Sleep(0.4); @@ -122,7 +122,7 @@ BOOST_AUTO_TEST_CASE(timeout_due_scope) boost::asio::deadline_timer timer (io); { - Timeout::Ptr timeout = new Timeout(io, strand, boost::posix_time::millisec(300), [&called] { ++called; }); + Timeout::Ptr timeout = new Timeout(strand, boost::posix_time::millisec(300), [&called] { ++called; }); // Give the timeout enough time to become due while blocking its strand to prevent it from actually running... Utility::Sleep(0.4);