Skip to content

Commit

Permalink
refactor(Core/Misc): Use steady_timer instead of deadline_timer (azer…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kitzunu authored Dec 19, 2024
1 parent ea02be9 commit 0bc7067
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 78 deletions.
34 changes: 0 additions & 34 deletions src/common/Asio/DeadlineTimer.h

This file was deleted.

5 changes: 2 additions & 3 deletions src/common/Asio/IoContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#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 @@ -52,9 +51,9 @@ namespace Acore::Asio
}

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

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

#include "Metric.h"
#include "Config.h"
#include "DeadlineTimer.h"
#include "Log.h"
#include "Strand.h"
#include "Tokenize.h"
Expand All @@ -42,8 +41,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<Acore::Asio::DeadlineTimer>(ioContext);
_overallStatusTimer = std::make_unique<Acore::Asio::DeadlineTimer>(ioContext);
_batchTimer = std::make_unique<boost::asio::steady_timer>(ioContext);
_overallStatusTimer = std::make_unique<boost::asio::steady_timer>(ioContext);
_overallStatusLogger = overallStatusLogger;
LoadFromConfigs();
}
Expand Down Expand Up @@ -247,7 +246,9 @@ void Metric::ScheduleSend()
{
if (_enabled)
{
_batchTimer->expires_from_now(boost::posix_time::seconds(_updateInterval));
// Calculate the expiration time
auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(_updateInterval);
_batchTimer->expires_at(expirationTime);
_batchTimer->async_wait(std::bind(&Metric::SendBatch, this));
}
else
Expand Down Expand Up @@ -280,7 +281,9 @@ void Metric::ScheduleOverallStatusLog()
{
if (_enabled)
{
_overallStatusTimer->expires_from_now(boost::posix_time::seconds(_overallStatusTimerInterval));
// Calculate the expiration time _overallStatusTimerInterval from now
auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(_overallStatusTimerInterval);
_overallStatusTimer->expires_at(expirationTime);
_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,6 +21,7 @@
#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 @@ -30,7 +31,6 @@
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<Acore::Asio::DeadlineTimer> _batchTimer;
std::unique_ptr<Acore::Asio::DeadlineTimer> _overallStatusTimer;
std::unique_ptr<boost::asio::steady_timer> _batchTimer;
std::unique_ptr<boost::asio::steady_timer> _overallStatusTimer;
int32 _updateInterval = 0;
int32 _overallStatusTimerInterval = 0;
bool _enabled = false;
Expand Down
39 changes: 24 additions & 15 deletions src/server/apps/authserver/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#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 @@ -60,8 +59,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<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);
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);
variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile);

/// Launch the auth server
Expand Down Expand Up @@ -178,14 +177,20 @@ 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<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));
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));

int32 banExpiryCheckInterval = sConfigMgr->GetOption<int32>("BanExpiryCheckInterval", 60);
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));
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));

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

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

dbPingTimer->expires_from_now(boost::posix_time::minutes(dbPingInterval));
// 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, dbPingTimerRef, dbPingInterval, std::placeholders::_1));
}
}
}

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

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

static void Start(std::shared_ptr<FreezeDetector> const& freezeDetector)
{
freezeDetector->_timer.expires_from_now(boost::posix_time::seconds(5));
// 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.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:
Acore::Asio::DeadlineTimer _timer;
boost::asio::steady_timer _timer;
uint32 _worldLoopCounter;
uint32 _lastChangeMsTime;
uint32 _maxCoreStuckTimeInMs;
Expand Down Expand Up @@ -631,7 +632,9 @@ void FreezeDetector::Handler(std::weak_ptr<FreezeDetector> freezeDetectorRef, bo
}
}

freezeDetector->_timer.expires_from_now(boost::posix_time::seconds(1));
// Calculate the expiration time
auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(1);
freezeDetector->_timer.expires_at(expirationTime);
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 <atomic>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/steady_timer.hpp>
#include <atomic>
#include <memory>
#include <mutex>
#include <set>
Expand Down Expand Up @@ -179,7 +179,7 @@ class NetworkThread
{
LOG_DEBUG("misc", "Network Thread Starting");

_updateTimer.expires_from_now(boost::posix_time::milliseconds(1));
_updateTimer.expires_at(std::chrono::steady_clock::now());
_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_from_now(boost::posix_time::milliseconds(1));
_updateTimer.expires_at(std::chrono::steady_clock::now());
_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;
Acore::Asio::DeadlineTimer _updateTimer;
boost::asio::steady_timer _updateTimer;

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

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

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

if (_updateInterval)
{
_updateTimer->expires_from_now(boost::posix_time::seconds(_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->async_wait([this](boost::system::error_code const& errorCode){ UpdateRealms(errorCode); });
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/server/shared/Realms/RealmList.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@

#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 @@ -36,11 +47,6 @@ 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 @@ -70,7 +76,7 @@ class AC_SHARED_API RealmList
std::vector<RealmBuildInfo> _builds;
RealmMap _realms;
uint32 _updateInterval{0};
std::unique_ptr<Acore::Asio::DeadlineTimer> _updateTimer;
std::unique_ptr<boost::asio::steady_timer> _updateTimer;
std::unique_ptr<Acore::Asio::Resolver> _resolver;
};

Expand Down

0 comments on commit 0bc7067

Please sign in to comment.