Skip to content

Commit 360f19d

Browse files
committed
feat: loop on a recv() with timeout, rather than blocking
1 parent 558982c commit 360f19d

3 files changed

Lines changed: 7 additions & 2 deletions

File tree

wherobots/db/connection.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import websockets.sync.client
1313

1414
from wherobots.db.constants import (
15+
DEFAULT_READ_TIMEOUT_SECONDS,
1516
RequestKind,
1617
EventKind,
1718
ExecutionState,
@@ -80,6 +81,9 @@ def __main_loop(self):
8081
while self.__ws.protocol.state < websockets.protocol.State.CLOSING:
8182
try:
8283
self.__listen()
84+
except TimeoutError:
85+
# Expected, retry next time
86+
continue
8387
except websockets.exceptions.ConnectionClosedOK:
8488
logging.info("Connection closed; stopping main loop.")
8589
return
@@ -164,7 +168,7 @@ def __send(self, message: dict[str, Any]) -> None:
164168
self.__ws.send(json.dumps(message))
165169

166170
def __recv(self) -> dict[str, Any]:
167-
frame = self.__ws.recv()
171+
frame = self.__ws.recv(timeout=DEFAULT_READ_TIMEOUT_SECONDS)
168172
if isinstance(frame, str):
169173
message = json.loads(frame)
170174
elif isinstance(frame, bytes):

wherobots/db/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
STAGING_ENDPOINT = "api.staging.wherobots.services" # "api.staging.wherobots.com"
99
DEFAULT_RUNTIME = Runtime.SEDONA
1010
DEFAULT_REGION = Region.AWS_US_WEST_2
11+
DEFAULT_READ_TIMEOUT_SECONDS = 0.5
1112
DEFAULT_SESSION_WAIT_TIMEOUT_SECONDS = 300
1213
MAX_MESSAGE_SIZE = 100 * 2**20 # 100MiB
1314

wherobots/db/driver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,4 @@ def connect_direct(uri: str, headers: dict[str, str] = None) -> Connection:
126126
session = Connection(ws)
127127
return session
128128
except Exception as e:
129-
raise InterfaceError("Failed to connect to SQL session!", e)
129+
raise InterfaceError("Failed to connect to SQL session!") from e

0 commit comments

Comments
 (0)