Skip to content

Commit 6f78964

Browse files
Switch to raw bytes recv() call to avoid round-tripping through str
1 parent 72a7e12 commit 6f78964

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/replit_river/v2/client_session.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from aiochannel.errors import ChannelClosed
1010
from opentelemetry.trace import Span
1111
from websockets.asyncio.client import ClientConnection
12-
from websockets.exceptions import ConnectionClosed
12+
from websockets.exceptions import ConnectionClosed, ConnectionClosedOK
1313
from websockets.frames import CloseCode
1414
from websockets.legacy.protocol import WebSocketCommonProtocol
1515

@@ -156,7 +156,11 @@ async def _handle_messages_from_ws(self) -> None:
156156
)
157157
try:
158158
ws = self._ws_unwrapped
159-
async for message in ws:
159+
while True:
160+
# decode=False: Avoiding an unnecessary round-trip through str
161+
# Ideally this should be type-ascripted to : bytes, but there is no
162+
# @overrides in `websockets` to hint this.
163+
message = await ws.recv(decode=False)
160164
try:
161165
if not self._ws_unwrapped:
162166
# We should not process messages if the websocket is closed.
@@ -241,6 +245,8 @@ async def _handle_messages_from_ws(self) -> None:
241245
logger.exception("Got invalid transport message, closing session")
242246
await self.close()
243247
return
248+
except ConnectionClosedOK:
249+
pass # Exited normally
244250
except ConnectionClosed as e:
245251
raise e
246252

0 commit comments

Comments
 (0)