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