]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Ensure async https() requests are bounded in total time
authorBob Halley <halley@dnspython.org>
Tue, 8 Aug 2023 01:16:42 +0000 (18:16 -0700)
committerBob Halley <halley@dnspython.org>
Tue, 8 Aug 2023 01:16:42 +0000 (18:16 -0700)
according to the timeout [#978].

Unfortunately we do not currently have a good way to
make this guarantee for sync https() calls.

dns/_asyncbackend.py
dns/_asyncio_backend.py
dns/_trio_backend.py
dns/asyncquery.py

index cebcbdfd46c178b7a0c0766b442b7fed346ec85d..49f14fed682f6088fc506ce19978fbe62da1fafe 100644 (file)
@@ -94,3 +94,6 @@ class Backend:  # pragma: no cover
 
     def get_transport_class(self):
         raise NotImplementedError
+
+    async def wait_for(self, awaitable, timeout):
+        raise NotImplementedError
index 0021f84fe7847e7772a7f5d49748b9a878e78349..2631228ecdc95684f1d30980780f3300bf81de9b 100644 (file)
@@ -270,3 +270,6 @@ class Backend(dns._asyncbackend.Backend):
 
     def get_transport_class(self):
         return _HTTPTransport
+
+    async def wait_for(self, awaitable, timeout):
+        return await _maybe_wait_for(awaitable, timeout)
index d414f0b37ab8c48292adb3037795cabc9463fb76..4d9fb820445a6b46ba3cdb23e0311d70c6fdc026 100644 (file)
@@ -237,3 +237,10 @@ class Backend(dns._asyncbackend.Backend):
 
     def get_transport_class(self):
         return _HTTPTransport
+
+    async def wait_for(self, awaitable, timeout):
+        with _maybe_timeout(timeout):
+            return await awaitable
+        raise dns.exception.Timeout(
+            timeout=timeout
+        )  # pragma: no cover  lgtm[py/unreachable-statement]
index 737e1c922e118a5f2b6bb53b6a7a0358ef8e66d5..ecf9c1a5faace190cc7bbc900ef685aa06c9db24 100644 (file)
@@ -563,14 +563,14 @@ async def https(
                     "content-length": str(len(wire)),
                 }
             )
-            response = await the_client.post(
-                url, headers=headers, content=wire, timeout=timeout
+            response = await backend.wait_for(
+                the_client.post(url, headers=headers, content=wire), timeout
             )
         else:
             wire = base64.urlsafe_b64encode(wire).rstrip(b"=")
             twire = wire.decode()  # httpx does a repr() if we give it bytes
-            response = await the_client.get(
-                url, headers=headers, timeout=timeout, params={"dns": twire}
+            response = await backend.wait_for(
+                the_client.get(url, headers=headers, params={"dns": twire}), timeout
             )
 
     # see https://tools.ietf.org/html/rfc8484#section-4.2.1 for info about DoH