From 930df8f2e6cdd66b6b8e01cf42c121207b20ba76 Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum <39048939+jnussbaum@users.noreply.github.com> Date: Fri, 29 Dec 2023 09:44:35 +0100 Subject: [PATCH] fix: increase timeout to prevent doubled resources (DEV-3114) (#698) --- .../commands/xmlupload/models/sipi.py | 3 +-- src/dsp_tools/utils/connection_live.py | 21 +++++++------------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/dsp_tools/commands/xmlupload/models/sipi.py b/src/dsp_tools/commands/xmlupload/models/sipi.py index f02d95029..556a8c200 100644 --- a/src/dsp_tools/commands/xmlupload/models/sipi.py +++ b/src/dsp_tools/commands/xmlupload/models/sipi.py @@ -26,6 +26,5 @@ def upload_bitstream(self, filepath: Path) -> dict[str, Any]: """ with open(filepath, "rb") as bitstream_file: files = {"file": (filepath.name, bitstream_file)} - timeout = 5 * 60 - res = self.con.post(route="/upload", files=files, timeout=timeout) + res = self.con.post(route="/upload", files=files) return res diff --git a/src/dsp_tools/utils/connection_live.py b/src/dsp_tools/utils/connection_live.py index 4eea9abba..d90a3ed42 100644 --- a/src/dsp_tools/utils/connection_live.py +++ b/src/dsp_tools/utils/connection_live.py @@ -114,6 +114,10 @@ class ConnectionLive: dump: bool = False dump_directory = Path("HTTP requests") token: Optional[str] = None + # downtimes of server-side services -> API still processes request + # -> retry too early has side effects (e.g. duplicated resources) + timeout_put_post: int = 30 * 60 + timeout_get_delete: int = 20 def __post_init__(self) -> None: """ @@ -234,11 +238,6 @@ def post( Returns: response from server """ - # timeout must be high enough, - # otherwise the client can get a timeout error while the API is still processing the request - # in that case, the client's retry will have undesired side effects (e.g. duplicated resources), - # and the response of the original API call will be lost - timeout = timeout or 60 if not route.startswith("/"): route = f"/{route}" url = self.server + route @@ -249,7 +248,7 @@ def post( if self.token: headers["Authorization"] = f"Bearer {self.token}" - request = partial(requests.post, url=url, headers=headers, timeout=timeout) + request = partial(requests.post, url=url, headers=headers, timeout=timeout or self.timeout_put_post) if jsondata: # 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 @@ -299,7 +298,7 @@ def get( lambda: requests.get( url=url, headers=headers, - timeout=20, + timeout=self.timeout_get_delete, ) ) if self.dump: @@ -333,10 +332,6 @@ def put( Returns: response from server """ - # timeout must be high enough, - # otherwise the client can get a timeout error while the API is still processing the request - # in that case, the client's retry will fail, and the response of the original API call will be lost - timeout = 60 if not route.startswith("/"): route = f"/{route}" url = self.server + route @@ -354,7 +349,7 @@ def put( # 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 data=jsondata.encode("utf-8") if jsondata else None, - timeout=timeout, + timeout=self.timeout_put_post, ) ) if self.dump: @@ -397,7 +392,7 @@ def delete( url=url, headers=headers, params=params, - timeout=20, + timeout=self.timeout_get_delete, ) if self.dump: self._write_request_to_file(