Skip to content

Commit

Permalink
fix: increase timeout to prevent doubled resources (DEV-3114) (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Dec 29, 2023
1 parent 228c79f commit 930df8f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
3 changes: 1 addition & 2 deletions src/dsp_tools/commands/xmlupload/models/sipi.py
Expand Up @@ -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
21 changes: 8 additions & 13 deletions src/dsp_tools/utils/connection_live.py
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -299,7 +298,7 @@ def get(
lambda: requests.get(
url=url,
headers=headers,
timeout=20,
timeout=self.timeout_get_delete,
)
)
if self.dump:
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 930df8f

Please sign in to comment.