Skip to content

Commit 8b927fb

Browse files
Set custom http timeouts (#155)
1 parent 67b59c7 commit 8b927fb

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/entitysdk/config.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,34 @@ class Settings(BaseSettings):
3333
),
3434
] = "https://www.openbraininstitute.org/api/entitycore"
3535

36+
connect_timeout: Annotated[
37+
float,
38+
Field(
39+
alias="ENTITYSDK_CONNECT_TIMEOUT",
40+
description="Maximum time to wait until a connection is established, in seconds.",
41+
),
42+
] = 5
43+
read_timeout: Annotated[
44+
float,
45+
Field(
46+
alias="ENTITYSDK_READ_TIMEOUT",
47+
description="Maximum time to wait for a chunk of data to be received, in seconds.",
48+
),
49+
] = 30
50+
write_timeout: Annotated[
51+
float,
52+
Field(
53+
alias="ENTITYSDK_WRITE_TIMEOUT",
54+
description="Maximum time to wait for a chunk of data to be sent, in seconds.",
55+
),
56+
] = 30
57+
pool_timeout: Annotated[
58+
float,
59+
Field(
60+
alias="ENTITYSDK_POOL_TIMEOUT",
61+
description="Maximum time to acquire a connection from the pool, in seconds.",
62+
),
63+
] = 5
64+
3665

3766
settings = Settings()

src/entitysdk/core.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from entitysdk import serdes
1313
from entitysdk.common import ProjectContext
14+
from entitysdk.config import settings
1415
from entitysdk.exception import EntitySDKError
1516
from entitysdk.models.asset import (
1617
Asset,
@@ -216,7 +217,7 @@ def delete_entity(
216217
entity_type: type[Identifiable],
217218
token: str,
218219
http_client: httpx.Client | None = None,
219-
):
220+
) -> None:
220221
"""Delete entity."""
221222
make_db_api_request(
222223
url=url,
@@ -310,16 +311,22 @@ def upload_asset_directory(
310311
js = response.json()
311312

312313
def upload(to_upload):
314+
upload_client = http_client or httpx.Client()
313315
failed = {}
314316
for path, url in to_upload.items():
315317
with open(paths[Path(path)], "rb") as fd:
316318
try:
317-
response = http_client.request(
319+
response = upload_client.request(
318320
method="PUT",
319321
url=url,
320322
content=fd,
321323
follow_redirects=True,
322-
timeout=20,
324+
timeout=httpx.Timeout(
325+
connect=settings.connect_timeout,
326+
read=settings.read_timeout,
327+
write=settings.write_timeout,
328+
pool=settings.pool_timeout,
329+
),
323330
)
324331
except httpx.HTTPError:
325332
L.exception("Upload failed, will retry again")

src/entitysdk/util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ def make_db_api_request(
4646
data=data,
4747
params=parameters,
4848
follow_redirects=True,
49+
timeout=httpx.Timeout(
50+
connect=settings.connect_timeout,
51+
read=settings.read_timeout,
52+
write=settings.write_timeout,
53+
pool=settings.pool_timeout,
54+
),
4955
)
5056
except httpx.RequestError as e:
5157
raise EntitySDKError(f"Request error: {e}") from e

0 commit comments

Comments
 (0)