Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rest api aiohttp timeout #337

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion kubernetes_asyncio/client/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
soamicharan marked this conversation as resolved.
Show resolved Hide resolved
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'
Expand Down
31 changes: 31 additions & 0 deletions kubernetes_asyncio/client/test_rest.py
Original file line number Diff line number Diff line change
@@ -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"}
)