11"""In-memory registry for interrupting active streaming requests."""
22
33import asyncio
4- import logging
4+ from log import get_logger
55from dataclasses import dataclass
66from threading import Lock
7- from typing import Optional
7+ from typing import Any
88
9- logger = logging . getLogger (__name__ )
9+ logger = get_logger (__name__ )
1010
1111
1212@dataclass
1313class ActiveStream :
14- """Represents one active streaming request bound to a user."""
14+ """Represents one active streaming request bound to a user.
15+
16+ Attributes:
17+ user_id: Owner of the streaming request.
18+ task: Asyncio task producing the stream response.
19+ """
1520
1621 user_id : str
1722 task : asyncio .Task
@@ -28,7 +33,13 @@ def __init__(self) -> None:
2833 def register_stream (
2934 self , request_id : str , user_id : str , task : asyncio .Task
3035 ) -> None :
31- """Register an active stream task for interrupt support."""
36+ """Register an active stream task for interrupt support.
37+
38+ Parameters:
39+ request_id: Unique streaming request identifier.
40+ user_id: User identifier that owns the stream.
41+ task: Asyncio task associated with the stream.
42+ """
3243 with self ._lock :
3344 self ._streams [request_id ] = ActiveStream (user_id = user_id , task = task )
3445
@@ -39,6 +50,10 @@ def cancel_stream(self, request_id: str, user_id: str) -> bool:
3950 lock so that a concurrent ``deregister_stream`` cannot remove
4051 the entry between the ownership check and the cancel call.
4152
53+ Parameters:
54+ request_id: Unique streaming request identifier.
55+ user_id: User identifier attempting the interruption.
56+
4257 Returns:
4358 bool: True when cancellation was requested, otherwise False.
4459 """
@@ -59,12 +74,23 @@ def cancel_stream(self, request_id: str, user_id: str) -> bool:
5974 return True
6075
6176 def deregister_stream (self , request_id : str ) -> None :
62- """Remove stream task from registry once completed/cancelled."""
77+ """Remove stream task from registry once completed/cancelled.
78+
79+ Parameters:
80+ request_id: Unique streaming request identifier.
81+ """
6382 with self ._lock :
6483 self ._streams .pop (request_id , None )
6584
66- def get_stream (self , request_id : str ) -> Optional [ActiveStream ]:
67- """Get currently registered stream metadata for tests/introspection."""
85+ def get_stream (self , request_id : str ) -> ActiveStream | None :
86+ """Get currently registered stream metadata for tests/introspection.
87+
88+ Parameters:
89+ request_id: Unique streaming request identifier.
90+
91+ Returns:
92+ ActiveStream | None: Registered stream metadata, or None when absent.
93+ """
6894 with self ._lock :
6995 return self ._streams .get (request_id )
7096
0 commit comments