Skip to content

Commit 18a0bc4

Browse files
committed
Fix no retry for failing connection to server and added retry to get
1 parent dc1702a commit 18a0bc4

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

simvue/api.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
import copy
1111
import json
1212
import typing
13+
import http
1314

1415
import requests
1516
from tenacity import (
1617
retry,
18+
retry_if_exception,
1719
stop_after_attempt,
1820
wait_exponential,
19-
retry_if_exception_type,
2021
)
2122
from .utilities import parse_validation_response
2223

@@ -25,6 +26,14 @@
2526
RETRY_MIN = 4
2627
RETRY_MAX = 10
2728
RETRY_STOP = 5
29+
RETRY_STATUS_CODES = (
30+
http.HTTPStatus.BAD_REQUEST,
31+
http.HTTPStatus.SERVICE_UNAVAILABLE,
32+
http.HTTPStatus.GATEWAY_TIMEOUT,
33+
http.HTTPStatus.REQUEST_TIMEOUT,
34+
http.HTTPStatus.TOO_EARLY,
35+
)
36+
RETRY_EXCEPTION_TYPES = (RuntimeError, requests.exceptions.ConnectionError)
2837

2938

3039
def set_json_header(headers: dict[str, str]) -> dict[str, str]:
@@ -37,10 +46,18 @@ def set_json_header(headers: dict[str, str]) -> dict[str, str]:
3746
return headers
3847

3948

49+
def is_retryable_exception(exception: Exception) -> bool:
50+
"""Returns if the given exception should lead to a retry being called"""
51+
if isinstance(exception, requests.HTTPError):
52+
return exception.status_code in RETRY_STATUS_CODES
53+
54+
return isinstance(exception, RETRY_EXCEPTION_TYPES)
55+
56+
4057
@retry(
4158
wait=wait_exponential(multiplier=RETRY_MULTIPLIER, min=RETRY_MIN, max=RETRY_MAX),
4259
stop=stop_after_attempt(RETRY_STOP),
43-
retry=retry_if_exception_type(RuntimeError),
60+
retry=retry_if_exception(is_retryable_exception),
4461
reraise=True,
4562
)
4663
def post(
@@ -96,7 +113,7 @@ def post(
96113

97114
@retry(
98115
wait=wait_exponential(multiplier=RETRY_MULTIPLIER, min=RETRY_MIN, max=RETRY_MAX),
99-
retry=retry_if_exception_type(RuntimeError),
116+
retry=retry_if_exception(is_retryable_exception),
100117
stop=stop_after_attempt(RETRY_STOP),
101118
reraise=True,
102119
)
@@ -119,7 +136,7 @@ def put(
119136
data to put
120137
is_json : bool, optional
121138
send as JSON string, by default True
122-
timeout : _type_, optional
139+
timeout : int, optional
123140
timeout of request, by default DEFAULT_API_TIMEOUT
124141
125142
Returns
@@ -140,6 +157,12 @@ def put(
140157
return response
141158

142159

160+
@retry(
161+
wait=wait_exponential(multiplier=RETRY_MULTIPLIER, min=RETRY_MIN, max=RETRY_MAX),
162+
retry=retry_if_exception(is_retryable_exception),
163+
stop=stop_after_attempt(RETRY_STOP),
164+
reraise=True,
165+
)
143166
def get(
144167
url: str, headers: dict[str, str], timeout: int = DEFAULT_API_TIMEOUT
145168
) -> requests.Response:
@@ -151,7 +174,7 @@ def get(
151174
URL to put to
152175
headers : dict[str, str]
153176
headers for the post request
154-
timeout : _type_, optional
177+
timeout : int, optional
155178
timeout of request, by default DEFAULT_API_TIMEOUT
156179
157180
Returns

0 commit comments

Comments
 (0)