Skip to content

Commit

Permalink
🐛Separate caching director call from user dependent service listing (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg authored May 4, 2022
1 parent eec1f9e commit 2a3913b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/models-library/src/models_library/services_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

class ServiceGroupAccessRights(BaseModel):
execute_access: bool = Field(
False,
default=False,
description="defines whether the group can execute the service",
)
write_access: bool = Field(
False, description="defines whether the group can modify the service"
default=False, description="defines whether the group can modify the service"
)


Expand Down
39 changes: 19 additions & 20 deletions services/catalog/src/simcore_service_catalog/api/routes/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import asyncio
import logging
import urllib.parse
from collections import deque
from typing import Any, Deque, Dict, Final, List, Optional, Set, Tuple, cast
from typing import Any, Dict, Final, List, Optional, Set, Tuple, cast

from aiocache import cached
from fastapi import APIRouter, Depends, Header, HTTPException, status
Expand Down Expand Up @@ -143,19 +142,11 @@ async def list_services(

# caching this steps brings down the time to generate it at the expense of being sometimes a bit out of date
@cached(ttl=DIRECTOR_CACHING_TTL)
async def cached_registry_services() -> Deque[Tuple[str, str, Dict[str, Any]]]:
services_in_registry = await director_client.get("/services")
filtered_services = deque(
(s["key"], s["version"], s)
for s in (
request.app.state.frontend_services_catalog + services_in_registry
)
if (s.get("key"), s.get("version")) in services_in_db
)
return filtered_services
async def cached_registry_services() -> dict[str, Any]:
return cast(dict[str, Any], await director_client.get("/services"))

(
registry_filtered_services,
services_in_registry,
services_access_rights,
services_owner_emails,
) = await asyncio.gather(
Expand All @@ -180,12 +171,17 @@ async def cached_registry_services() -> Deque[Tuple[str, str, Dict[str, Any]]]:
asyncio.get_event_loop().run_in_executor(
None,
_prepare_service_details,
details,
services_in_db[key, version],
services_access_rights[key, version],
services_owner_emails.get(services_in_db[key, version].owner),
s,
services_in_db[s["key"], s["version"]],
services_access_rights[s["key"], s["version"]],
services_owner_emails.get(
services_in_db[s["key"], s["version"]].owner or 0
),
)
for key, version, details in registry_filtered_services
for s in (
request.app.state.frontend_services_catalog + services_in_registry
)
if (s.get("key"), s.get("version")) in services_in_db
]
)
return [s for s in services_details if s is not None]
Expand Down Expand Up @@ -315,8 +311,11 @@ async def get_service(
)
_service_data = frontend_service
else:
services_in_registry = await director_client.get(
f"/services/{urllib.parse.quote_plus(service_key)}/{service_version}"
services_in_registry = cast(
list[Any],
await director_client.get(
f"/services/{urllib.parse.quote_plus(service_key)}/{service_version}"
),
)
_service_data = services_in_registry[0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def _cancel_task_if_client_disconnected(
await asyncio.sleep(interval)


def cancellable_request(handler: Callable[..., Coroutine[Any, Any, Response]]):
def cancellable_request(handler: Callable[..., Coroutine[Any, Any, Any]]):
"""this decorator periodically checks if the client disconnected and then will cancel the request and return a 499 code (a la nginx)."""

@wraps(handler)
Expand Down

0 comments on commit 2a3913b

Please sign in to comment.