diff --git a/bento_service_registry/config.py b/bento_service_registry/config.py index 84949e1..445addb 100644 --- a/bento_service_registry/config.py +++ b/bento_service_registry/config.py @@ -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 diff --git a/bento_service_registry/services.py b/bento_service_registry/services.py index d886e55..83c1292 100644 --- a/bento_service_registry/services.py +++ b/bento_service_registry/services.py @@ -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 @@ -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]] = {} @@ -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)") @@ -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)]