fix memory usage redis

This commit is contained in:
Mateusz Gruszczyński
2026-08-16 22:43:06 +02:00
parent 40474cdc59
commit 074d17be89
22 changed files with 1377 additions and 382 deletions
+21 -10
View File
@@ -12,24 +12,25 @@ from typing import Any
class RedisSupervisor:
"""Run the persistent Redis history service inside the IDS container."""
"""Run the bounded Redis ingestion buffer inside the IDS container."""
def __init__(
self,
enabled: bool,
data_dir: str,
port: int = 6379,
maxmemory_mb: int = 0,
snapshot_seconds: int = 1800,
aof: bool = True,
maxmemory_mb: int = 128,
snapshot_seconds: int = 0,
aof: bool = False,
) -> None:
self.enabled = bool(enabled)
self.data_dir = data_dir
self.port = int(port)
# 0 means unlimited. Traffic retention is time-based; Redis must not evict
# arbitrary history just because an old deployment exported a memory cap.
# Redis is an ingestion buffer. A hard ceiling prevents host OOMK if the
# SQLite archive worker stalls; noeviction makes overload explicit instead
# of silently discarding arbitrary history.
self.maxmemory_mb = max(0, int(maxmemory_mb))
self.snapshot_seconds = max(300, int(snapshot_seconds))
self.snapshot_seconds = max(0, int(snapshot_seconds))
self.aof = bool(aof)
self.executable = shutil.which("redis-server")
self._lock = threading.RLock()
@@ -108,7 +109,12 @@ class RedisSupervisor:
"maxmemory_mb": self.maxmemory_mb,
"snapshot_seconds": self.snapshot_seconds,
"aof": self.aof,
"persistence": "AOF everysec + RDB" if self.aof else "RDB",
"persistence": (
"AOF everysec + RDB" if self.aof and self.snapshot_seconds > 0
else "AOF everysec" if self.aof
else "RDB" if self.snapshot_seconds > 0
else "disabled (SQLite archive is durable)"
),
"last_error": self._last_error,
}
@@ -146,7 +152,12 @@ class RedisSupervisor:
"--bind", "127.0.0.1",
"--protected-mode", "yes",
"--port", str(self.port),
"--save", str(self.snapshot_seconds), "100",
]
if self.snapshot_seconds > 0:
cmd.extend(["--save", str(self.snapshot_seconds), "100"])
else:
cmd.extend(["--save", ""])
cmd.extend([
"--appendonly", "yes" if self.aof else "no",
"--appendfsync", "everysec",
"--aof-use-rdb-preamble", "yes",
@@ -154,7 +165,7 @@ class RedisSupervisor:
"--dbfilename", "traffic.rdb",
"--maxmemory-policy", "noeviction",
"--loglevel", "warning",
]
])
if self.maxmemory_mb > 0:
cmd.extend(["--maxmemory", f"{self.maxmemory_mb}mb"])
else: