fix: timeout error

This commit is contained in:
omg-xtao 2024-03-30 14:02:27 +00:00 committed by GitHub
parent e40e2b3052
commit d7dc6b0884
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,7 +1,7 @@
from typing import Union from typing import Union
from fastapi import FastAPI from fastapi import FastAPI
from httpx import AsyncClient from httpx import AsyncClient, ReadTimeout
from starlette.middleware.cors import CORSMiddleware from starlette.middleware.cors import CORSMiddleware
from starlette.requests import Request from starlette.requests import Request
from starlette.responses import StreamingResponse, RedirectResponse from starlette.responses import StreamingResponse, RedirectResponse
@ -77,7 +77,10 @@ async def get_names(request: Request):
@app.get("/luoxu/avatar/{avatar}") @app.get("/luoxu/avatar/{avatar}")
async def read_avatar(avatar: str): async def read_avatar(avatar: str):
async with AsyncClient(timeout=60) as client: async with AsyncClient(timeout=60) as client:
try:
r = await client.get(f"{local_api}avatar/{avatar}") r = await client.get(f"{local_api}avatar/{avatar}")
except ReadTimeout:
return RedirectResponse(url=ghost_url)
if r.status_code == 307: if r.status_code == 307:
return RedirectResponse(url=ghost_url) return RedirectResponse(url=ghost_url)
return StreamingResponse(r.aiter_bytes(), media_type="image/png") return StreamingResponse(r.aiter_bytes(), media_type="image/png")