@@ -63,6 +63,27 @@ async def broadcast_json(self, data: dict):
6363
6464ws_manager = WSManager ()
6565
66+ # Configuration: retention and capacity limits
67+ MAX_REQUESTS = int (os .getenv ("INTERCEPTER_MAX_REQUESTS" , "100" ))
68+ RETENTION_SECONDS = int (os .getenv ("INTERCEPTER_RETENTION_SECONDS" , str (24 * 60 * 60 )))
69+
70+
71+ def _prune_requests () -> None :
72+ """Apply time-based retention and max-capacity trimming.
73+
74+ - Drop requests older than RETENTION_SECONDS
75+ - Keep only most recent MAX_REQUESTS
76+ """
77+ global _requests
78+ now_ts = datetime .now (timezone .utc ).timestamp ()
79+ if RETENTION_SECONDS > 0 :
80+ cutoff = now_ts - RETENTION_SECONDS
81+ _requests = [r for r in _requests if r .ts >= cutoff ]
82+ if MAX_REQUESTS > 0 and len (_requests ) > MAX_REQUESTS :
83+ # keep most recent by timestamp
84+ _requests .sort (key = lambda r : r .ts )
85+ _requests = _requests [- MAX_REQUESTS :]
86+
6687
6788@app .api_route ("/inbound" , methods = ["GET" , "POST" , "PUT" , "PATCH" , "DELETE" , "OPTIONS" , "HEAD" ])
6889async def inbound (request : Request ):
@@ -104,6 +125,7 @@ async def inbound(request: Request):
104125 )
105126 _requests .append (item )
106127 _next_id += 1
128+ _prune_requests ()
107129 # Broadcast summary to websocket listeners
108130 summary = RequestSummary (
109131 id = item .id ,
@@ -119,6 +141,7 @@ async def inbound(request: Request):
119141
120142@app .get ("/api/requests" , response_model = List [RequestSummary ])
121143async def list_requests ():
144+ _prune_requests ()
122145 return [
123146 RequestSummary (
124147 id = r .id ,
0 commit comments