|
| 1 | +import asyncio |
1 | 2 | import logging |
| 3 | +from typing import Any |
2 | 4 |
|
| 5 | +from aiochannel import Channel, ChannelClosed |
3 | 6 | from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator |
| 7 | +from websockets.exceptions import ConnectionClosed |
4 | 8 |
|
| 9 | +from replit_river.messages import ( |
| 10 | + FailedSendingMessageException, |
| 11 | + parse_transport_msg, |
| 12 | +) |
| 13 | +from replit_river.seq_manager import ( |
| 14 | + IgnoreMessageException, |
| 15 | + InvalidMessageException, |
| 16 | + OutOfOrderMessageException, |
| 17 | +) |
5 | 18 | from replit_river.session import Session |
| 19 | +from replit_river.transport_options import MAX_MESSAGE_BUFFER_SIZE |
6 | 20 |
|
7 | 21 | from .rpc import ( |
| 22 | + ACK_BIT, |
| 23 | + STREAM_CLOSED_BIT, |
| 24 | + STREAM_OPEN_BIT, |
| 25 | + TransportMessage, |
8 | 26 | TransportMessageTracingSetter, |
9 | 27 | ) |
10 | 28 |
|
| 29 | +logger = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
11 | 32 | logger = logging.getLogger(__name__) |
12 | 33 |
|
13 | 34 | trace_propagator = TraceContextTextMapPropagator() |
|
17 | 38 | class ServerSession(Session): |
18 | 39 | """A transport object that handles the websocket connection with a client.""" |
19 | 40 |
|
20 | | - pass |
| 41 | + async def start_serve_responses(self) -> None: |
| 42 | + self._task_manager.create_task(self.serve()) |
| 43 | + |
| 44 | + async def serve(self) -> None: |
| 45 | + """Serve messages from the websocket.""" |
| 46 | + self._reset_session_close_countdown() |
| 47 | + try: |
| 48 | + async with asyncio.TaskGroup() as tg: |
| 49 | + try: |
| 50 | + await self._handle_messages_from_ws(tg) |
| 51 | + except ConnectionClosed: |
| 52 | + if self._retry_connection_callback: |
| 53 | + self._task_manager.create_task( |
| 54 | + self._retry_connection_callback() |
| 55 | + ) |
| 56 | + |
| 57 | + await self._begin_close_session_countdown() |
| 58 | + logger.debug("ConnectionClosed while serving", exc_info=True) |
| 59 | + except FailedSendingMessageException: |
| 60 | + # Expected error if the connection is closed. |
| 61 | + logger.debug( |
| 62 | + "FailedSendingMessageException while serving", exc_info=True |
| 63 | + ) |
| 64 | + except Exception: |
| 65 | + logger.exception("caught exception at message iterator") |
| 66 | + except ExceptionGroup as eg: |
| 67 | + _, unhandled = eg.split(lambda e: isinstance(e, ConnectionClosed)) |
| 68 | + if unhandled: |
| 69 | + raise ExceptionGroup( |
| 70 | + "Unhandled exceptions on River server", unhandled.exceptions |
| 71 | + ) |
| 72 | + |
| 73 | + async def _update_book_keeping(self, msg: TransportMessage) -> None: |
| 74 | + await self._seq_manager.check_seq_and_update(msg) |
| 75 | + await self._remove_acked_messages_in_buffer() |
| 76 | + self._reset_session_close_countdown() |
| 77 | + |
| 78 | + async def _handle_messages_from_ws( |
| 79 | + self, tg: asyncio.TaskGroup | None = None |
| 80 | + ) -> None: |
| 81 | + logger.debug( |
| 82 | + "%s start handling messages from ws %s", |
| 83 | + "server", |
| 84 | + self._ws_wrapper.id, |
| 85 | + ) |
| 86 | + try: |
| 87 | + ws_wrapper = self._ws_wrapper |
| 88 | + async for message in ws_wrapper.ws: |
| 89 | + try: |
| 90 | + if not await ws_wrapper.is_open(): |
| 91 | + # We should not process messages if the websocket is closed. |
| 92 | + break |
| 93 | + msg = parse_transport_msg(message, self._transport_options) |
| 94 | + |
| 95 | + logger.debug(f"{self._transport_id} got a message %r", msg) |
| 96 | + |
| 97 | + await self._update_book_keeping(msg) |
| 98 | + if msg.controlFlags & ACK_BIT != 0: |
| 99 | + continue |
| 100 | + async with self._stream_lock: |
| 101 | + stream = self._streams.get(msg.streamId, None) |
| 102 | + if msg.controlFlags & STREAM_OPEN_BIT == 0: |
| 103 | + if not stream: |
| 104 | + logger.warning("no stream for %s", msg.streamId) |
| 105 | + raise IgnoreMessageException( |
| 106 | + "no stream for message, ignoring" |
| 107 | + ) |
| 108 | + await self._add_msg_to_stream(msg, stream) |
| 109 | + else: |
| 110 | + # TODO(dstewart) This looks like it opens a new call to handler |
| 111 | + # on ever ws message, instead of demuxing and |
| 112 | + # routing. |
| 113 | + _stream = await self._open_stream_and_call_handler(msg, tg) |
| 114 | + if not stream: |
| 115 | + async with self._stream_lock: |
| 116 | + self._streams[msg.streamId] = _stream |
| 117 | + stream = _stream |
| 118 | + |
| 119 | + if msg.controlFlags & STREAM_CLOSED_BIT != 0: |
| 120 | + if stream: |
| 121 | + stream.close() |
| 122 | + async with self._stream_lock: |
| 123 | + del self._streams[msg.streamId] |
| 124 | + except IgnoreMessageException: |
| 125 | + logger.debug("Ignoring transport message", exc_info=True) |
| 126 | + continue |
| 127 | + except OutOfOrderMessageException: |
| 128 | + logger.exception("Out of order message, closing connection") |
| 129 | + await ws_wrapper.close() |
| 130 | + return |
| 131 | + except InvalidMessageException: |
| 132 | + logger.exception("Got invalid transport message, closing session") |
| 133 | + await self.close() |
| 134 | + return |
| 135 | + except ConnectionClosed as e: |
| 136 | + raise e |
| 137 | + |
| 138 | + async def _open_stream_and_call_handler( |
| 139 | + self, |
| 140 | + msg: TransportMessage, |
| 141 | + tg: asyncio.TaskGroup | None, |
| 142 | + ) -> Channel: |
| 143 | + if not msg.serviceName or not msg.procedureName: |
| 144 | + raise IgnoreMessageException( |
| 145 | + f"Service name or procedure name is missing in the message {msg}" |
| 146 | + ) |
| 147 | + key = (msg.serviceName, msg.procedureName) |
| 148 | + handler = self._handlers.get(key, None) |
| 149 | + if not handler: |
| 150 | + raise IgnoreMessageException( |
| 151 | + f"No handler for {key} handlers : {self._handlers.keys()}" |
| 152 | + ) |
| 153 | + method_type, handler_func = handler |
| 154 | + is_streaming_output = method_type in ( |
| 155 | + "subscription-stream", # subscription |
| 156 | + "stream", |
| 157 | + ) |
| 158 | + is_streaming_input = method_type in ( |
| 159 | + "upload-stream", # subscription |
| 160 | + "stream", |
| 161 | + ) |
| 162 | + # New channel pair. |
| 163 | + input_stream: Channel[Any] = Channel( |
| 164 | + MAX_MESSAGE_BUFFER_SIZE if is_streaming_input else 1 |
| 165 | + ) |
| 166 | + output_stream: Channel[Any] = Channel( |
| 167 | + MAX_MESSAGE_BUFFER_SIZE if is_streaming_output else 1 |
| 168 | + ) |
| 169 | + if ( |
| 170 | + msg.controlFlags & STREAM_CLOSED_BIT == 0 |
| 171 | + or msg.payload.get("type", None) != "CLOSE" |
| 172 | + ): |
| 173 | + try: |
| 174 | + await input_stream.put(msg.payload) |
| 175 | + except (RuntimeError, ChannelClosed) as e: |
| 176 | + raise InvalidMessageException(e) from e |
| 177 | + # Start the handler. |
| 178 | + self._task_manager.create_task( |
| 179 | + handler_func(msg.from_, input_stream, output_stream), tg |
| 180 | + ) |
| 181 | + self._task_manager.create_task( |
| 182 | + self._send_responses_from_output_stream( |
| 183 | + msg.streamId, output_stream, is_streaming_output |
| 184 | + ), |
| 185 | + tg, |
| 186 | + ) |
| 187 | + return input_stream |
0 commit comments