opencode commited on
Commit
16b68a5
·
1 Parent(s): f726e88

perf: use singleton httpx client to reduce RAM usage

Browse files
Files changed (1) hide show
  1. main.py +35 -25
main.py CHANGED
@@ -1,13 +1,24 @@
1
  from fastapi import FastAPI, Request, Response, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
  import httpx
4
- import asyncio
5
-
6
- app = FastAPI()
7
 
8
  # The target Azure VM backend URL
9
  TARGET_URL = "https://snap-providers-mercy-protocol.trycloudflare.com"
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  app.add_middleware(
12
  CORSMiddleware,
13
  allow_origins=["*"],
@@ -17,34 +28,33 @@ app.add_middleware(
17
 
18
  @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"])
19
  async def proxy(request: Request, path: str):
20
- """Forwards all requests to the target backend."""
21
  url = f"{TARGET_URL}/{path}"
22
 
23
  if request.method == "OPTIONS":
24
  return Response(status_code=200)
25
 
26
- async with httpx.AsyncClient() as client:
27
- body = await request.body()
28
- headers = dict(request.headers)
29
- headers.pop("host", None)
 
 
 
 
 
 
 
 
30
 
31
- try:
32
- resp = await client.request(
33
- method=request.method,
34
- url=url,
35
- headers=headers,
36
- content=body,
37
- timeout=60.0
38
- )
39
-
40
- # Return the original response from the backend
41
- return Response(
42
- content=resp.content,
43
- status_code=resp.status_code,
44
- headers=dict(resp.headers)
45
- )
46
- except httpx.RequestError as e:
47
- raise HTTPException(status_code=502, detail=f"Proxy Error: {str(e)}")
48
 
49
  if __name__ == "__main__":
50
  import uvicorn
 
1
  from fastapi import FastAPI, Request, Response, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
  import httpx
4
+ from contextlib import asynccontextmanager
 
 
5
 
6
  # The target Azure VM backend URL
7
  TARGET_URL = "https://snap-providers-mercy-protocol.trycloudflare.com"
8
 
9
+ # Use a global client to avoid creating a new one for every request (reduces RAM/CPU overhead)
10
+ client_state = {}
11
+
12
+ @asynccontextmanager
13
+ async def lifespan(app: FastAPI):
14
+ # Initialize the client on startup
15
+ client_state["client"] = httpx.AsyncClient(timeout=60.0)
16
+ yield
17
+ # Close the client on shutdown
18
+ await client_state["client"].aclose()
19
+
20
+ app = FastAPI(lifespan=lifespan)
21
+
22
  app.add_middleware(
23
  CORSMiddleware,
24
  allow_origins=["*"],
 
28
 
29
  @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"])
30
  async def proxy(request: Request, path: str):
31
+ \"\"\"Forwards all requests to the target backend.\"\"\"
32
  url = f"{TARGET_URL}/{path}"
33
 
34
  if request.method == "OPTIONS":
35
  return Response(status_code=200)
36
 
37
+ client = client_state["client"]
38
+ body = await request.body()
39
+ headers = dict(request.headers)
40
+ headers.pop("host", None)
41
+
42
+ try:
43
+ resp = await client.request(
44
+ method=request.method,
45
+ url=url,
46
+ headers=headers,
47
+ content=body
48
+ )
49
 
50
+ return Response(
51
+ content=resp.content,
52
+ status_code=resp.status_code,
53
+ headers=dict(resp.headers)
54
+ )
55
+ except httpx.RequestError as e:
56
+ raise HTTPException(status_code=502, detail=f"Proxy Error: {str(e)}")
57
+
 
 
 
 
 
 
 
 
 
58
 
59
  if __name__ == "__main__":
60
  import uvicorn