Skip to content

Commit

Permalink
chore: add timeout for api
Browse files Browse the repository at this point in the history
  • Loading branch information
nquang29 committed Jan 4, 2025
1 parent 4e65dab commit 61da8a0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/lib/backend/http/api/conversations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ Future<CreateConversationResponse?> processInProgressConversation() async {
}

Future<List<ServerConversation>> getConversations(
{int limit = 50, int offset = 0, List<ConversationStatus> statuses = const []}) async {
{int limit = 50, int offset = 0, List<ConversationStatus> statuses = const [], int segmentLimit = 50}) async {
var response = await makeApiCall(
url:
'${Env.apiBaseUrl}v1/memories?limit=$limit&offset=$offset&statuses=${statuses.map((val) => val.toString().split(".").last).join(",")}',
'${Env.apiBaseUrl}v1/memories?limit=$limit&offset=$offset&statuses=${statuses.map((val) => val.toString().split(".").last).join(",")}&segment_limit=$segmentLimit',
headers: {},
method: 'GET',
body: '');
Expand Down
2 changes: 1 addition & 1 deletion app/lib/pages/settings/developer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class _DeveloperSettingsPageState extends State<DeveloperSettingsPage> {
),
);
List<ServerConversation> memories =
await getConversations(limit: 10000, offset: 0); // 10k for now
await getConversations(limit: 10000, offset: 0, segmentLimit: 10000); // 10k for now
String json = const JsonEncoder.withIndent(" ").convert(memories);
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/conversations.json');
Expand Down
9 changes: 9 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from modal import Image, App, asgi_app, Secret
from routers import workflow, chat, firmware, plugins, memories, transcribe_v2, notifications, \
speech_profile, agents, facts, users, processing_memories, trends, sdcard, sync, apps, custom_auth, payment
from utils.other.timeout import TimeoutMiddleware

if os.environ.get('SERVICE_ACCOUNT_JSON'):
service_account_info = json.loads(os.environ["SERVICE_ACCOUNT_JSON"])
Expand Down Expand Up @@ -40,6 +41,14 @@

app.include_router(payment.router)

timeoutSecond = 2 * 60
if os.environ.get('HTTP_TIMEOUT'):
try:
timeoutSecond = float(os.environ['HTTP_TIMEOUT'])
except ValueError:
raise ValueError("HTTP_TIMEOUT invalid")
app.add_middleware(TimeoutMiddleware, timeout=timeoutSecond)

modal_app = App(
name='backend',
secrets=[Secret.from_name("gcp-credentials"), Secret.from_name('envs')],
Expand Down
9 changes: 6 additions & 3 deletions backend/routers/memories.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ def reprocess_memory(


@router.get('/v1/memories', response_model=List[Memory], tags=['memories'])
def get_memories(limit: int = 100, offset: int = 0, statuses: str = "", uid: str = Depends(auth.get_current_user_uid)):
def get_memories(limit: int = 100, offset: int = 0, statuses: str = "", segment_limit: int = None, uid: str = Depends(auth.get_current_user_uid)):
print('get_memories', uid, limit, offset, statuses)
return memories_db.get_memories(uid, limit, offset, include_discarded=True,
memories = memories_db.get_memories(uid, limit, offset, include_discarded=True,
statuses=statuses.split(",") if len(statuses) > 0 else [])

if segment_limit is not None:
for memory in memories:
memory["transcript_segments"] = memory.get("transcript_segments", [])[:segment_limit]
return memories

@router.get("/v1/memories/{memory_id}", response_model=Memory, tags=['memories'])
def get_memory_by_id(memory_id: str, uid: str = Depends(auth.get_current_user_uid)):
Expand Down
15 changes: 15 additions & 0 deletions backend/utils/other/timeout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import Response
from fastapi import Request
import asyncio

class TimeoutMiddleware(BaseHTTPMiddleware):
def __init__(self, app, timeout: float):
super().__init__(app)
self.timeout = timeout

async def dispatch(self, request: Request, call_next):
try:
return await asyncio.wait_for(call_next(request), timeout=self.timeout)
except asyncio.TimeoutError:
return Response(status_code=504)

0 comments on commit 61da8a0

Please sign in to comment.