Skip to content

Commit 89929dc

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 89929dc

2 files changed

Lines changed: 22 additions & 5 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 = "max@wherobots.com" }]
66
requires-python = ">=3.10, <4"

wherobots/db/driver.py

Lines changed: 21 additions & 4 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

@@ -200,17 +201,33 @@ def connect_direct(
200201
geometry_representation: Union[GeometryRepresentation, None] = None,
201202
) -> Connection:
202203
uri_with_protocol = f"{uri}/{protocol}"
204+
ssl_context = ssl.create_default_context()
205+
ssl_context.load_verify_locations(certifi.where())
203206

204-
try:
207+
@tenacity.retry(
208+
stop=tenacity.stop_after_attempt(5),
209+
wait=tenacity.wait_exponential(multiplier=1, min=1, max=5),
210+
retry=tenacity.retry_if_exception_type(
211+
(
212+
ConnectionRefusedError,
213+
ConnectionResetError,
214+
TimeoutError,
215+
websockets.exceptions.InvalidHandshake,
216+
)
217+
),
218+
reraise=True,
219+
)
220+
def ws_connect() -> websockets.sync.client.ClientConnection:
205221
logging.info("Connecting to SQL session at %s ...", uri_with_protocol)
206-
ssl_context = ssl.create_default_context()
207-
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)