Skip to content

Commit d944e6f

Browse files
committedJan 3, 2025
fix(websockets): add retry backoff to server
1 parent e1db50d commit d944e6f

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed
 

‎src/diart/websockets.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from dataclasses import dataclass
33
from pathlib import Path
44
from typing import Any, AnyStr, Callable, Dict, Optional, Text, Union
5+
import time
56

67
import numpy as np
78
from websocket_server import WebsocketServer
@@ -331,10 +332,15 @@ def close_all(self) -> None:
331332
logger.error(f"Error during shutdown: {e}")
332333

333334
def run(self) -> None:
334-
"""Start the WebSocket server."""
335+
"""Start the WebSocket server.
336+
337+
The server will attempt to restart on connection errors with exponential backoff.
338+
After max retries are exhausted, it will shut down gracefully.
339+
"""
335340
logger.info(f"Starting WebSocket server on {self.uri}")
336341
max_retries = 3
337342
retry_count = 0
343+
base_delay = 1 # in seconds
338344

339345
while retry_count < max_retries:
340346
try:
@@ -344,11 +350,17 @@ def run(self) -> None:
344350
logger.warning(f"WebSocket server connection error: {e}")
345351
retry_count += 1
346352
if retry_count < max_retries:
353+
delay = base_delay * (2 ** (retry_count - 1)) # Exponential backoff
347354
logger.info(
348-
f"Attempting to restart server (attempt {retry_count + 1}/{max_retries})"
355+
f"Retrying in {delay} seconds... "
356+
f"(attempt {retry_count}/{max_retries})"
349357
)
358+
time.sleep(delay)
350359
else:
351-
logger.error("Max retry attempts reached. Server shutting down.")
360+
logger.error(
361+
f"WebSocket server failed to start after {max_retries} attempts. "
362+
f"Last error: {e}"
363+
)
352364
except Exception as e:
353365
logger.error(f"Fatal server error: {e}")
354366
break

0 commit comments

Comments
 (0)