Skip to content

Commit c154f88

Browse files
committed
Address PR comments
1 parent a864314 commit c154f88

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

template/server/messaging.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
logger = logging.getLogger(__name__)
3333

34+
MAX_RECONNECT_RETRIES = 1
35+
PING_TIMEOUT = 30
36+
3437

3538
class Execution:
3639
def __init__(self, in_background: bool = False):
@@ -82,7 +85,7 @@ async def connect(self):
8285

8386
self._ws = await connect(
8487
self.url,
85-
ping_timeout=30,
88+
ping_timeout=PING_TIMEOUT,
8689
max_size=None,
8790
max_queue=None,
8891
logger=ws_logger,
@@ -328,9 +331,9 @@ async def execute(
328331
execution = Execution()
329332
self._executions[message_id] = execution
330333

331-
max_retries = 3
332334
# Send the code for execution
333-
for i in range(max_retries):
335+
# Initial request and retries
336+
for i in range(1 + MAX_RECONNECT_RETRIES):
334337
try:
335338
logger.info(
336339
f"Sending code for the execution ({message_id}): {complete_code}"
@@ -342,17 +345,13 @@ async def execute(
342345
break
343346
except (ConnectionClosedError, WebSocketException) as e:
344347
# Keep the last result, even if error
345-
if i < max_retries - 1:
348+
if i < MAX_RECONNECT_RETRIES - 1:
346349
logger.warning(
347350
f"WebSocket connection lost while sending execution request, {i + 1}. reconnecting...: {str(e)}"
348351
)
349352
await self.reconnect()
350-
351-
del self._executions[message_id]
352-
message_id = str(uuid.uuid4())
353-
execution = Execution()
354-
self._executions[message_id] = execution
355353
else:
354+
# The retry didn't help, request wasn't sent successfully
356355
logger.error("Failed to send execution request")
357356
await execution.queue.put(
358357
Error(
@@ -387,6 +386,7 @@ async def _receive_message(self):
387386
logger.error(f"WebSocket received error while receiving messages: {str(e)}")
388387
finally:
389388
# To prevent infinite hang, we need to cancel all ongoing execution as we could lost results during the reconnect
389+
# Thanks to the locking, there can be either no ongoing execution or just one.
390390
for key, execution in self._executions.items():
391391
await execution.queue.put(
392392
Error(

0 commit comments

Comments
 (0)