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

refactor: move session renewal into own method #755

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/dsp_tools/utils/connection_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,7 @@ def _try_network_action(self, action: Callable[[], Response]) -> Response:
self._log_and_sleep(reason="Timeout Error", retry_counter=i)
continue
except (ConnectionError, RequestException):
self.session.close()
self.session = Session()
self.session.headers["Authorization"] = f"Bearer {self.token}"
self._renew_session()
self._log_and_sleep(reason="Connection Error raised", retry_counter=i)
continue

Expand All @@ -308,6 +306,11 @@ def _try_network_action(self, action: Callable[[], Response]) -> Response:
# after 7 vain attempts to create a response, try it a last time and let it escalate
return action()

def _renew_session(self) -> None:
self.session.close()
self.session = Session()
self.session.headers["Authorization"] = f"Bearer {self.token}"

def _log_and_sleep(self, reason: str, retry_counter: int) -> None:
msg = f"{reason}: Try reconnecting to DSP server, next attempt in {2 ** retry_counter} seconds..."
print(f"{datetime.now()}: {msg}")
Expand Down