Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server errors fix #373

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions balsam/client/requests_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ def close_session(self) -> None:
self._session = None
return None

def backoff(self, reason: Exception) -> None:
if self._attempt > self.retry_count:
raise TimeoutError(f"Exceeded max retries: {reason}")
sleep_time = 2**self._attempt + random.random()
def backoff(self) -> None:
if self._attempt < self.retry_count:
sleep_time = 2**self._attempt + random.random()
else:
sleep_time = 2**self.retry_count + random.random()
time.sleep(sleep_time)
self._attempt += 1

Expand All @@ -96,11 +97,21 @@ def request(
logger.debug(f"{http_method}: {absolute_url} {params if params else ''}")
response = self._do_request(absolute_url, http_method, params, json, data)
except requests.Timeout as exc:
logger.warning(f"Attempt Retry of Timed-out request {http_method} {absolute_url}")
self.backoff(exc)
if self._attempt < self.retry_count:
logger.warning(f"Attempt Retry of Timed-out request {http_method} {absolute_url}")
else:
logger.warning(
f"Max backoff retries exceed for timed-out request; attempt retry every {2**self.retry_count}s: {exc}"
)
self.backoff()
except (requests.ConnectionError, requests.HTTPError) as exc:
logger.warning(f"Attempt retry ({self._attempt} of {self.retry_count}) of connection: {exc}")
self.backoff(exc)
if self._attempt < self.retry_count:
logger.warning(f"Attempt retry ({self._attempt} of {self.retry_count}) of connection: {exc}")
else:
logger.warning(
f"Max backoff retries exceed for failed request; attempt retry every {2**self.retry_count}s: {exc}"
)
self.backoff()
else:
try:
return response.json() # type: ignore
Expand Down