Issue-108. Track recent packets in unordered_set

Check individual packets seen recently for expiry - and purge.
Otherwise - only scan all of recentPackets for expired once
fill > 75% (of MAX_NUM_NODES).
This commit is contained in:
Audun Foyen
2021-07-20 21:19:35 +02:00
parent 2e1746ca0f
commit 986d44873a
2 changed files with 51 additions and 52 deletions

View File

@@ -1,7 +1,6 @@
#pragma once
#include "Router.h"
#include <queue>
#include <unordered_set>
/// We clear our old flood record five minute after we see the last of it
@@ -24,37 +23,15 @@ class PacketRecordHashFunction
size_t operator()(const PacketRecord &p) const { return (std::hash<NodeNum>()(p.sender)) ^ (std::hash<PacketId>()(p.id)); }
};
/// Order packet records by arrival time, we want the oldest packets to be in the front of our heap
class PacketRecordOrderFunction
{
public:
size_t operator()(const PacketRecord &p1, const PacketRecord &p2) const
{
// If the timer ticks have rolled over the difference between times will be _enormous_. Handle that case specially
uint32_t t1 = p1.rxTimeMsec, t2 = p2.rxTimeMsec;
if (t1 - t2 > UINT32_MAX / 2) {
// time must have rolled over, swap them because the new little number is 'bigger' than the old big number
t1 = t2;
t2 = p1.rxTimeMsec;
}
return t1 > t2;
}
};
/**
* This is a mixin that adds a record of past packets we have seen
*/
class PacketHistory
{
private:
/** FIXME: really should be a std::unordered_set with the key being sender,id.
* This would make checking packets in wasSeenRecently faster.
*/
std::vector<PacketRecord> recentPackets;
// priority_queue<PacketRecord, vector<PacketRecord>, PacketRecordOrderFunction> arrivalTimes;
// unordered_set<PacketRecord, PacketRecordHashFunction> recentPackets;
std::unordered_set<PacketRecord, PacketRecordHashFunction> recentPackets;
void clearExpiredRecentPackets(); // clear all recentPackets older than FLOOD_EXPIRE_TIME
public:
PacketHistory();