Server: Fix blocklist loading - only once at startup
- Move public blocklist loading into lifespan (not on import) - Avoids loading 8 times with 4 workers - Cleaner startup logs
This commit is contained in:
+12
-14
@@ -33,20 +33,9 @@ BUILDS_DIR = Path("builds")
|
|||||||
import os
|
import os
|
||||||
import middleware as mw
|
import middleware as mw
|
||||||
|
|
||||||
# Manually blocked IPs
|
# Only configure manually blocked IPs at import time
|
||||||
BLOCKED_IPS = set(os.environ.get("BLOCKED_IPS", "").split(",")) - {""}
|
# Public blocklists are loaded in lifespan (once, not per-worker)
|
||||||
|
MANUAL_BLOCKED_IPS = set(os.environ.get("BLOCKED_IPS", "").split(",")) - {""}
|
||||||
# Load public blocklists (set to "false" to disable)
|
|
||||||
USE_PUBLIC_BLOCKLIST = os.environ.get("PUBLIC_BLOCKLIST", "true").lower() == "true"
|
|
||||||
|
|
||||||
if USE_PUBLIC_BLOCKLIST:
|
|
||||||
public_ips = mw.load_public_blocklists()
|
|
||||||
BLOCKED_IPS.update(public_ips)
|
|
||||||
|
|
||||||
if BLOCKED_IPS:
|
|
||||||
logger.info(f"Total blocked IPs: {len(BLOCKED_IPS)}")
|
|
||||||
|
|
||||||
mw.set_ip_config(blocked=BLOCKED_IPS)
|
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
@@ -56,6 +45,15 @@ async def lifespan(app: FastAPI):
|
|||||||
# Initialize logging
|
# Initialize logging
|
||||||
init_logging()
|
init_logging()
|
||||||
|
|
||||||
|
# Load public blocklists (only once, in main process)
|
||||||
|
USE_PUBLIC_BLOCKLIST = os.environ.get("PUBLIC_BLOCKLIST", "true").lower() == "true"
|
||||||
|
all_blocked = set(MANUAL_BLOCKED_IPS)
|
||||||
|
if USE_PUBLIC_BLOCKLIST:
|
||||||
|
public_ips = mw.load_public_blocklists()
|
||||||
|
all_blocked.update(public_ips)
|
||||||
|
mw.set_ip_config(blocked=all_blocked)
|
||||||
|
logger.info(f"IP blocklist loaded: {len(all_blocked)} IPs")
|
||||||
|
|
||||||
# Determine environment
|
# Determine environment
|
||||||
if args.test:
|
if args.test:
|
||||||
env = "test"
|
env = "test"
|
||||||
|
|||||||
Reference in New Issue
Block a user