ensure stale peer status entries are erased after a period of inactivity;

pull/121/merge
Bryan Biedenkapp 1 month ago
parent 6c9883a084
commit 6d15d94379

@ -310,6 +310,22 @@ void* threadNetworkPump(void* arg)
if (g_network != nullptr) {
g_network->clock(ms);
// clock peer status timers and remove expired entries from the peer status map
g_network->lockPeerStatus();
for (auto it = g_network->peerStatusTimers.begin(); it != g_network->peerStatusTimers.end();) {
if (it->second.isRunning() && it->second.hasExpired()) {
uint32_t peerId = it->first;
it = g_network->peerStatusTimers.erase(it);
g_network->peerStatus.erase(peerId);
g_network->peerStatusTimers[peerId].stop();
LogInfoEx(LOG_HOST, "peer status expired, peerId = %u", peerId);
}
else {
++it;
}
}
g_network->unlockPeerStatus();
hrc::hrc_t pktTime = hrc::now();
uint32_t length = 0U;

@ -22,6 +22,12 @@ using namespace network;
#include <fstream>
#include <streambuf>
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const uint32_t PEER_STATUS_EXPIRY = 60000U; // 60 seconds
// ---------------------------------------------------------------------------
// Static Class Members
// ---------------------------------------------------------------------------
@ -38,6 +44,7 @@ PeerNetwork::PeerNetwork(const std::string& address, uint16_t port, uint16_t loc
bool duplex, bool debug, bool allowActivityTransfer, bool allowDiagnosticTransfer, bool updateLookup, bool saveLookup) :
Network(address, port, localPort, peerId, password, duplex, debug, true, true, true, true, true, true, allowActivityTransfer, allowDiagnosticTransfer, updateLookup, saveLookup),
peerStatus(),
peerStatusTimers(),
m_peerReplica(false),
m_tgidPkt(true, "Peer Replication, TGID List"),
m_ridPkt(true, "Peer Replication, RID List")
@ -107,6 +114,7 @@ void PeerNetwork::userPacketHandler(uint32_t peerId, FrameQueue::OpcodePair opco
uint32_t actualPeerId = obj["peerId"].getDefault<uint32_t>(peerId);
std::lock_guard<std::mutex> lock(s_peerStatusMutex);
peerStatus[actualPeerId] = obj;
peerStatusTimers[actualPeerId].start(PEER_STATUS_EXPIRY);
}
break;

@ -74,6 +74,11 @@ namespace network
* @brief Map of peer status.
*/
std::unordered_map<uint32_t, json::object> peerStatus;
/**
* @brief Map of peer status timers, used to track when peer status entries should be expired and
* removed from the map.
*/
std::unordered_map<uint32_t, Timer> peerStatusTimers;
protected:
/**

Loading…
Cancel
Save

Powered by TurnKey Linux.