From 8e858045d6debbcd0e1d4bcdb9328be09fe72266 Mon Sep 17 00:00:00 2001 From: SergeantPanda Date: Thu, 30 Apr 2026 16:23:51 -0500 Subject: [PATCH] Enhancement: Throttle Redis stats writes in StreamGenerator to reduce traffic during high viewer load --- apps/proxy/ts_proxy/stream_generator.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/apps/proxy/ts_proxy/stream_generator.py b/apps/proxy/ts_proxy/stream_generator.py index df74b8d1..3e73c43e 100644 --- a/apps/proxy/ts_proxy/stream_generator.py +++ b/apps/proxy/ts_proxy/stream_generator.py @@ -66,6 +66,11 @@ class StreamGenerator: self.last_ttl_refresh = time.time() self.ttl_refresh_interval = 3 # Refresh TTL every 3 seconds of active streaming + # Throttle per-client stats writes to Redis. + # channels with many viewers. + self.last_stats_write = 0.0 + self.stats_write_interval = 1.0 + # Cached proxy server reference self.proxy_server = None @@ -448,9 +453,12 @@ class StreamGenerator: logger.debug(f"[{self.client_id}] Stats: {self.chunks_sent} chunks, {self.bytes_sent/1024:.1f} KB, " f"avg: {avg_rate:.1f} KB/s, current: {self.current_rate:.1f} KB/s") - # Store stats in Redis client metadata - if proxy_server.redis_client: + # Store stats in Redis client metadata, throttled to avoid an + # hset on every chunk. Frontend stats panels poll on the order + # of seconds, so 1s resolution is sufficient. + if proxy_server.redis_client and (current_time - self.last_stats_write) >= self.stats_write_interval: try: + self.last_stats_write = current_time client_key = RedisKeys.client_metadata(self.channel_id, self.client_id) stats = { ChannelMetadataField.CHUNKS_SENT: str(self.chunks_sent),