Skip to content
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

perf: cache service info responses + JSON file load #42

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions bento_service_registry/bento_services_json.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import aiofiles
import orjson
from async_lru import alru_cache
from fastapi import Depends

from typing import Annotated

from .config import ConfigDependency
from .config import Config, ConfigDependency
from .types import BentoService


Expand All @@ -22,7 +23,9 @@
BentoServicesByKind = dict[str, BentoService]


async def get_bento_services_by_compose_id(config: ConfigDependency) -> BentoServicesByComposeID:
# cache bento_services.json contents for the lifetime of the service:
@alru_cache()
async def _get_bento_services_by_compose_id(config: Config) -> BentoServicesByComposeID:
async with aiofiles.open(config.bento_services, "rb") as fh:
bento_services_data: BentoServicesByComposeID = orjson.loads(await fh.read())

Expand All @@ -43,6 +46,10 @@ async def get_bento_services_by_compose_id(config: ConfigDependency) -> BentoSer
}


async def get_bento_services_by_compose_id(config: ConfigDependency) -> BentoServicesByComposeID:
return await _get_bento_services_by_compose_id(config)


BentoServicesByComposeIDDependency = Annotated[BentoServicesByComposeID, Depends(get_bento_services_by_compose_id)]


Expand Down
2 changes: 1 addition & 1 deletion bento_service_registry/http_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


async def get_http_session(config: ConfigDependency):
connector = aiohttp.TCPConnector(verify_ssl=config.bento_validate_ssl)
connector = aiohttp.TCPConnector(verify_ssl=config.bento_validate_ssl, force_close=True)
session = aiohttp.ClientSession(
connector=connector,
timeout=aiohttp.ClientTimeout(total=config.contact_timeout),
Expand Down
11 changes: 9 additions & 2 deletions bento_service_registry/service_info.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from async_lru import alru_cache
from bento_lib.service_info.helpers import build_service_info_from_pydantic_config
from bento_lib.service_info.types import GA4GHServiceInfo
from bento_service_registry import __version__
from fastapi import Depends
from logging import Logger
from typing import Annotated

from .config import ConfigDependency
from .config import Config, ConfigDependency
from .constants import BENTO_SERVICE_KIND, SERVICE_TYPE
from .logger import LoggerDependency

Expand All @@ -15,7 +17,8 @@
]


async def get_service_info(config: ConfigDependency, logger: LoggerDependency) -> GA4GHServiceInfo:
@alru_cache()
async def _get_service_info(config: Config, logger: Logger) -> GA4GHServiceInfo:
return await build_service_info_from_pydantic_config(
config,
logger,
Expand All @@ -25,4 +28,8 @@ async def get_service_info(config: ConfigDependency, logger: LoggerDependency) -
)


async def get_service_info(config: ConfigDependency, logger: LoggerDependency) -> GA4GHServiceInfo:
return await _get_service_info(config, logger)


ServiceInfoDependency = Annotated[GA4GHServiceInfo, Depends(get_service_info)]
21 changes: 18 additions & 3 deletions bento_service_registry/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@
]


CACHE_TTL = 30


class ServiceManager:
def __init__(self, logger: logging.Logger):
self._co: Awaitable[list[dict | None]] | None = None
self._logger = logger
self._cache: dict[str, tuple[datetime, GA4GHServiceInfo]] = {}

async def get_service(
self,
Expand All @@ -51,6 +55,15 @@ async def get_service(
service_info_url: str = urljoin(f"{s_url}/", "service-info")

dt = datetime.now()

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:
del self._cache[service_info_url]
else:
self._logger.debug(f"Found {service_info_url} in cache (age={entry_age:.1f}s)")
return entry

self._logger.info(f"Contacting {service_info_url}{' with bearer token' if authz_header else ''}")

service_resp: dict | None = None
Expand All @@ -70,7 +83,9 @@ async def get_service(

try:
service_resp = {**(await r.json()), "url": s_url}
self._logger.debug(f"{service_info_url}: Took {(datetime.now() - dt).total_seconds():.1f}s")
res_dt = datetime.now()
self._cache[service_info_url] = (res_dt, service_resp)
self._logger.debug(f"{service_info_url}: Took {(res_dt - dt).total_seconds():.1f}s")
except (JSONDecodeError, aiohttp.ContentTypeError, TypeError) as e:
# JSONDecodeError can happen if the JSON is invalid
# ContentTypeError can happen if the Content-Type is not application/json
Expand All @@ -95,10 +110,10 @@ async def get_services(
service_info: GA4GHServiceInfo,
) -> tuple[dict, ...]:
if not self._co:
self._co = asyncio.gather(*[
self._co = asyncio.gather(*(
self.get_service(authz_header, http_session, service_info, s)
for s in bento_services_by_kind.values()
])
))

service_list: list[dict | None] = await self._co
self._co = None
Expand Down
Loading
Loading