Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions rustplus/remote/websocket/ws.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import shlex
import base64
import betterproto
from websockets.exceptions import InvalidURI, InvalidHandshake, ConnectionClosedError
from websockets.exceptions import InvalidURI, InvalidHandshake, ConnectionClosedError, ConnectionClosedOK
from websockets.legacy.client import WebSocketClientProtocol
from websockets.client import connect
from asyncio import TimeoutError, Task, AbstractEventLoop
Expand Down Expand Up @@ -64,7 +64,8 @@ async def connect(self) -> bool:
self.connection = await connect(
address,
close_timeout=0,
ping_interval=None,
ping_interval=20,
ping_timeout=10,
max_size=1_000_000_000,
)
except (InvalidURI, OSError, InvalidHandshake, TimeoutError) as err:
Expand Down Expand Up @@ -97,9 +98,13 @@ async def disconnect(self) -> None:
self.connection = None

async def run(self) -> None:
RECV_TIMEOUT = 30
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a file-level static variable


while self.open:
try:
data = await self.connection.recv()
data = await asyncio.wait_for(
self.connection.recv(), timeout=RECV_TIMEOUT
)

await self.run_coroutine_non_blocking(
self.run_proto_event(data, self.server_details)
Expand All @@ -111,15 +116,24 @@ async def run(self) -> None:
app_message = AppMessage()
app_message.parse(data)

except ConnectionClosedError as e:
except asyncio.TimeoutError:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are only waiting 30 seconds for a packet, but you just are not sending anything at that current moment, that is going to cause you to cycle through connections super fast despite not actually needing to, surely?

self.logger.warning(
"WebSocket recv timeout (%s), closing connection",
RECV_TIMEOUT,
)
break

except ConnectionClosedOK:
if self.debug:
self.logger.exception("Connection Interrupted: %s", e)
else:
self.logger.warning("Connection Interrupted: %s", e)
self.logger.info("WebSocket connection closed normally")
break

except ConnectionClosedError as e:
self.logger.warning("Connection Interrupted: %s", e)
break

except Exception as e:
self.logger.exception(
self.logger.error(
"An Error occurred whilst parsing the message from the server: %s",
e,
)
Expand All @@ -128,7 +142,7 @@ async def run(self) -> None:
try:
await self.run_coroutine_non_blocking(self.handle_message(app_message))
except Exception as e:
self.logger.exception(
self.logger.error(
"An Error occurred whilst handling the message from the server %s",
e,
)
Expand Down
Loading