|
13 | 13 | import tenacity |
14 | 14 | from typing import Final, Union, Dict |
15 | 15 | import urllib.parse |
| 16 | +import websockets.exceptions |
16 | 17 | import websockets.sync.client |
17 | 18 | import certifi |
18 | 19 |
|
@@ -200,17 +201,33 @@ def connect_direct( |
200 | 201 | geometry_representation: Union[GeometryRepresentation, None] = None, |
201 | 202 | ) -> Connection: |
202 | 203 | uri_with_protocol = f"{uri}/{protocol}" |
| 204 | + ssl_context = ssl.create_default_context() |
| 205 | + ssl_context.load_verify_locations(certifi.where()) |
203 | 206 |
|
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: |
205 | 221 | 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( |
209 | 223 | uri=uri_with_protocol, |
210 | 224 | additional_headers=headers, |
211 | 225 | max_size=MAX_MESSAGE_SIZE, |
212 | 226 | ssl=ssl_context, |
213 | 227 | ) |
| 228 | + |
| 229 | + try: |
| 230 | + ws = ws_connect() |
214 | 231 | except Exception as e: |
215 | 232 | raise InterfaceError("Failed to connect to SQL session!") from e |
216 | 233 |
|
|
0 commit comments