Coverage for src / local_deep_research / web / auth / middleware_optimizer.py: 90%
25 statements
« prev ^ index » next coverage.py v7.12.0, created at 2026-01-11 00:51 +0000
« prev ^ index » next coverage.py v7.12.0, created at 2026-01-11 00:51 +0000
1"""
2Middleware optimization to skip unnecessary checks for static files and public routes.
3"""
5from flask import request
8def should_skip_database_middleware():
9 """
10 Determine if the current request should skip database middleware.
11 Returns True for requests that don't need database access.
12 """
13 path = request.path
15 # Skip for static files
16 if path.startswith("/static/"):
17 return True
19 # Skip for favicon, robots.txt, etc
20 if path in ["/favicon.ico", "/robots.txt", "/health"]:
21 return True
23 # Skip for Socket.IO polling/websocket
24 if path.startswith("/socket.io/"): 24 ↛ 25line 24 didn't jump to line 25 because the condition on line 24 was never true
25 return True
27 # Skip for public auth routes that don't need existing database
28 if path in ["/auth/login", "/auth/register", "/auth/logout"]:
29 return True
31 # Skip for preflight CORS requests
32 if request.method == "OPTIONS": 32 ↛ 33line 32 didn't jump to line 33 because the condition on line 32 was never true
33 return True
35 return False
38def should_skip_queue_checks():
39 """
40 Determine if the current request should skip queue processing checks.
41 """
42 # Skip all GET requests - they don't create new work
43 if request.method == "GET":
44 return True
46 # Skip if already skipping database middleware
47 if should_skip_database_middleware():
48 return True
50 return False
53def should_skip_session_cleanup():
54 """
55 Determine if the current request should skip session cleanup.
56 Session cleanup should only run occasionally, not on every request.
57 """
58 import random
60 # Only run cleanup 1% of the time (1 in 100 requests)
61 if random.randint(1, 100) > 1:
62 return True
64 # Or skip based on the same rules as database middleware
65 return should_skip_database_middleware()