Skip to content

Commit

Permalink
Revert "refactor(Core/Misc): Use steady_timer instead of deadline_tim…
Browse files Browse the repository at this point in the history
…er (azerothcore#20940)"

This reverts commit 0bc7067.
  • Loading branch information
skelUA committed Jan 4, 2025
1 parent 0d917dc commit a01e8a6
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 65 deletions.
34 changes: 34 additions & 0 deletions src/common/Asio/DeadlineTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef DeadlineTimer_h__
#define DeadlineTimer_h__

#include <boost/asio/deadline_timer.hpp>

#define DeadlineTimerBase boost::asio::basic_deadline_timer<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime>, boost::asio::io_context::executor_type>

namespace Acore::Asio
{
class DeadlineTimer : public DeadlineTimerBase
{
public:
using DeadlineTimerBase::basic_deadline_timer;
};
}

#endif // DeadlineTimer_h__
5 changes: 3 additions & 2 deletions src/common/Asio/IoContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define IoContext_h__

#include <boost/version.hpp>

#include <boost/asio/io_context.hpp>
#include <boost/asio/post.hpp>
#define IoContextBaseNamespace boost::asio
Expand Down Expand Up @@ -51,9 +52,9 @@ namespace Acore::Asio
}

template<typename T>
inline boost::asio::io_context& get_io_context(T&& ioObject)
inline decltype(auto) get_io_context(T&& ioObject)
{
return static_cast<boost::asio::io_context&>(ioObject.get_executor().context());
return ioObject.get_executor().context();
}
}

Expand Down
13 changes: 5 additions & 8 deletions src/common/Metric/Metric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "Metric.h"
#include "Config.h"
#include "DeadlineTimer.h"
#include "Log.h"
#include "Strand.h"
#include "Tokenize.h"
Expand All @@ -41,8 +42,8 @@ void Metric::Initialize(std::string const& realmName, Acore::Asio::IoContext& io
{
_dataStream = std::make_unique<boost::asio::ip::tcp::iostream>();
_realmName = FormatInfluxDBTagValue(realmName);
_batchTimer = std::make_unique<boost::asio::steady_timer>(ioContext);
_overallStatusTimer = std::make_unique<boost::asio::steady_timer>(ioContext);
_batchTimer = std::make_unique<Acore::Asio::DeadlineTimer>(ioContext);
_overallStatusTimer = std::make_unique<Acore::Asio::DeadlineTimer>(ioContext);
_overallStatusLogger = overallStatusLogger;
LoadFromConfigs();
}
Expand Down Expand Up @@ -246,9 +247,7 @@ void Metric::ScheduleSend()
{
if (_enabled)
{
// Calculate the expiration time
auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(_updateInterval);
_batchTimer->expires_at(expirationTime);
_batchTimer->expires_from_now(boost::posix_time::seconds(_updateInterval));
_batchTimer->async_wait(std::bind(&Metric::SendBatch, this));
}
else
Expand Down Expand Up @@ -281,9 +280,7 @@ void Metric::ScheduleOverallStatusLog()
{
if (_enabled)
{
// Calculate the expiration time _overallStatusTimerInterval from now
auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(_overallStatusTimerInterval);
_overallStatusTimer->expires_at(expirationTime);
_overallStatusTimer->expires_from_now(boost::posix_time::seconds(_overallStatusTimerInterval));
_overallStatusTimer->async_wait([this](const boost::system::error_code&)
{
_overallStatusTimerTriggered = true;
Expand Down
6 changes: 3 additions & 3 deletions src/common/Metric/Metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "Define.h"
#include "Duration.h"
#include "MPSCQueue.h"
#include <boost/asio/steady_timer.hpp>
#include <functional>
#include <memory> // NOTE: this import is NEEDED (even though some IDEs report it as unused)
#include <string>
Expand All @@ -31,6 +30,7 @@
namespace Acore::Asio
{
class IoContext;
class DeadlineTimer;
}

enum MetricDataType
Expand Down Expand Up @@ -62,8 +62,8 @@ class AC_COMMON_API Metric
std::iostream& GetDataStream() { return *_dataStream; }
std::unique_ptr<std::iostream> _dataStream;
MPSCQueue<MetricData> _queuedData;
std::unique_ptr<boost::asio::steady_timer> _batchTimer;
std::unique_ptr<boost::asio::steady_timer> _overallStatusTimer;
std::unique_ptr<Acore::Asio::DeadlineTimer> _batchTimer;
std::unique_ptr<Acore::Asio::DeadlineTimer> _overallStatusTimer;
int32 _updateInterval = 0;
int32 _overallStatusTimerInterval = 0;
bool _enabled = false;
Expand Down
39 changes: 15 additions & 24 deletions src/server/apps/authserver/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "Config.h"
#include "DatabaseEnv.h"
#include "DatabaseLoader.h"
#include "DeadlineTimer.h"
#include "IPLocation.h"
#include "IoContext.h"
#include "Log.h"
Expand Down Expand Up @@ -59,8 +60,8 @@ namespace fs = std::filesystem;
bool StartDB();
void StopDB();
void SignalHandler(std::weak_ptr<Acore::Asio::IoContext> ioContextRef, boost::system::error_code const& error, int signalNumber);
void KeepDatabaseAliveHandler(std::weak_ptr<boost::asio::steady_timer> dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const& error);
void BanExpiryHandler(std::weak_ptr<boost::asio::steady_timer> banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const& error);
void KeepDatabaseAliveHandler(std::weak_ptr<Acore::Asio::DeadlineTimer> dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const& error);
void BanExpiryHandler(std::weak_ptr<Acore::Asio::DeadlineTimer> banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const& error);
variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile);

/// Launch the auth server
Expand Down Expand Up @@ -177,20 +178,14 @@ int main(int argc, char** argv)

// Enabled a timed callback for handling the database keep alive ping
int32 dbPingInterval = sConfigMgr->GetOption<int32>("MaxPingTime", 30);
std::shared_ptr<boost::asio::steady_timer> dbPingTimer = std::make_shared<boost::asio::steady_timer>(*ioContext);

// Calculate the expiration time
auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(dbPingInterval);
dbPingTimer->expires_at(expirationTime);
dbPingTimer->async_wait(std::bind(&KeepDatabaseAliveHandler, std::weak_ptr<boost::asio::steady_timer>(dbPingTimer), dbPingInterval, std::placeholders::_1));
std::shared_ptr<Acore::Asio::DeadlineTimer> dbPingTimer = std::make_shared<Acore::Asio::DeadlineTimer>(*ioContext);
dbPingTimer->expires_from_now(boost::posix_time::minutes(dbPingInterval));
dbPingTimer->async_wait(std::bind(&KeepDatabaseAliveHandler, std::weak_ptr<Acore::Asio::DeadlineTimer>(dbPingTimer), dbPingInterval, std::placeholders::_1));

int32 banExpiryCheckInterval = sConfigMgr->GetOption<int32>("BanExpiryCheckInterval", 60);
std::shared_ptr<boost::asio::steady_timer> banExpiryCheckTimer = std::make_shared<boost::asio::steady_timer>(*ioContext);

// Calculate the expiration time
auto expirationTimeBanExpiry = std::chrono::steady_clock::now() + std::chrono::seconds(banExpiryCheckInterval);
banExpiryCheckTimer->expires_at(expirationTimeBanExpiry);
banExpiryCheckTimer->async_wait(std::bind(&BanExpiryHandler, std::weak_ptr<boost::asio::steady_timer>(banExpiryCheckTimer), banExpiryCheckInterval, std::placeholders::_1));
std::shared_ptr<Acore::Asio::DeadlineTimer> banExpiryCheckTimer = std::make_shared<Acore::Asio::DeadlineTimer>(*ioContext);
banExpiryCheckTimer->expires_from_now(boost::posix_time::seconds(banExpiryCheckInterval));
banExpiryCheckTimer->async_wait(std::bind(&BanExpiryHandler, std::weak_ptr<Acore::Asio::DeadlineTimer>(banExpiryCheckTimer), banExpiryCheckInterval, std::placeholders::_1));

// Start the io service worker loop
ioContext->run();
Expand Down Expand Up @@ -243,35 +238,31 @@ void SignalHandler(std::weak_ptr<Acore::Asio::IoContext> ioContextRef, boost::sy
}
}

void KeepDatabaseAliveHandler(std::weak_ptr<boost::asio::steady_timer> dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const& error)
void KeepDatabaseAliveHandler(std::weak_ptr<Acore::Asio::DeadlineTimer> dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const& error)
{
if (!error)
{
if (std::shared_ptr<boost::asio::steady_timer> dbPingTimer = dbPingTimerRef.lock())
if (std::shared_ptr<Acore::Asio::DeadlineTimer> dbPingTimer = dbPingTimerRef.lock())
{
LOG_INFO("server.authserver", "Ping MySQL to keep connection alive");
LoginDatabase.KeepAlive();

// Calculate the expiration time
auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(dbPingInterval);
dbPingTimer->expires_at(expirationTime);
dbPingTimer->expires_from_now(boost::posix_time::minutes(dbPingInterval));
dbPingTimer->async_wait(std::bind(&KeepDatabaseAliveHandler, dbPingTimerRef, dbPingInterval, std::placeholders::_1));
}
}
}

void BanExpiryHandler(std::weak_ptr<boost::asio::steady_timer> banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const& error)
void BanExpiryHandler(std::weak_ptr<Acore::Asio::DeadlineTimer> banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const& error)
{
if (!error)
{
if (std::shared_ptr<boost::asio::steady_timer> banExpiryCheckTimer = banExpiryCheckTimerRef.lock())
if (std::shared_ptr<Acore::Asio::DeadlineTimer> banExpiryCheckTimer = banExpiryCheckTimerRef.lock())
{
LoginDatabase.Execute(LoginDatabase.GetPreparedStatement(LOGIN_DEL_EXPIRED_IP_BANS));
LoginDatabase.Execute(LoginDatabase.GetPreparedStatement(LOGIN_UPD_EXPIRED_ACCOUNT_BANS));

// Calculate the expiration time
auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(banExpiryCheckInterval);
banExpiryCheckTimer->expires_at(expirationTime);
banExpiryCheckTimer->expires_from_now(boost::posix_time::seconds(banExpiryCheckInterval));
banExpiryCheckTimer->async_wait(std::bind(&BanExpiryHandler, banExpiryCheckTimerRef, banExpiryCheckInterval, std::placeholders::_1));
}
}
Expand Down
11 changes: 4 additions & 7 deletions src/server/apps/worldserver/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "Config.h"
#include "DatabaseEnv.h"
#include "DatabaseLoader.h"
#include "DeadlineTimer.h"
#include "GitRevision.h"
#include "IoContext.h"
#include "MapMgr.h"
Expand Down Expand Up @@ -90,16 +91,14 @@ class FreezeDetector

static void Start(std::shared_ptr<FreezeDetector> const& freezeDetector)
{
// Calculate the expiration time 5seconds from now
auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(5);
freezeDetector->_timer.expires_at(expirationTime);
freezeDetector->_timer.expires_from_now(boost::posix_time::seconds(5));
freezeDetector->_timer.async_wait(std::bind(&FreezeDetector::Handler, std::weak_ptr<FreezeDetector>(freezeDetector), std::placeholders::_1));
}

static void Handler(std::weak_ptr<FreezeDetector> freezeDetectorRef, boost::system::error_code const& error);

private:
boost::asio::steady_timer _timer;
Acore::Asio::DeadlineTimer _timer;
uint32 _worldLoopCounter;
uint32 _lastChangeMsTime;
uint32 _maxCoreStuckTimeInMs;
Expand Down Expand Up @@ -632,9 +631,7 @@ void FreezeDetector::Handler(std::weak_ptr<FreezeDetector> freezeDetectorRef, bo
}
}

// Calculate the expiration time
auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(1);
freezeDetector->_timer.expires_at(expirationTime);
freezeDetector->_timer.expires_from_now(boost::posix_time::seconds(1));
freezeDetector->_timer.async_wait(std::bind(&FreezeDetector::Handler, freezeDetectorRef, std::placeholders::_1));
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/server/shared/Network/NetworkThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#ifndef NetworkThread_h__
#define NetworkThread_h__

#include "DeadlineTimer.h"
#include "Define.h"
#include "Errors.h"
#include "IoContext.h"
#include "Log.h"
#include "Socket.h"
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/steady_timer.hpp>
#include <atomic>
#include <boost/asio/ip/tcp.hpp>
#include <memory>
#include <mutex>
#include <set>
Expand Down Expand Up @@ -179,7 +179,7 @@ class NetworkThread
{
LOG_DEBUG("misc", "Network Thread Starting");

_updateTimer.expires_at(std::chrono::steady_clock::now());
_updateTimer.expires_from_now(boost::posix_time::milliseconds(1));
_updateTimer.async_wait([this](boost::system::error_code const&) { Update(); });
_ioContext.run();

Expand All @@ -193,7 +193,7 @@ class NetworkThread
if (_stopped)
return;

_updateTimer.expires_at(std::chrono::steady_clock::now());
_updateTimer.expires_from_now(boost::posix_time::milliseconds(1));
_updateTimer.async_wait([this](boost::system::error_code const&) { Update(); });

AddNewSockets();
Expand Down Expand Up @@ -230,7 +230,7 @@ class NetworkThread

Acore::Asio::IoContext _ioContext;
tcp::socket _acceptSocket;
boost::asio::steady_timer _updateTimer;
Acore::Asio::DeadlineTimer _updateTimer;

bool _proxyHeaderReadingEnabled;
};
Expand Down
7 changes: 3 additions & 4 deletions src/server/shared/Realms/RealmList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "RealmList.h"
#include "DatabaseEnv.h"
#include "DeadlineTimer.h"
#include "Log.h"
#include "Resolver.h"
#include "QueryResult.h"
Expand All @@ -36,7 +37,7 @@ RealmList* RealmList::Instance()
void RealmList::Initialize(Acore::Asio::IoContext& ioContext, uint32 updateInterval)
{
_updateInterval = updateInterval;
_updateTimer = std::make_unique<boost::asio::steady_timer>(ioContext);
_updateTimer = std::make_unique<Acore::Asio::DeadlineTimer>(ioContext);
_resolver = std::make_unique<Acore::Asio::Resolver>(ioContext);

LoadBuildInfo();
Expand Down Expand Up @@ -227,9 +228,7 @@ void RealmList::UpdateRealms(boost::system::error_code const& error)

if (_updateInterval)
{
// Calculate the expiration time _updateInterval from now
auto expiration_time = std::chrono::steady_clock::now() + std::chrono::seconds(_updateInterval);
_updateTimer->expires_at(expiration_time);
_updateTimer->expires_from_now(boost::posix_time::seconds(_updateInterval));
_updateTimer->async_wait([this](boost::system::error_code const& errorCode){ UpdateRealms(errorCode); });
}
}
Expand Down
18 changes: 6 additions & 12 deletions src/server/shared/Realms/RealmList.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,11 @@

#include "Define.h"
#include "Realm.h"
#include <boost/asio/steady_timer.hpp>
#include <array>
#include <map>
#include <memory> // NOTE: this import is NEEDED (even though some IDEs report it as unused)
#include <vector>

namespace Acore::Asio
{
class IoContext;
}

namespace boost::system
{
class error_code;
}

struct RealmBuildInfo
{
uint32 Build;
Expand All @@ -47,6 +36,11 @@ struct RealmBuildInfo
std::array<uint8, 20> MacHash;
};

namespace boost::system
{
class error_code;
}

/// Storage object for the list of realms on the server
class AC_SHARED_API RealmList
{
Expand Down Expand Up @@ -76,7 +70,7 @@ class AC_SHARED_API RealmList
std::vector<RealmBuildInfo> _builds;
RealmMap _realms;
uint32 _updateInterval{0};
std::unique_ptr<boost::asio::steady_timer> _updateTimer;
std::unique_ptr<Acore::Asio::DeadlineTimer> _updateTimer;
std::unique_ptr<Acore::Asio::Resolver> _resolver;
};

Expand Down

0 comments on commit a01e8a6

Please sign in to comment.