Timeout#Timeout(): drop unnecessary template parameters

This commit is contained in:
Alexander A. Klimov 2024-11-28 16:03:27 +01:00
parent d2285bcf0e
commit cb51649363
6 changed files with 18 additions and 16 deletions

View File

@ -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<class Executor, class TimeoutFromNow, class OnTimeout>
Timeout(boost::asio::io_context& io, Executor& executor, TimeoutFromNow timeoutFromNow, OnTimeout onTimeout)
: m_Timer(io, timeoutFromNow)
template<class OnTimeout>
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;
};
}

View File

@ -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();

View File

@ -515,7 +515,6 @@ template<class StreamPtr>
Timeout::Ptr RedisConnection::MakeTimeout(StreamPtr& stream)
{
return new Timeout(
m_Strand.context(),
m_Strand,
boost::posix_time::microseconds(intmax_t(m_ConnectTimeout * 1000000)),
[stream] {

View File

@ -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()

View File

@ -534,7 +534,7 @@ 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::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] {

View File

@ -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);