11import asyncio
22import logging
3- from typing import Any , Callable , Coroutine
3+ from typing import Any , Callable , Coroutine , assert_never
44
55import websockets
66from aiochannel import Channel , ChannelClosed
1212 parse_transport_msg ,
1313)
1414from replit_river .seq_manager import (
15- IgnoreMessageException ,
15+ IgnoreMessage ,
1616 InvalidMessageException ,
1717 OutOfOrderMessageException ,
1818)
@@ -122,11 +122,20 @@ async def _handle_messages_from_ws(self, tg: asyncio.TaskGroup) -> None:
122122 # We should not process messages if the websocket is closed.
123123 break
124124 msg = parse_transport_msg (message )
125+ if isinstance (msg , str ):
126+ logger .debug ("Ignoring transport message" , exc_info = True )
127+ continue
125128
126129 logger .debug (f"{ self ._transport_id } got a message %r" , msg )
127130
128131 # Update bookkeeping
129- await self ._seq_manager .check_seq_and_update (msg )
132+ match self ._seq_manager .check_seq_and_update (msg ):
133+ case IgnoreMessage ():
134+ continue
135+ case None :
136+ pass
137+ case other :
138+ assert_never (other )
130139 await self ._buffer .remove_old_messages (
131140 self ._seq_manager .receiver_ack ,
132141 )
@@ -135,13 +144,11 @@ async def _handle_messages_from_ws(self, tg: asyncio.TaskGroup) -> None:
135144 if msg .controlFlags & ACK_BIT != 0 :
136145 continue
137146 async with self ._stream_lock :
138- stream = self ._streams .get (msg .streamId , None )
147+ stream = self ._streams .get (msg .streamId )
139148 if msg .controlFlags & STREAM_OPEN_BIT == 0 :
140149 if not stream :
141150 logger .warning ("no stream for %s" , msg .streamId )
142- raise IgnoreMessageException (
143- "no stream for message, ignoring"
144- )
151+ continue
145152
146153 if (
147154 msg .controlFlags & STREAM_CLOSED_BIT != 0
@@ -160,6 +167,8 @@ async def _handle_messages_from_ws(self, tg: asyncio.TaskGroup) -> None:
160167 raise InvalidMessageException (e ) from e
161168 else :
162169 _stream = await self ._open_stream_and_call_handler (msg , tg )
170+ if isinstance (_stream , IgnoreMessage ):
171+ continue
163172 if not stream :
164173 async with self ._stream_lock :
165174 self ._streams [msg .streamId ] = _stream
@@ -170,9 +179,6 @@ async def _handle_messages_from_ws(self, tg: asyncio.TaskGroup) -> None:
170179 stream .close ()
171180 async with self ._stream_lock :
172181 del self ._streams [msg .streamId ]
173- except IgnoreMessageException :
174- logger .debug ("Ignoring transport message" , exc_info = True )
175- continue
176182 except OutOfOrderMessageException :
177183 logger .exception ("Out of order message, closing connection" )
178184 await ws_wrapper .close ()
@@ -188,17 +194,22 @@ async def _open_stream_and_call_handler(
188194 self ,
189195 msg : TransportMessage ,
190196 tg : asyncio .TaskGroup | None ,
191- ) -> Channel :
197+ ) -> Channel | IgnoreMessage :
192198 if not msg .serviceName or not msg .procedureName :
193- raise IgnoreMessageException (
194- f"Service name or procedure name is missing in the message { msg } "
199+ logger .warning (
200+ "Service name or procedure name is missing in the message %r" ,
201+ msg ,
195202 )
203+ return IgnoreMessage ()
196204 key = (msg .serviceName , msg .procedureName )
197205 handler = self ._handlers .get (key , None )
198206 if not handler :
199- raise IgnoreMessageException (
200- f"No handler for { key } handlers : { self ._handlers .keys ()} "
207+ logger .warning (
208+ "No handler for %r handlers: %r" ,
209+ key ,
210+ self ._handlers .keys (),
201211 )
212+ return IgnoreMessage ()
202213 method_type , handler_func = handler
203214 is_streaming_output = method_type in (
204215 "subscription-stream" , # subscription
0 commit comments