From e4d3ea1a34a5ae2e778a25bf3821834168c7a74e Mon Sep 17 00:00:00 2001 From: soamicharan Date: Tue, 15 Oct 2024 00:02:11 +0530 Subject: [PATCH 1/3] fix rest api aiohttp timeout --- kubernetes_asyncio/client/rest.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/kubernetes_asyncio/client/rest.py b/kubernetes_asyncio/client/rest.py index 5246c2d7..88cb096f 100644 --- a/kubernetes_asyncio/client/rest.py +++ b/kubernetes_asyncio/client/rest.py @@ -117,7 +117,16 @@ async def request(self, method, url, query_params=None, headers=None, post_params = post_params or {} headers = headers or {} - timeout = _request_timeout or 5 * 60 + timeout = aiohttp.ClientTimeout() + if _request_timeout: + if isinstance(_request_timeout, (int, float)): + timeout = aiohttp.ClientTimeout(total=_request_timeout) + elif isinstance(_request_timeout, tuple) and len(_request_timeout) == 2: + timeout = aiohttp.ClientTimeout( + connect=_request_timeout[0], + sock_connect=_request_timeout[0], + sock_read=_request_timeout[1], + ) if 'Content-Type' not in headers: headers['Content-Type'] = 'application/json' From 931a2655d033c2b1161b36570f9c6c835e21bb40 Mon Sep 17 00:00:00 2001 From: soamicharan Date: Tue, 15 Oct 2024 23:14:17 +0530 Subject: [PATCH 2/3] Added test cases for rest api timeout parameter --- kubernetes_asyncio/client/test_rest.py | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 kubernetes_asyncio/client/test_rest.py diff --git a/kubernetes_asyncio/client/test_rest.py b/kubernetes_asyncio/client/test_rest.py new file mode 100644 index 00000000..330fe92a --- /dev/null +++ b/kubernetes_asyncio/client/test_rest.py @@ -0,0 +1,31 @@ +import asyncio +import unittest +from unittest.mock import AsyncMock +import aiohttp +from kubernetes_asyncio.client.rest import RESTClientObject +from kubernetes_asyncio.client.configuration import Configuration +class TestRESTClientObject(unittest.IsolatedAsyncioTestCase): + + @classmethod + def setUpClass(cls): + cls.config = Configuration() + + + async def test_rest_request_timeout(self): + rest_api = RESTClientObject(configuration=self.config) + for request_timeout, expected_timeout_arg in [ + (None, aiohttp.ClientTimeout()), + (5.0, aiohttp.ClientTimeout(total=5.0)), + (3, aiohttp.ClientTimeout(total=3)), + ((5, 7), aiohttp.ClientTimeout(connect=5, sock_connect=5, sock_read=7)), + ]: + with self.subTest(request_timeout=request_timeout, expected_timeout_arg=expected_timeout_arg): + mock_request = AsyncMock() + rest_api.pool_manager.request = mock_request + await rest_api.request(method="GET", url="http://test-api", _preload_content=False, _request_timeout=request_timeout) + mock_request.assert_called_once_with( + method="GET", + url="http://test-api", + timeout=expected_timeout_arg, + headers={"Content-Type": "application/json"} + ) From 9adb4b59f3dd70bf5a3f89ef67b97cfb3680f709 Mon Sep 17 00:00:00 2001 From: soamicharan Date: Tue, 29 Oct 2024 02:14:43 +0530 Subject: [PATCH 3/3] handled scenario when request_timeout is ClientTimeout object --- kubernetes_asyncio/client/rest.py | 5 ++++- kubernetes_asyncio/client/test_rest.py | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/kubernetes_asyncio/client/rest.py b/kubernetes_asyncio/client/rest.py index 88cb096f..c5b75c6e 100644 --- a/kubernetes_asyncio/client/rest.py +++ b/kubernetes_asyncio/client/rest.py @@ -104,7 +104,8 @@ async def request(self, method, url, query_params=None, headers=None, :param _request_timeout: timeout setting for this request. If one number provided, it will be total request timeout. It can also be a pair (tuple) of - (connection, read) timeouts. + (connection, read) timeouts or object + of aiohttp.ClientTimeout. """ method = method.upper() assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', @@ -127,6 +128,8 @@ async def request(self, method, url, query_params=None, headers=None, sock_connect=_request_timeout[0], sock_read=_request_timeout[1], ) + elif isinstance(_request_timeout, aiohttp.ClientTimeout): + timeout = _request_timeout if 'Content-Type' not in headers: headers['Content-Type'] = 'application/json' diff --git a/kubernetes_asyncio/client/test_rest.py b/kubernetes_asyncio/client/test_rest.py index 330fe92a..f379637f 100644 --- a/kubernetes_asyncio/client/test_rest.py +++ b/kubernetes_asyncio/client/test_rest.py @@ -18,6 +18,7 @@ async def test_rest_request_timeout(self): (5.0, aiohttp.ClientTimeout(total=5.0)), (3, aiohttp.ClientTimeout(total=3)), ((5, 7), aiohttp.ClientTimeout(connect=5, sock_connect=5, sock_read=7)), + (aiohttp.ClientTimeout(total=None), aiohttp.ClientTimeout(total=None)), ]: with self.subTest(request_timeout=request_timeout, expected_timeout_arg=expected_timeout_arg): mock_request = AsyncMock()