Skip to content

Commit

Permalink
fix dsp_client (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum authored Mar 26, 2024
1 parent 933a5f7 commit 1ecb1ea
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions dsp_permissions_scripts/utils/dsp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ class RequestParameters:
headers: dict[str, str] | None = None

def __post_init__(self) -> None:
self.data_serialized = self._serialize_payload(self.data)

def _serialize_payload(self, payload: dict[str, Any] | None) -> bytes | None:
# If data is not encoded as bytes, issues can occur with non-ASCII characters,
# where the content-length of the request will turn out to be different from the actual length.
return json.dumps(payload, ensure_ascii=False).encode("utf-8") if payload else None
self.data_serialized = json.dumps(self.data, ensure_ascii=False).encode("utf-8") if self.data else None

def as_kwargs(self) -> dict[str, Any]:
return {
Expand Down Expand Up @@ -228,10 +225,11 @@ def _try_network_action(self, params: RequestParameters) -> Response:
self._log_request(params)
response = action()
except (TimeoutError, ReadTimeout):
self._log_and_sleep(reason="Timeout Error raised", retry_counter=i, exc_info=True)
self._log_and_sleep(reason="TimeoutError/ReadTimeout raised", retry_counter=i, exc_info=True)
continue
except (ConnectionError, RequestException):
self._renew_session()
self._log_and_sleep(reason="Connection Error raised", retry_counter=i, exc_info=True)
self._log_and_sleep(reason="ConnectionError/RequestException raised", retry_counter=i, exc_info=True)
continue

self._log_response(response)
Expand Down Expand Up @@ -267,7 +265,6 @@ def _renew_session(self) -> None:

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

Expand Down

0 comments on commit 1ecb1ea

Please sign in to comment.