Skip to content

Commit

Permalink
chore: configurable cache TTL for service info data
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed May 28, 2024
1 parent 71799f9 commit 501be18
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion bento_service_registry/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Config(BentoBaseConfig):
service_name: str = "Bento Service Registry"

bento_services: Path
contact_timeout: int = 5
contact_timeout: int = 5 # service-info contact timeout for other services
cache_ttl: int = 30 # service-info cache TTL for other services

bento_url: str
bento_public_url: str
Expand Down
12 changes: 6 additions & 6 deletions bento_service_registry/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .authz_header import OptionalAuthzHeader, OptionalAuthzHeaderDependency
from .bento_services_json import BentoServicesByKind, BentoServicesByKindDependency
from .config import Config, ConfigDependency
from .constants import BENTO_SERVICE_KIND
from .http_session import HTTPSessionDependency
from .logger import LoggerDependency
Expand All @@ -28,11 +29,9 @@
]


CACHE_TTL = 30


class ServiceManager:
def __init__(self, logger: logging.Logger):
def __init__(self, config: Config, logger: logging.Logger):
self._config = config
self._co: Awaitable[list[dict | None]] | None = None
self._logger = logger
self._cache: dict[str, tuple[datetime, GA4GHServiceInfo]] = {}
Expand All @@ -58,7 +57,7 @@ async def get_service(

if service_info_url in self._cache:
entry_dt, entry = self._cache[service_info_url]
if (entry_age := (dt - entry_dt).total_seconds()) > CACHE_TTL:
if (entry_age := (dt - entry_dt).total_seconds()) > self._config.cache_ttl:
del self._cache[service_info_url]
else:
self._logger.debug(f"Found {service_info_url} in cache (age={entry_age:.1f}s)")
Expand Down Expand Up @@ -123,9 +122,10 @@ async def get_services(

@lru_cache
def get_service_manager(
config: ConfigDependency,
logger: LoggerDependency,
):
return ServiceManager(logger)
return ServiceManager(config, logger)


ServiceManagerDependency = Annotated[ServiceManager, Depends(get_service_manager)]
Expand Down

0 comments on commit 501be18

Please sign in to comment.