This commit is contained in:
aozhiwei 2019-06-27 10:50:03 +08:00
parent dfa6dfb9cc
commit e75261e41e
2 changed files with 43 additions and 1 deletions

View File

@ -1,13 +1,19 @@
#include "precompile.h"
#include <a8/mutable_xobject.h>
#include <a8/timer.h>
#include "gsmgr.h"
#include "GGListener.h"
void GSMgr::Init()
{
a8::Timer::Instance()->AddRepeatTimer(1000 * 2,
a8::XParams(),
[] (const a8::XParams& param)
{
GSMgr::Instance()->ClearTimeOutNode();
});
}
void GSMgr::UnInit()
@ -59,6 +65,7 @@ void GSMgr::___GSReport(f8::JsonHttpRequest* request)
itr->second.servicing = servicing;
RearrangeNode();
}
itr->second.last_active_tick = a8::XGetTickCount();
} else {
GSNode gs;
gs.node_id = node_id;
@ -68,6 +75,7 @@ void GSMgr::___GSReport(f8::JsonHttpRequest* request)
gs.ip = ip;
gs.port = port;
gs.servicing = servicing;
gs.last_active_tick = a8::XGetTickCount();
node_key_hash_[key] = gs;
node_sorted_list_.push_back(&node_key_hash_[key]);
RearrangeNode();
@ -128,3 +136,35 @@ void GSMgr::RearrangeNode()
}
);
}
void GSMgr::ClearTimeOutNode()
{
std::vector<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 (GSNode* node : time_out_nodes) {
{
for (size_t i = 0; i < node_sorted_list_.size(); ++i) {
if (node_sorted_list_[i] == node) {
node_sorted_list_.erase(node_sorted_list_.begin() + i);
break;
}
}
}
{
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);
}
}

View File

@ -13,6 +13,7 @@ struct GSNode
std::string ip;
int port = 0;
bool servicing = false;
long long last_active_tick = 0;
};
class GSMgr : public a8::Singleton<GSMgr>
@ -38,6 +39,7 @@ class GSMgr : public a8::Singleton<GSMgr>
GSNode* GetNodeByTeamId(const std::string& team_id);
GSNode* AllocNode();
void RearrangeNode();
void ClearTimeOutNode();
private: