Skip to content

Commit 58c0e1b

Browse files
SNOW-2136842: Retry requests on connection errors
Before this change, requests that failed due to connection errors raised by the requests module, like NewConnectionError, weren't retried
2 parents 3fe4db6 + 9270d44 commit 58c0e1b

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

snowflake/ingest/utils/network.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,30 @@ def sleep_time(self):
7171
retry_context = RetryCtx(DEFAULT_REQUEST_TIMEOUT)
7272

7373
while True:
74-
response = self._exec_request(url=url, method=method, headers=headers, json=json)
74+
try:
75+
response = self._exec_request(url=url, method=method, headers=headers, json=json)
7576

76-
if response.ok:
77-
return response.json()
78-
elif self._can_retry(response.status_code):
77+
if response.ok:
78+
return response.json()
79+
elif self._can_retry(response.status_code):
80+
next_sleep_time = retry_context.sleep_time()
81+
if next_sleep_time > 0:
82+
time.sleep(next_sleep_time)
83+
continue
84+
85+
raise IngestResponseError(response)
86+
87+
except requests.exceptions.RequestException as e:
88+
logger.error(f"Request exception occurred: {e}")
89+
# Handle connection-level errors
7990
next_sleep_time = retry_context.sleep_time()
8091
if next_sleep_time > 0:
92+
logger.debug(f"Connection error, sleeping for {next_sleep_time} seconds before retry")
8193
time.sleep(next_sleep_time)
8294
continue
83-
84-
raise IngestResponseError(response)
95+
else:
96+
logger.error("Maximum retry timeout reached, giving up")
97+
raise e
8598

8699
def _exec_request(self, url: Text, method: Text, headers: Dict = None, json: Dict = None) -> Response:
87100
return requests.request(method=method,

0 commit comments

Comments
 (0)