Skip to content

Commit 0bc3d49

Browse files
committed
fix: add retry logic for WebSocket connection to SQL session
Adds tenacity-based retry with exponential backoff to the initial WebSocket connection in connect_direct(). This fixes intermittent failures in the SQL session canary tests caused by transient connection errors during the WebSocket handshake. Bump version to 0.26.1.
1 parent ee710ef commit 0bc3d49

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "wherobots-python-dbapi"
3-
version = "0.26.0"
3+
version = "0.26.1"
44
description = "Python DB-API driver for Wherobots DB"
55
authors = [{ name = "Maxime Petazzoni", email = "[email protected]" }]
66
requires-python = ">=3.10, <4"

wherobots/db/driver.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import tenacity
1414
from typing import Final, Union, Dict
1515
import urllib.parse
16+
import websockets.exceptions
1617
import websockets.sync.client
1718
import certifi
1819

@@ -201,16 +202,32 @@ def connect_direct(
201202
) -> Connection:
202203
uri_with_protocol = f"{uri}/{protocol}"
203204

204-
try:
205+
@tenacity.retry(
206+
stop=tenacity.stop_after_attempt(5),
207+
wait=tenacity.wait_exponential(multiplier=1, min=1, max=5),
208+
retry=tenacity.retry_if_exception_type(
209+
(
210+
ConnectionRefusedError,
211+
TimeoutError,
212+
OSError,
213+
websockets.exceptions.InvalidHandshake,
214+
)
215+
),
216+
reraise=True,
217+
)
218+
def ws_connect() -> websockets.sync.client.ClientConnection:
205219
logging.info("Connecting to SQL session at %s ...", uri_with_protocol)
206220
ssl_context = ssl.create_default_context()
207221
ssl_context.load_verify_locations(certifi.where())
208-
ws = websockets.sync.client.connect(
222+
return websockets.sync.client.connect(
209223
uri=uri_with_protocol,
210224
additional_headers=headers,
211225
max_size=MAX_MESSAGE_SIZE,
212226
ssl=ssl_context,
213227
)
228+
229+
try:
230+
ws = ws_connect()
214231
except Exception as e:
215232
raise InterfaceError("Failed to connect to SQL session!") from e
216233

0 commit comments

Comments
 (0)