Skip to content

feat(fetcher): add configurable timeout for httpx fetcher #782

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion documentation/docs/getting-started/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ Please use this table as a reference.
| AUTH_PUBLIC_KEY | | |
| AUTH_JWT_ALGORITHM | JWT algorithm. See possible values [here](https://pyjwt.readthedocs.io/en/stable/algorithms.html). | |
| AUTH_JWT_AUDIENCE | | |
| AUTH_JWT_ISSUER | | |
| AUTH_JWT_ISSUER | | |
| HTTP_FETCHER_PROVIDER_CLIENT | The client to use for fetching data, can be either aiohttp or httpx. | |
| HTTP_FETCHER_TIMEOUT | The timeout for the httpx fetcher provider, in seconds. | |

## OPAL Server Configuration Variables

Expand Down
6 changes: 6 additions & 0 deletions packages/opal-common/opal_common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ class OpalCommonConfig(Confi):
description="The client to use for fetching data, can be either aiohttp or httpx."
"if provided different value, aiohttp will be used.",
)
HTTP_FETCHER_TIMEOUT = confi.float(
"HTTP_FETCHER_TIMEOUT",
5,
description="The timeout for the httpx or aiohttp fetcher provider, in seconds. "
"if provided different value, 5 seconds will be used.",
)


opal_common_config = OpalCommonConfig(prefix="OPAL_")
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Union, cast

import httpx
from aiohttp import ClientResponse, ClientSession
from aiohttp import ClientResponse, ClientSession, ClientTimeout
from opal_common.config import opal_common_config
from opal_common.fetcher.events import FetcherConfig, FetchEvent
from opal_common.fetcher.fetch_provider import BaseFetchProvider
Expand Down Expand Up @@ -70,12 +70,17 @@ def parse_event(self, event: FetchEvent) -> HttpFetchEvent:

async def __aenter__(self):
headers = {}
timeout = opal_common_config.HTTP_FETCHER_TIMEOUT
if self._event.config.headers is not None:
headers = self._event.config.headers
if opal_common_config.HTTP_FETCHER_PROVIDER_CLIENT == "httpx":
self._session = httpx.AsyncClient(headers=headers)
self._session = httpx.AsyncClient(headers=headers, timeout=timeout)
else:
self._session = ClientSession(headers=headers, raise_for_status=True)
self._session = ClientSession(
headers=headers,
raise_for_status=True,
timeout=ClientTimeout(total=timeout),
)
self._session = await self._session.__aenter__()
return self

Expand Down
Loading