Skip to content

Commit 8a9ad21

Browse files
authored
Include a default timeout for HTTP requests library to 70 seconds (#1188)
* Include a default timeout for HTTP requests library to 70 seconds
1 parent e2b4635 commit 8a9ad21

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

synapseclient/client.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ class Synapse(object):
248248
authEndpoint: Location of authentication service
249249
fileHandleEndpoint: Location of file service
250250
portalEndpoint: Location of the website
251-
serviceTimeoutSeconds: Wait time before timeout (currently unused)
252251
debug: Print debugging messages if True
253252
skip_checks: Skip version and endpoint checks
254253
configPath: Path to config File with setting for Synapse. Defaults to ~/.synapseConfig
@@ -281,6 +280,9 @@ class Synapse(object):
281280
raised. These will be appended to the default `User-Agent` header that
282281
already includes the version of this client that you are using, and the
283282
HTTP library used to make the request.
283+
timeout: The timeout in seconds for HTTP requests. The default is 70 seconds.
284+
You may increase this if you are experiencing timeouts when interacting
285+
with slow services.
284286
285287
Example: Getting started
286288
Logging in to Synapse using an authToken
@@ -336,6 +338,7 @@ def __init__(
336338
asyncio_event_loop: asyncio.AbstractEventLoop = None,
337339
cache_client: bool = True,
338340
user_agent: Union[str, List[str]] = None,
341+
http_timeout_seconds: int = 70,
339342
) -> "Synapse":
340343
"""
341344
Initialize Synapse object
@@ -374,6 +377,9 @@ def __init__(
374377
raised. These will be appended to the default `User-Agent` header that
375378
already includes the version of this client that you are using, and the
376379
HTTP library used to make the request.
380+
http_timeout_seconds: The timeout in seconds for HTTP requests.
381+
The default is 70 seconds. You may increase this if you are
382+
experiencing timeouts when interacting with slow services.
377383
378384
Raises:
379385
ValueError: Warn for non-boolean debug value.
@@ -391,7 +397,8 @@ def __init__(
391397
else:
392398
self._requests_session_async_synapse = {}
393399

394-
httpx_timeout = httpx.Timeout(70, pool=None)
400+
self._http_timeout_seconds = http_timeout_seconds
401+
httpx_timeout = httpx.Timeout(http_timeout_seconds, pool=None)
395402
self._requests_session_storage = requests_session_storage or httpx.Client(
396403
timeout=httpx_timeout
397404
)
@@ -505,7 +512,7 @@ async def close_connection() -> None:
505512
await self._requests_session_async_synapse[asyncio_event_loop].aclose()
506513
del self._requests_session_async_synapse[asyncio_event_loop]
507514

508-
httpx_timeout = httpx.Timeout(70, pool=None)
515+
httpx_timeout = httpx.Timeout(self._http_timeout_seconds, pool=None)
509516
self._requests_session_async_synapse.update(
510517
{
511518
asyncio_event_loop: httpx.AsyncClient(
@@ -758,6 +765,7 @@ def setEndpoints(
758765
endpoints[point],
759766
allow_redirects=False,
760767
headers=synapseclient.USER_AGENT,
768+
timeout=self._http_timeout_seconds,
761769
),
762770
verbose=self.debug,
763771
**STANDARD_RETRY_PARAMS,
@@ -6216,12 +6224,14 @@ def _rest_call(
62166224

62176225
auth = kwargs.pop("auth", self.credentials)
62186226
requests_method_fn = getattr(requests_session, method)
6227+
timeout = kwargs.pop("timeout", self._http_timeout_seconds)
62196228
response = with_retry(
62206229
lambda: requests_method_fn(
62216230
uri,
62226231
data=data,
62236232
headers=headers,
62246233
auth=auth,
6234+
timeout=timeout,
62256235
**kwargs,
62266236
),
62276237
verbose=self.debug,

tests/unit/synapseclient/unit_test_client.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2312,7 +2312,12 @@ def _rest_call_test(self, requests_session=None):
23122312
mock_build_retry_policy.assert_called_once_with(retryPolicy)
23132313
mock_handle_synapse_http_error.assert_called_once_with(response)
23142314
mock_requests_call.assert_called_once_with(
2315-
uri, data=data, headers=headers, auth=self.syn.credentials, **kwargs
2315+
uri,
2316+
data=data,
2317+
headers=headers,
2318+
auth=self.syn.credentials,
2319+
timeout=70,
2320+
**kwargs,
23162321
)
23172322

23182323
return response

0 commit comments

Comments
 (0)