Skip to content

Commit

Permalink
fix: add limit to http pool
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Nov 26, 2024
1 parent 2f31e71 commit 0637852
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
Request,
User,
XRequestIdMiddleware,
http_client,
external_http_client,
pg,
pg_pool_startup,
redis_client,
Expand Down Expand Up @@ -192,7 +192,7 @@ async def startup_fetch_missing_users() -> None:
return

for user in s:
r = await http_client.get(f"https://api.bgm.tv/user/{user}")
r = await external_http_client.get(f"https://api.bgm.tv/user/{user}")
data = r.json()
await pg.execute(
"""
Expand Down
5 changes: 3 additions & 2 deletions server/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
RedirectException,
Request,
User,
external_http_client,
http_client,
pg,
session_key_back_to,
Expand Down Expand Up @@ -170,7 +171,7 @@ async def callback(code: str, request: Request) -> Redirect:

access_token = data["access_token"]

res = await http_client.get(
res = await external_http_client.get(
"https://api.bgm.tv/v0/me", headers={"Authorization": f"Bearer {access_token}"}
)

Expand Down Expand Up @@ -252,7 +253,7 @@ async def refresh_access_token(request: AuthorizedRequest, back_to: str) -> None

access_token = data["access_token"]

res = await http_client.get(
res = await external_http_client.get(
"https://api.bgm.tv/v0/me", headers={"Authorization": f"Bearer {access_token}"}
)

Expand Down
4 changes: 2 additions & 2 deletions server/badge.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from server.base import (
disable_cookies_opt,
http_client,
external_http_client,
pg,
redis_client,
)
Expand Down Expand Up @@ -111,7 +111,7 @@ async def __get_badge(count: int) -> bytes:
else:
color = "green"

res = await http_client.get(f"https://img.shields.io/badge/待审核-{s}-{color}")
res = await external_http_client.get(f"https://img.shields.io/badge/待审核-{s}-{color}")
badge = res.content
await redis_client.set(val_key, badge, ex=7 * 24 * 3600)

Expand Down
17 changes: 17 additions & 0 deletions server/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,24 @@ class QueueItem:
http_client = httpx.AsyncClient(
follow_redirects=False,
headers={"user-agent": "trim21/submit-patch"},
limits=httpx.Limits(
max_keepalive_connections=3,
max_connections=5,
keepalive_expiry=5,
),
)

external_http_client = httpx.AsyncClient(
follow_redirects=False,
headers={"user-agent": "trim21/submit-patch"},
limits=httpx.Limits(
max_keepalive_connections=3,
max_connections=5,
keepalive_expiry=5,
),
)


pg = asyncpg.create_pool(dsn=PG_DSN, server_settings={"application_name": "patch"})


Expand Down
3 changes: 2 additions & 1 deletion server/contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
AuthorizedRequest,
QueueItem,
Request,
external_http_client,
http_client,
patch_keys,
pg,
Expand All @@ -41,7 +42,7 @@


async def _validate_captcha(cf_turnstile_response: str) -> None:
res = await http_client.post(
res = await external_http_client.post(
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
data={
"secret": TURNSTILE_SECRET_KEY,
Expand Down

0 comments on commit 0637852

Please sign in to comment.