From 93a9cce0c0d0d1d1195e591cbd7ab9583a33c0c9 Mon Sep 17 00:00:00 2001 From: Hemna Date: Tue, 7 May 2024 19:58:11 -0400 Subject: [PATCH] Put an upper bound on the QueueHandler queue This patch overrides the base QueueHandler class from logging to ensure that the queue doesn't grow infinitely. That can be a problem when there is no consumer pulling items out of the queue. the queue is now capped at 200 entries max. --- ChangeLog | 1 + aprsd/log/log.py | 21 ++++++++++++++++++++- aprsd/stats/app.py | 3 +++ aprsd/threads/keep_alive.py | 4 +++- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 736f5f6..01fd2df 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,7 @@ CHANGES v3.4.0 ------ +* Updated Changelog for 3.4.0 * Change setup.h * Fixed docker setup.sh comparison * Fixed unit tests failing with WatchList diff --git a/aprsd/log/log.py b/aprsd/log/log.py index 2c34e12..e5c20d5 100644 --- a/aprsd/log/log.py +++ b/aprsd/log/log.py @@ -12,7 +12,22 @@ from aprsd.conf import log as conf_log CONF = cfg.CONF # LOG = logging.getLogger("APRSD") LOG = logger -logging_queue = queue.Queue() + + +class QueueLatest(queue.Queue): + """Custom Queue to keep only the latest N items. + + This prevents the queue from blowing up in size. + """ + def put(self, *args, **kwargs): + try: + super().put(*args, **kwargs) + except queue.Full: + self.queue.popleft() + super().put(*args, **kwargs) + + +logging_queue = QueueLatest(maxsize=200) class InterceptHandler(logging.Handler): @@ -59,6 +74,10 @@ def setup_logging(loglevel=None, quiet=False): "werkzeug._internal", "socketio", "urllib3.connectionpool", + "chardet", + "chardet.charsetgroupprober", + "chardet.eucjpprober", + "chardet.mbcharsetprober", ] # We don't really want to see the aprslib parsing debug output. diff --git a/aprsd/stats/app.py b/aprsd/stats/app.py index 1308510..0e64ff6 100644 --- a/aprsd/stats/app.py +++ b/aprsd/stats/app.py @@ -5,6 +5,7 @@ from oslo_config import cfg import aprsd from aprsd import utils +from aprsd.log import log as aprsd_log CONF = cfg.CONF @@ -32,6 +33,7 @@ class APRSDStats: def stats(self, serializable=False) -> dict: current, peak = tracemalloc.get_traced_memory() uptime = self.uptime() + qsize = aprsd_log.logging_queue.qsize() if serializable: uptime = str(uptime) stats = { @@ -42,5 +44,6 @@ class APRSDStats: "memory_current_str": utils.human_size(current), "memory_peak": int(peak), "memory_peak_str": utils.human_size(peak), + "loging_queue": qsize, } return stats diff --git a/aprsd/threads/keep_alive.py b/aprsd/threads/keep_alive.py index 3b1555a..c40cb0d 100644 --- a/aprsd/threads/keep_alive.py +++ b/aprsd/threads/keep_alive.py @@ -6,6 +6,7 @@ import tracemalloc from oslo_config import cfg from aprsd import client, packets, utils +from aprsd.log import log as aprsd_log from aprsd.stats import collector from aprsd.threads import APRSDThread, APRSDThreadList @@ -59,7 +60,7 @@ class KeepAliveThread(APRSDThread): keepalive = ( "{} - Uptime {} RX:{} TX:{} Tracker:{} Msgs TX:{} RX:{} " - "Last:{} Email: {} - RAM Current:{} Peak:{} Threads:{}" + "Last:{} Email: {} - RAM Current:{} Peak:{} Threads:{} LoggingQueue:{}" ).format( stats_json["APRSDStats"]["callsign"], stats_json["APRSDStats"]["uptime"], @@ -73,6 +74,7 @@ class KeepAliveThread(APRSDThread): stats_json["APRSDStats"]["memory_current_str"], stats_json["APRSDStats"]["memory_peak_str"], len(thread_list), + aprsd_log.logging_queue.qsize(), ) LOG.info(keepalive) if "APRSDThreadList" in stats_json: