Skip to content

Commit da27bb2

Browse files
committed
fix(backend): use http adapter for task worker sdk client
This avoids overhead when initializing SSL context. Signed-off-by: Fatih Acar <[email protected]>
1 parent e2c06c6 commit da27bb2

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

backend/infrahub/services/adapters/http/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ async def get(
1111
self,
1212
url: str,
1313
headers: dict[str, Any] | None = None,
14+
timeout: int | None = None,
1415
) -> httpx.Response:
1516
raise NotImplementedError()
1617

@@ -21,5 +22,6 @@ async def post(
2122
json: Any | None = None,
2223
headers: dict[str, Any] | None = None,
2324
verify: bool | None = None,
25+
timeout: int | None = None,
2426
) -> httpx.Response:
2527
raise NotImplementedError()

backend/infrahub/services/adapters/http/httpx.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ async def _request(
5252
json: Any | None = None,
5353
headers: dict[str, Any] | None = None,
5454
verify: bool | None = None,
55+
timeout: int | None = None,
5556
) -> httpx.Response:
5657
"""Returns an httpx.Response object or raises HTTPServerError or child classes."""
5758
params: dict[str, Any] = {}
@@ -65,7 +66,7 @@ async def _request(
6566
method=method,
6667
url=url,
6768
headers=headers,
68-
timeout=self.settings.timeout,
69+
timeout=timeout or self.settings.timeout,
6970
**params,
7071
)
7172
except ssl.SSLCertVerificationError as exc:
@@ -87,11 +88,13 @@ async def get(
8788
self,
8889
url: str,
8990
headers: dict[str, Any] | None = None,
91+
timeout: int | None = None,
9092
) -> httpx.Response:
9193
return await self._request(
9294
method="get",
9395
url=url,
9496
headers=headers,
97+
timeout=timeout,
9598
)
9699

97100
async def post(
@@ -101,5 +104,8 @@ async def post(
101104
json: Any | None = None,
102105
headers: dict[str, Any] | None = None,
103106
verify: bool | None = None,
107+
timeout: int | None = None,
104108
) -> httpx.Response:
105-
return await self._request(method="post", url=url, data=data, json=json, headers=headers, verify=verify)
109+
return await self._request(
110+
method="post", url=url, data=data, json=json, headers=headers, verify=verify, timeout=timeout
111+
)

backend/infrahub/workers/dependencies.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from typing import Any
22

3+
import httpx
34
from fast_depends import Depends, inject
45
from infrahub_sdk.client import InfrahubClient
56
from infrahub_sdk.config import Config
7+
from infrahub_sdk.types import HTTPMethod
68

7-
from infrahub import config
9+
from infrahub import config, services
810
from infrahub.components import ComponentType
911
from infrahub.constants.environment import INSTALLATION_TYPE
1012
from infrahub.database import InfrahubDatabase, get_db
@@ -33,8 +35,27 @@ def get_component_type() -> ComponentType:
3335
raise ValueError("Component type is not set. It needs to be initialized before working with services.") from exc
3436

3537

38+
async def internal_requester(
39+
url: str,
40+
method: HTTPMethod,
41+
headers: dict[str, Any],
42+
timeout: int,
43+
payload: dict | None = None,
44+
) -> httpx.Response:
45+
if method == HTTPMethod.GET:
46+
return await get_http().get(url=url, headers=headers, timeout=timeout)
47+
if method == HTTPMethod.POST:
48+
return await get_http().post(url=url, json=payload, headers=headers, timeout=timeout)
49+
50+
raise Exception("Unsupported HTTP Method")
51+
52+
3653
def build_client() -> InfrahubClient:
37-
return InfrahubClient(config=Config(address=config.SETTINGS.main.internal_address, retry_on_failure=True))
54+
return InfrahubClient(
55+
config=Config(
56+
address=config.SETTINGS.main.internal_address, retry_on_failure=True, requester=internal_requester
57+
)
58+
)
3859

3960

4061
@inject

backend/tests/adapters/http.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ async def get(
1414
self,
1515
url: str,
1616
headers: dict[str, Any] | None = None,
17+
timeout: int | None = None,
1718
) -> httpx.Response:
1819
return self._get_response[url]
1920

@@ -24,6 +25,7 @@ async def post(
2425
json: Any | None = None,
2526
headers: dict[str, Any] | None = None,
2627
verify: bool | None = None,
28+
timeout: int | None = None,
2729
) -> httpx.Response:
2830
return self._post_response[url]
2931

0 commit comments

Comments
 (0)