Added PacketList.set_maxlen()

If you want a constructor time member to have a
value you have to set it after the stats collector
registration is done because it will only be the default
since the CONF isn't setup at that point yet.
This commit is contained in:
Hemna 2024-04-15 21:39:45 -04:00
parent 3e8716365e
commit 4542c0a643
1 changed files with 8 additions and 8 deletions

View File

@ -18,12 +18,12 @@ class PacketList(objectstore.ObjectStoreMixin):
lock = threading.Lock() lock = threading.Lock()
_total_rx: int = 0 _total_rx: int = 0
_total_tx: int = 0 _total_tx: int = 0
_maxlen: int = 100 maxlen: int = 100
def __new__(cls, *args, **kwargs): def __new__(cls, *args, **kwargs):
if cls._instance is None: if cls._instance is None:
cls._instance = super().__new__(cls) cls._instance = super().__new__(cls)
cls._instance._maxlen = CONF.packet_list_maxlen cls._instance.maxlen = CONF.packet_list_maxlen
cls._instance.data = { cls._instance.data = {
"types": {}, "types": {},
"packets": OrderedDict(), "packets": OrderedDict(),
@ -59,7 +59,7 @@ class PacketList(objectstore.ObjectStoreMixin):
def _add(self, packet): def _add(self, packet):
if packet.key in self.data["packets"]: if packet.key in self.data["packets"]:
self.data["packets"].move_to_end(packet.key) self.data["packets"].move_to_end(packet.key)
elif len(self.data["packets"]) == self._maxlen: elif len(self.data["packets"]) == self.maxlen:
self.data["packets"].popitem(last=False) self.data["packets"].popitem(last=False)
self.data["packets"][packet.key] = packet self.data["packets"][packet.key] = packet
@ -67,9 +67,9 @@ class PacketList(objectstore.ObjectStoreMixin):
def copy(self): def copy(self):
return self.data.copy() return self.data.copy()
@property @wrapt.synchronized(lock)
def maxlen(self): def set_maxlen(self, maxlen):
return self._maxlen self.maxlen = maxlen
@wrapt.synchronized(lock) @wrapt.synchronized(lock)
def find(self, packet): def find(self, packet):
@ -90,8 +90,7 @@ class PacketList(objectstore.ObjectStoreMixin):
@wrapt.synchronized(lock) @wrapt.synchronized(lock)
def stats(self, serializable=False) -> dict: def stats(self, serializable=False) -> dict:
# limit the number of packets to return to 50 # limit the number of packets to return to 50
LOG.info(f"PacketList stats called len={len(self.data['packets'])}") tmp = OrderedDict(reversed(list(self.data.get("packets", []).items())))
tmp = OrderedDict(reversed(list(self.data["packets"].items())))
pkts = [] pkts = []
count = 1 count = 1
for packet in tmp: for packet in tmp:
@ -106,6 +105,7 @@ class PacketList(objectstore.ObjectStoreMixin):
"tx": self._total_tx, "tx": self._total_tx,
"types": self.data["types"], "types": self.data["types"],
"packet_count": len(self.data["packets"]), "packet_count": len(self.data["packets"]),
"maxlen": self.maxlen,
"packets": pkts, "packets": pkts,
} }
return stats return stats