|
1 | 1 | import asyncio |
2 | 2 | import logging |
3 | | -from typing import Any, Awaitable, Callable, Coroutine |
| 3 | +from typing import Any, Awaitable, Callable, Coroutine, TypeAlias |
4 | 4 |
|
5 | 5 | import nanoid |
6 | 6 | import websockets |
|
37 | 37 | trace_propagator = TraceContextTextMapPropagator() |
38 | 38 | trace_setter = TransportMessageTracingSetter() |
39 | 39 |
|
| 40 | +CloseSessionCallback: TypeAlias = Callable[["Session"], Coroutine[Any, Any, Any]] |
| 41 | +RetryConnectionCallback: TypeAlias = Callable[ |
| 42 | + [], |
| 43 | + Coroutine[Any, Any, Any], |
| 44 | +] |
| 45 | + |
40 | 46 |
|
41 | 47 | class Session: |
42 | 48 | """Common functionality shared between client_session and server_session""" |
43 | 49 |
|
| 50 | + _transport_id: str |
| 51 | + _to_id: str |
| 52 | + session_id: str |
| 53 | + _transport_options: TransportOptions |
| 54 | + |
| 55 | + # session state |
| 56 | + _state: SessionState |
| 57 | + _state_lock: asyncio.Lock |
| 58 | + _close_session_callback: CloseSessionCallback |
| 59 | + _close_session_after_time_secs: float | None |
| 60 | + |
| 61 | + # ws state |
| 62 | + _ws_lock: asyncio.Lock |
| 63 | + _ws_wrapper: WebsocketWrapper |
| 64 | + _heartbeat_misses: int |
| 65 | + _retry_connection_callback: RetryConnectionCallback | None |
| 66 | + |
| 67 | + # stream for tasks |
| 68 | + _streams: dict[str, Channel[Any]] |
| 69 | + |
| 70 | + # book keeping |
| 71 | + _seq_manager: SeqManager |
| 72 | + _msg_lock: asyncio.Lock |
| 73 | + _buffer: MessageBuffer |
| 74 | + _task_manager: BackgroundTaskManager |
| 75 | + |
44 | 76 | def __init__( |
45 | 77 | self, |
46 | 78 | transport_id: str, |
47 | 79 | to_id: str, |
48 | 80 | session_id: str, |
49 | 81 | websocket: websockets.WebSocketCommonProtocol, |
50 | 82 | transport_options: TransportOptions, |
51 | | - close_session_callback: Callable[["Session"], Coroutine[Any, Any, Any]], |
52 | | - retry_connection_callback: ( |
53 | | - Callable[ |
54 | | - [], |
55 | | - Coroutine[Any, Any, Any], |
56 | | - ] |
57 | | - | None |
58 | | - ) = None, |
| 83 | + close_session_callback: CloseSessionCallback, |
| 84 | + retry_connection_callback: RetryConnectionCallback | None = None, |
59 | 85 | ) -> None: |
60 | 86 | self._transport_id = transport_id |
61 | 87 | self._to_id = to_id |
|
0 commit comments