我希望创建一个接收请求、进行一些处理并将请求转发到另一个端点的服务器。我似乎在更高的并发性下遇到了我client.post
导致httpx.ConnectTimeout
异常的问题。
我还没有完全排除端点出现问题的可能性(我目前正在与他们合作调试任何可能在他们终端上的东西),但我试图弄清楚我的终端是否有问题或如果有任何明显的低效率我可以改进。
我在 ECS 中运行它,目前在一个任务有 4 个 vCPU 的集群上。我正在使用 docker 镜像uvicorn-gunicorn-fastapi
(https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker)。目前所有默认设置减去绑定/端口/日志记录。这是一个最小的代码示例:
import httpx
from fastapi import FastAPI, Request, Response
app = FastAPI()
def process_request(path, request):
#Process Request Here
def create_headers(path):
#Create headers here
@app.get('/')
async def root(path: str, request: Request):
endpoint = 'https://endpoint.com/'
querystring = 'path=' + path
data = process_request(request, path, request)
headers = create_headers(request)
async with httpx.AsyncClient() as client:
await client.post(endpoint + "?" + querystring, data=data, headers=headers)
return Response(status_code=200)