fix: A42 terminé — client httpx partagé par boucle (v7.28.0)
FlowDeck CI / lint (push) Canceled after 0s
FlowDeck CI / test (push) Canceled after 0s
FlowDeck CI / docker (push) Canceled after 0s

- `app/services/http_client.py` : `async with shared_client(timeout=15)
  as client:` remplace les 49 créations `async with httpx.AsyncClient(`
  de 14 fichiers (gitea ×21, providers oidc/oauth ×11, calendar ×4,
  automations ×3…) — le pool de connexions est réutilisé au lieu d'être
  recréé à chaque appel. __aexit__ no-op (le client partagé ne se ferme
  pas à la sortie).
- Cache par (boucle d'event, kwargs) en WeakKeyDictionary : un
  AsyncClient n'est JAMAIS partagé entre deux loops (piège des tests
  « Event loop is closed ») — une boucle par test = client propre
  collecté avec la boucle. Clé = kwargs triés, repr() pour les valeurs
  non hashables (`headers=` dict → TypeError rattrapé par la suite).
- Laissés délibérément : github_adapter (transport MockTransport
  injecté), webhook_outbound (client « own_client » fermé par la
  fonction).
- Tests : `test_http_client_shared_and_loop_scoped` (réutilisation mêmes
  kwargs / cloisonné kwargs / cloisonné loop) ; le stub des webhooks
  patche aussi la fabrique `http_client.httpx` + purge du cache (avant :
  webhook_outbound.httpx patché mais la fabrique partagée créait un vrai
  client → réseau réel dans les tests).

suite **1091/1091** · ruff OK · docs à jour
This commit is contained in:
2026-10-01 23:09:45 -04:00
parent 069c438aae
commit 3bb8e87ef2
23 changed files with 200 additions and 71 deletions
+22 -23
View File
@@ -5,9 +5,8 @@ import logging
from datetime import datetime, timedelta
from typing import Any
import httpx
from app.config import settings
from app.services.http_client import shared_client
logger = logging.getLogger(__name__)
@@ -48,7 +47,7 @@ class GiteaClient:
if cached:
return cached
async with httpx.AsyncClient(timeout=15) as client:
async with shared_client(timeout=15) as client:
resp = await client.get(
f"{self._base}/user/repos",
headers=self._headers,
@@ -65,7 +64,7 @@ class GiteaClient:
if cached:
return cached
async with httpx.AsyncClient(timeout=15) as client:
async with shared_client(timeout=15) as client:
resp = await client.get(
f"{self._base}/orgs/{org}/repos",
headers=self._headers,
@@ -82,7 +81,7 @@ class GiteaClient:
cached = self._cached(cache_key)
if cached:
return cached
async with httpx.AsyncClient(timeout=15) as client:
async with shared_client(timeout=15) as client:
resp = await client.get(
f"{self._base}/repos/{owner}/{repo}",
headers=self._headers,
@@ -109,7 +108,7 @@ class GiteaClient:
if cached:
return cached
async with httpx.AsyncClient(timeout=10) as client:
async with shared_client(timeout=10) as client:
resp = await client.get(
f"{self._base}/user/orgs", headers=self._headers
)
@@ -128,7 +127,7 @@ class GiteaClient:
if cached:
return cached
async with httpx.AsyncClient(timeout=15) as client:
async with shared_client(timeout=15) as client:
resp = await client.get(
f"{self._base}/repos/{owner}/{repo}/issues",
headers=self._headers,
@@ -140,7 +139,7 @@ class GiteaClient:
return data
async def get_issue(self, owner: str, repo: str, issue_id: int) -> dict:
async with httpx.AsyncClient(timeout=10) as client:
async with shared_client(timeout=10) as client:
resp = await client.get(
f"{self._base}/repos/{owner}/{repo}/issues/{issue_id}",
headers=self._headers,
@@ -163,7 +162,7 @@ class GiteaClient:
payload["assignees"] = [assignee]
self._invalidate_issue_cache(owner, repo)
async with httpx.AsyncClient(timeout=10) as client:
async with shared_client(timeout=10) as client:
resp = await client.post(
f"{self._base}/repos/{owner}/{repo}/issues",
headers=self._headers,
@@ -177,7 +176,7 @@ class GiteaClient:
) -> dict:
"""Update an issue (title, body, state, labels, milestone, assignees)."""
self._invalidate_issue_cache(owner, repo)
async with httpx.AsyncClient(timeout=10) as client:
async with shared_client(timeout=10) as client:
resp = await client.patch(
f"{self._base}/repos/{owner}/{repo}/issues/{issue_id}",
headers=self._headers,
@@ -191,7 +190,7 @@ class GiteaClient:
) -> list[dict]:
"""Replace all labels on an issue (PUT endpoint)."""
self._invalidate_issue_cache(owner, repo)
async with httpx.AsyncClient(timeout=10) as client:
async with shared_client(timeout=10) as client:
resp = await client.put(
f"{self._base}/repos/{owner}/{repo}/issues/{issue_id}/labels",
headers=self._headers,
@@ -210,7 +209,7 @@ class GiteaClient:
self, owner: str, repo: str, issue_id: int
) -> list[dict]:
"""Get comments for an issue."""
async with httpx.AsyncClient(timeout=10) as client:
async with shared_client(timeout=10) as client:
resp = await client.get(
f"{self._base}/repos/{owner}/{repo}/issues/{issue_id}/comments",
headers=self._headers,
@@ -232,7 +231,7 @@ class GiteaClient:
if cached:
return cached
async with httpx.AsyncClient(timeout=10) as client:
async with shared_client(timeout=10) as client:
resp = await client.get(
f"{self._base}/repos/{owner}/{repo}/labels",
headers=self._headers,
@@ -252,7 +251,7 @@ class GiteaClient:
if cached:
return cached
async with httpx.AsyncClient(timeout=10) as client:
async with shared_client(timeout=10) as client:
resp = await client.get(
f"{self._base}/repos/{owner}/{repo}/milestones",
headers=self._headers,
@@ -267,7 +266,7 @@ class GiteaClient:
async def list_webhooks(self, owner: str, repo: str) -> list[dict]:
"""List webhooks for a repo."""
async with httpx.AsyncClient(timeout=10) as client:
async with shared_client(timeout=10) as client:
resp = await client.get(
f"{self._base}/repos/{owner}/{repo}/hooks",
headers=self._headers,
@@ -292,7 +291,7 @@ class GiteaClient:
"events": events,
"active": True,
}
async with httpx.AsyncClient(timeout=10) as client:
async with shared_client(timeout=10) as client:
resp = await client.post(
f"{self._base}/repos/{owner}/{repo}/hooks",
headers=self._headers,
@@ -303,7 +302,7 @@ class GiteaClient:
async def delete_webhook(self, owner: str, repo: str, hook_id: int) -> bool:
"""Delete a webhook."""
async with httpx.AsyncClient(timeout=10) as client:
async with shared_client(timeout=10) as client:
resp = await client.delete(
f"{self._base}/repos/{owner}/{repo}/hooks/{hook_id}",
headers=self._headers,
@@ -319,7 +318,7 @@ class GiteaClient:
if cached:
return cached
async with httpx.AsyncClient(timeout=10) as client:
async with shared_client(timeout=10) as client:
resp = await client.get(
f"{self._base}/repos/{owner}/{repo}/collaborators",
headers=self._headers,
@@ -341,7 +340,7 @@ class GiteaClient:
cached = self._cached(cache_key)
if cached:
return cached
async with httpx.AsyncClient(timeout=15) as client:
async with shared_client(timeout=15) as client:
resp = await client.get(url, headers=self._headers)
resp.raise_for_status()
data = resp.json()
@@ -357,7 +356,7 @@ class GiteaClient:
cached = self._cached(cache_key)
if cached:
return cached
async with httpx.AsyncClient(timeout=15) as client:
async with shared_client(timeout=15) as client:
resp = await client.get(
f"{self._base}/repos/{owner}/{repo}/contents/{path}",
headers=self._headers,
@@ -382,7 +381,7 @@ class GiteaClient:
}
if sha:
body["sha"] = sha
async with httpx.AsyncClient(timeout=15) as client:
async with shared_client(timeout=15) as client:
resp = await client.put(
f"{self._base}/repos/{owner}/{repo}/contents/{path}",
headers=self._headers,
@@ -398,7 +397,7 @@ class GiteaClient:
async def delete_file(self, owner, repo, path, sha, message):
"""Delete a file from the repo."""
async with httpx.AsyncClient(timeout=15) as client:
async with shared_client(timeout=15) as client:
resp = await client.delete(
f"{self._base}/repos/{owner}/{repo}/contents/{path}",
headers=self._headers,
@@ -422,7 +421,7 @@ class GiteaClient:
cached = self._cached(key)
if cached:
return cached
async with httpx.AsyncClient(timeout=15) as client:
async with shared_client(timeout=15) as client:
resp = await client.get(
f"{self._base}/repos/{owner}/{repo}/commits",
headers=self._headers,