From d7dc6b08841efd386cbbca11351a022854845124 Mon Sep 17 00:00:00 2001 From: omg-xtao <100690902+omg-xtao@users.noreply.github.com> Date: Sat, 30 Mar 2024 14:02:27 +0000 Subject: [PATCH] fix: timeout error --- main.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index b89f20c..caa4a12 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ from typing import Union from fastapi import FastAPI -from httpx import AsyncClient +from httpx import AsyncClient, ReadTimeout from starlette.middleware.cors import CORSMiddleware from starlette.requests import Request from starlette.responses import StreamingResponse, RedirectResponse @@ -77,7 +77,10 @@ async def get_names(request: Request): @app.get("/luoxu/avatar/{avatar}") async def read_avatar(avatar: str): async with AsyncClient(timeout=60) as client: - r = await client.get(f"{local_api}avatar/{avatar}") + try: + r = await client.get(f"{local_api}avatar/{avatar}") + except ReadTimeout: + return RedirectResponse(url=ghost_url) if r.status_code == 307: return RedirectResponse(url=ghost_url) return StreamingResponse(r.aiter_bytes(), media_type="image/png")