Skip to content

Commit 73e9d80

Browse files
committed
fix(backend): use local cache for git repository init
Unfortunately we cannot leverage Prefect's caching because: - local caching requires a lot of setup (wrt Blocks, etc) - it's memory intensive due to excessive serialization in our case Revert to basic local TTL cache. Signed-off-by: Fatih Acar <[email protected]>
1 parent 27edb96 commit 73e9d80

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

backend/infrahub/git/repository.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
from __future__ import annotations
22

3-
from datetime import timedelta
43
from typing import TYPE_CHECKING, Any
54

5+
from cachetools import TTLCache, cached
6+
from cachetools.keys import hashkey
67
from git.exc import BadName, GitCommandError
78
from infrahub_sdk.exceptions import GraphQLError
89
from prefect import task
9-
from prefect.cache_policies import INPUTS
10-
from prefect.locking.memory import MemoryLockManager
11-
from prefect.transactions import IsolationLevel
10+
from prefect.cache_policies import NONE
1211
from pydantic import Field
1312

1413
from infrahub.core.constants import InfrahubKind, RepositoryInternalStatus
@@ -251,14 +250,13 @@ async def sync_from_remote(self, commit: str | None = None) -> None:
251250
await self.update_commit_value(branch_name=self.infrahub_branch_name, commit=commit)
252251

253252

254-
@task(
255-
name="Fetch repository commit",
256-
description="Retrieve a git repository at a given commit, if it does not already exist locally",
257-
cache_policy=INPUTS.configure(isolation_level=IsolationLevel.SERIALIZABLE, lock_manager=MemoryLockManager())
258-
- "client",
259-
cache_expiration=timedelta(seconds=30),
253+
@cached(
254+
TTLCache(maxsize=100, ttl=30),
255+
key=lambda *_, **kwargs: hashkey(
256+
kwargs.get("repository_id"), kwargs.get("name"), kwargs.get("repository_kind"), kwargs.get("commit")
257+
),
260258
)
261-
async def get_initialized_repo(
259+
async def _get_initialized_repo(
262260
client: InfrahubClient, repository_id: str, name: str, repository_kind: str, commit: str | None = None
263261
) -> InfrahubReadOnlyRepository | InfrahubRepository:
264262
if repository_kind == InfrahubKind.REPOSITORY:
@@ -268,3 +266,16 @@ async def get_initialized_repo(
268266
return await InfrahubReadOnlyRepository.init(id=repository_id, name=name, commit=commit, client=client)
269267

270268
raise NotImplementedError(f"The repository kind {repository_kind} has not been implemented")
269+
270+
271+
@task(
272+
name="Fetch repository commit",
273+
description="Retrieve a git repository at a given commit, if it does not already exist locally",
274+
cache_policy=NONE,
275+
)
276+
async def get_initialized_repo(
277+
client: InfrahubClient, repository_id: str, name: str, repository_kind: str, commit: str | None = None
278+
) -> InfrahubReadOnlyRepository | InfrahubRepository:
279+
return await _get_initialized_repo(
280+
client=client, repository_id=repository_id, name=name, repository_kind=repository_kind, commit=commit
281+
)

poetry.lock

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ dulwich = "^0.22.7"
7676
whenever = "0.7.3"
7777
netutils = "1.12.0"
7878
copier = "^9.8.0"
79+
cachetools-async = "^0.0.5"
7980

8081
[tool.poetry.group.dev.dependencies]
8182
yamllint = "*"

0 commit comments

Comments
 (0)