-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* raise 503 if catalog timesout * added requests decorator * listing services is cancellable now
- Loading branch information
Showing
8 changed files
with
100 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
48 changes: 48 additions & 0 deletions
48
services/catalog/src/simcore_service_catalog/utils/requests_decorators.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import asyncio | ||
import logging | ||
from asyncio import CancelledError | ||
from contextlib import suppress | ||
from functools import wraps | ||
from typing import Any, Callable, Coroutine | ||
|
||
from fastapi import Request, Response | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
_DEFAULT_CHECK_INTERVAL_S: float = 0.5 | ||
|
||
|
||
async def _cancel_task_if_client_disconnected( | ||
request: Request, task: asyncio.Task, interval: float = _DEFAULT_CHECK_INTERVAL_S | ||
) -> bool: | ||
with suppress(CancelledError): | ||
while True: | ||
if await request.is_disconnected(): | ||
logger.warning("client %s disconnected!", request.client) | ||
task.cancel() | ||
break | ||
await asyncio.sleep(interval) | ||
|
||
|
||
def cancellable_request(handler: Callable[[Any], Coroutine[Any, Any, Response]]): | ||
"""this decorator periodically checks if the client disconnected and then will cancel the request and return a 499 code (a la nginx).""" | ||
|
||
@wraps(handler) | ||
async def decorator(request: Request, *args, **kwargs) -> Response: | ||
handler_task = asyncio.get_event_loop().create_task( | ||
handler(request, *args, **kwargs) | ||
) | ||
auto_cancel_task = asyncio.get_event_loop().create_task( | ||
_cancel_task_if_client_disconnected(request, handler_task) | ||
) | ||
try: | ||
return await handler_task | ||
except CancelledError: | ||
logger.warning( | ||
"request %s was cancelled by client %s!", request.url, request.client | ||
) | ||
return Response("Oh No!", status_code=499) | ||
finally: | ||
auto_cancel_task.cancel() | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
|
||
# added as minimal integration tests | ||
|
||
|
||
def test_mock(): | ||
assert True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters