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

fix(main.py): Introduce request time limit on fast requests #581

Merged
merged 1 commit into from
Dec 4, 2024
Merged
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
18 changes: 13 additions & 5 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import os
import re
import asyncio
from typing import List, Union, Optional
from datetime import datetime
from contextlib import asynccontextmanager
Expand All @@ -33,7 +34,7 @@
from pymongo.errors import DuplicateKeyError
from fastapi_users import FastAPIUsers
from beanie import PydanticObjectId
from kernelci.api.models import (

Check failure on line 37 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Unable to import 'kernelci.api.models'
Node,
Hierarchy,
PublishEvent,
Expand Down Expand Up @@ -598,6 +599,13 @@
add_pagination(app)


async def db_find_node_nonpaginated(query_params):
"""Find all the matching nodes without pagination"""
model = Node
translated_params = model.translate_fields(query_params)
return await db.find_by_attributes_nonpaginated(model, translated_params)


@app.get('/nodes/fast', response_model=List[Node])
async def get_nodes_fast(request: Request):
"""Get all the nodes if no request parameters have passed.
Expand All @@ -611,11 +619,11 @@

try:
# Query using the base Node model, regardless of the specific
# node type
model = Node
translated_params = model.translate_fields(query_params)
resp = await db.find_by_attributes_nonpaginated(model,
translated_params)
# node type, use asyncio.wait_for with timeout 30 seconds
resp = await asyncio.wait_for(
db_find_node_nonpaginated(query_params),
timeout=15
)
return resp
except KeyError as error:
raise HTTPException(
Expand Down
Loading