Skip to content

Commit 06da367

Browse files
committed
rest: apply sane default timeout on requests
datadog API has a default timeout of 60s We observe regular error from lambda client using this lib where the connection is open without any timeout leading to write errors and connection interrupted by our servers. this patch aims to set a sane default for requests. we also bump aiosonic version to benefit from better connection handling in 0.24.0: https://github.com/sonic182/aiosonic/blob/54f8b9f3e13bac87071826d942da7496ecc2a690/CHANGELOG.md?plain=1#L9 ``` - Multiple pools configurations in client connector per target host. - conn idle timeout closing for more robust conns. ``` Signed-off-by: William Dauchy <[email protected]>
1 parent 082dc9f commit 06da367

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ setup_requires =
5151
apm =
5252
ddtrace>=1.15.0
5353
async =
54-
aiosonic==0.15.1
54+
aiosonic==0.24.0
5555
zstandard =
5656
zstandard
5757
tests =
58-
aiosonic==0.15.1
58+
aiosonic==0.24.0
5959
glom
6060
jinja2
6161
pytest<8.0.0

src/datadog_api_client/rest.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
RETRY_AFTER_STATUS_CODES = frozenset([429, 500, 501, 502, 503, 504, 505, 506, 507, 509, 510, 511, 512])
2727
RETRY_ALLOWED_METHODS = frozenset(["GET", "PUT", "DELETE", "POST", "PATCH"])
28+
# apply sane default for calls to datadog API which has 60s timeout
29+
DEFAULT_TIMEOUT = 55
2830

2931

3032
class ClientRetry(urllib3.util.Retry):
@@ -140,7 +142,7 @@ def request(
140142
post_params = post_params or {}
141143
headers = headers or {}
142144

143-
timeout = None
145+
timeout = urllib3.Timeout(total=DEFAULT_TIMEOUT)
144146
if request_timeout:
145147
if isinstance(request_timeout, (int, float)):
146148
timeout = urllib3.Timeout(total=request_timeout)
@@ -290,13 +292,14 @@ async def request(
290292
(connection, read) timeouts.
291293
"""
292294
assert not post_params, "not supported for now"
295+
timeout = Timeouts(request_timeout=DEFAULT_TIMEOUT)
293296
if request_timeout is not None:
294297
from aiosonic.timeout import Timeouts # type: ignore
295298

296299
if isinstance(request_timeout, (int, float)):
297-
request_timeout = Timeouts(request_timeout=request_timeout)
300+
timeout = Timeouts(request_timeout=request_timeout)
298301
else:
299-
request_timeout = Timeouts(sock_connect=request_timeout[0], sock_read=request_timeout[1])
302+
timeout = Timeouts(sock_connect=request_timeout[0], sock_read=request_timeout[1])
300303
request_body = None
301304
if (
302305
"Content-Type" not in headers
@@ -317,7 +320,7 @@ async def request(
317320
counter = 0
318321
while True:
319322
response = await self._client.request(
320-
url, method, headers, query_params, request_body, timeouts=request_timeout
323+
url, method, headers, query_params, request_body, timeouts=timeout
321324
)
322325
retry = self._retry(method, response, counter)
323326
if not retry:

0 commit comments

Comments
 (0)