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

[httpx] use timeouts and remove footgun #11770

Merged
Changes from all 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
57 changes: 30 additions & 27 deletions hail/python/hailtop/httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,34 @@ async def __aexit__(


class ClientSession:
def __init__(self, *args, **kwargs):
self.raise_for_status = kwargs.pop('raise_for_status', False)
self.client_session = aiohttp.ClientSession(*args, raise_for_status=False, **kwargs)
def __init__(self,
*args,
raise_for_status: bool = True,
timeout: Union[aiohttp.ClientTimeout, float] = None,
**kwargs):
location = get_deploy_config().location()
if location == 'external':
tls = external_client_ssl_context()
elif location == 'k8s':
tls = internal_client_ssl_context()
else:
assert location in ('gce', 'azure')
# no encryption on the internal gateway
tls = external_client_ssl_context()

assert 'connector' not in kwargs

if timeout is None:
timeout = aiohttp.ClientTimeout(total=5)

self.raise_for_status = raise_for_status
self.client_session = aiohttp.ClientSession(
*args,
timeout=timeout,
raise_for_status=False,
connector=aiohttp.TCPConnector(ssl=tls),
**kwargs
)

def request(
self, method: str, url: aiohttp.client.StrOrURL, **kwargs: Any
Expand Down Expand Up @@ -126,7 +151,7 @@ async def request_and_raise_for_status():

def ws_connect(
self, *args, **kwargs
) -> aiohttp.client_ws.ClientWebSocketResponse:
) -> aiohttp.client._WSRequestContextManager:
return self.client_session.ws_connect(*args, **kwargs)

def get(
Expand Down Expand Up @@ -191,29 +216,7 @@ async def __aexit__(
await self.client_session.__aexit__(exc_type, exc_val, exc_tb)


def client_session(*args,
raise_for_status: bool = True,
timeout: Union[aiohttp.ClientTimeout, float] = None,
**kwargs) -> ClientSession:
location = get_deploy_config().location()
if location == 'external':
tls = external_client_ssl_context()
elif location == 'k8s':
tls = internal_client_ssl_context()
else:
assert location in ('gce', 'azure')
# no encryption on the internal gateway
tls = external_client_ssl_context()

assert 'connector' not in kwargs
kwargs['connector'] = aiohttp.TCPConnector(ssl=tls)

kwargs['raise_for_status'] = raise_for_status

if timeout is None:
timeout = aiohttp.ClientTimeout(total=5)
kwargs['timeout'] = timeout

def client_session(*args, **kwargs) -> ClientSession:
return ClientSession(*args, **kwargs)


Expand Down