This commit is contained in:
azw 2023-09-28 13:13:55 +00:00
parent 24a004ef01
commit 412b1a7731
2 changed files with 27 additions and 39 deletions

View File

@ -3,6 +3,7 @@
#include <a8/mutable_xobject.h> #include <a8/mutable_xobject.h>
#include <f8/utils.h> #include <f8/utils.h>
#include <f8/timer.h>
#include <f8/jsonhttprequest.h> #include <f8/jsonhttprequest.h>
#include "gsmgr.h" #include "gsmgr.h"
@ -24,20 +25,11 @@ struct GSNode
int port = 0; int port = 0;
bool servicing = false; bool servicing = false;
int version = 0; int version = 0;
long long last_active_tick = 0; f8::TimerWp timer_wp;
}; };
void GSMgr::Init() void GSMgr::Init()
{ {
f8::Timer::Instance()->SetIntervalEx
(1000 * 2,
[] (int e, const a8::Args* args)
{
if (e == a8::TIMER_EXEC_EVENT) {
GSMgr::Instance()->ClearTimeOutNode();
}
},
&timer_attacher_);
} }
void GSMgr::UnInit() void GSMgr::UnInit()
@ -115,7 +107,9 @@ void GSMgr::___GSReport(std::shared_ptr<f8::JsonHttpRequest> request)
RearrangeNode(); RearrangeNode();
} }
itr->second->alive_count = alive_count; itr->second->alive_count = alive_count;
itr->second->last_active_tick = a8::XGetTickCount(); if (!itr->second->timer_wp.expired()) {
f8::Timer::Instance()->ModifyTime(itr->second->timer_wp, 1000 * 5);
}
} else { } else {
auto gs = std::make_shared<GSNode>(); auto gs = std::make_shared<GSNode>();
gs->key = key; gs->key = key;
@ -129,7 +123,28 @@ void GSMgr::___GSReport(std::shared_ptr<f8::JsonHttpRequest> request)
gs->port = port; gs->port = port;
gs->servicing = servicing; gs->servicing = servicing;
gs->version = version; gs->version = version;
gs->last_active_tick = a8::XGetTickCount(); gs->timer_wp = f8::Timer::Instance()->SetIntervalWp
(
1000 * 5,
[this, gs] (int e, const a8::Args* args)
{
if (a8::TIMER_EXEC_EVENT == e) {
RemoveNodeFromSortedNodes(gs);
{
std::vector<std::string> deleted_teams;
for (auto& pair : team_hash_) {
if (pair.second == gs) {
deleted_teams.push_back(pair.first);
}
}
for (const std::string& team_id : deleted_teams) {
team_hash_.erase(team_id);
}
}
node_key_hash_.erase(gs->key);
RearrangeNode();
}
});
node_key_hash_[key] = gs; node_key_hash_[key] = gs;
AddNodeToSortedNodes(gs); AddNodeToSortedNodes(gs);
RearrangeNode(); RearrangeNode();
@ -269,32 +284,6 @@ void GSMgr::RearrangeNode()
} }
} }
void GSMgr::ClearTimeOutNode()
{
std::vector<std::shared_ptr<GSNode>> time_out_nodes;
for (auto& pair : node_key_hash_) {
if (a8::XGetTickCount() - pair.second->last_active_tick > 1000 * 5) {
time_out_nodes.push_back(pair.second);
}
}
for (std::shared_ptr<GSNode> node : time_out_nodes) {
RemoveNodeFromSortedNodes(node);
{
std::vector<std::string> deleted_teams;
for (auto& pair : team_hash_) {
if (pair.second == node) {
deleted_teams.push_back(pair.first);
}
}
for (const std::string& team_id : deleted_teams) {
team_hash_.erase(team_id);
}
}
node_key_hash_.erase(node->key);
}
RearrangeNode();
}
void GSMgr::AddNodeToSortedNodes(std::shared_ptr<GSNode> node) void GSMgr::AddNodeToSortedNodes(std::shared_ptr<GSNode> node)
{ {
auto itr = sorted_node_hash_.find(node->version); auto itr = sorted_node_hash_.find(node->version);

View File

@ -34,7 +34,6 @@ class GSMgr : public a8::Singleton<GSMgr>
std::shared_ptr<GSNode> GetNodeByNodeKey(const std::string& node_key); std::shared_ptr<GSNode> GetNodeByNodeKey(const std::string& node_key);
std::shared_ptr<GSNode> AllocNode(); std::shared_ptr<GSNode> AllocNode();
void RearrangeNode(); void RearrangeNode();
void ClearTimeOutNode();
void AddNodeToSortedNodes(std::shared_ptr<GSNode> node); void AddNodeToSortedNodes(std::shared_ptr<GSNode> node);
void RemoveNodeFromSortedNodes(std::shared_ptr<GSNode> node); void RemoveNodeFromSortedNodes(std::shared_ptr<GSNode> node);
std::vector<std::shared_ptr<GSNode>>* GetSortedNodes(); std::vector<std::shared_ptr<GSNode>>* GetSortedNodes();