feat: bound aggregate Gitea transport traffic (#246)
CI / lint (pull_request) Successful in 21s
CI / build-frontend (pull_request) Successful in 5s

This commit is contained in:
timmy committed 2026-08-08 00:34:10 +00:00
1 parent d7d6170c3e
commit 4f6ba0302d
3 files changed
+321 -4

No files matched your search

+192
View File
@@ -28,6 +28,198 @@ async def test_gitea_transport_is_reused_across_requests_and_closed():
assert client.is_closed
@pytest.mark.anyio
async def test_gitea_transport_coalesces_identical_concurrent_gets():
calls = 0
release = asyncio.Event()
async def handler(request):
nonlocal calls
calls += 1
await release.wait()
return httpx.Response(200, json={"path": request.url.path})
gitea_proxy.start_client(
transport=httpx.MockTransport(handler),
max_concurrency=4,
admission_timeout=0.1,
)
try:
requests = [asyncio.create_task(gitea_proxy.fetch("user")) for _ in range(20)]
await asyncio.sleep(0)
release.set()
results = await asyncio.gather(*requests)
finally:
await gitea_proxy.stop_client()
assert calls == 1
assert results == [{"path": "/api/v1/user"}] * 20
@pytest.mark.anyio
async def test_cancelling_one_coalesced_waiter_keeps_the_shared_get_running():
started = asyncio.Event()
release = asyncio.Event()
async def handler(request):
started.set()
await release.wait()
return httpx.Response(200, json={"path": request.url.path})
gitea_proxy.start_client(transport=httpx.MockTransport(handler))
owner = asyncio.create_task(gitea_proxy.fetch("user"))
try:
await started.wait()
waiter = asyncio.create_task(gitea_proxy.fetch("user"))
await asyncio.sleep(0)
waiter.cancel()
with pytest.raises(asyncio.CancelledError):
await waiter
release.set()
assert await owner == {"path": "/api/v1/user"}
finally:
release.set()
await asyncio.gather(owner, return_exceptions=True)
await gitea_proxy.stop_client()
@pytest.mark.anyio
async def test_gitea_transport_rejects_reads_beyond_bounded_capacity():
active = 0
peak = 0
saturated = asyncio.Event()
release = asyncio.Event()
async def handler(request):
nonlocal active, peak
active += 1
peak = max(peak, active)
if active == 2:
saturated.set()
try:
await release.wait()
return httpx.Response(200, json={"path": request.url.path})
finally:
active -= 1
gitea_proxy.start_client(
transport=httpx.MockTransport(handler),
max_concurrency=3,
admission_timeout=0.02,
)
first = asyncio.create_task(gitea_proxy.fetch("first"))
second = asyncio.create_task(gitea_proxy.fetch("second"))
try:
await asyncio.wait_for(saturated.wait(), timeout=0.2)
with pytest.raises(gitea_proxy.GiteaOverloadedError):
await gitea_proxy.fetch("third")
finally:
release.set()
await asyncio.gather(first, second)
await gitea_proxy.stop_client()
assert peak == 2
@pytest.mark.anyio
async def test_gitea_transport_reserves_one_slot_for_mutations_and_bounds_them():
active = 0
peak = 0
reads_started = asyncio.Event()
mutation_started = asyncio.Event()
release = asyncio.Event()
async def handler(request):
nonlocal active, peak
active += 1
peak = max(peak, active)
if active == 2:
reads_started.set()
if request.method == "POST":
mutation_started.set()
try:
await release.wait()
return httpx.Response(200, json={"method": request.method})
finally:
active -= 1
client = gitea_proxy.start_client(
transport=httpx.MockTransport(handler),
max_concurrency=3,
admission_timeout=0.02,
)
reads = [
asyncio.create_task(gitea_proxy.fetch("first")),
asyncio.create_task(gitea_proxy.fetch("second")),
]
mutation = None
try:
await asyncio.wait_for(reads_started.wait(), timeout=0.2)
mutation = asyncio.create_task(client.post("/api/v1/mutate", json={"value": 1}))
await asyncio.wait_for(mutation_started.wait(), timeout=0.2)
with pytest.raises(gitea_proxy.GiteaOverloadedError):
await asyncio.wait_for(
client.post("/api/v1/mutate", json={"value": 1}), timeout=0.1
)
finally:
release.set()
await asyncio.gather(*reads, *(task for task in [mutation] if task is not None))
await gitea_proxy.stop_client()
assert peak == 3
@pytest.mark.anyio
async def test_gitea_transport_reads_bulkhead_configuration_from_environment(monkeypatch):
monkeypatch.setenv("GITEA_MAX_CONCURRENCY", "12")
monkeypatch.setenv("GITEA_ADMISSION_TIMEOUT_SECONDS", "0.75")
client = gitea_proxy.start_client(
transport=httpx.MockTransport(lambda request: httpx.Response(200, json={}))
)
try:
assert client.max_concurrency == 12
assert client.admission_timeout == 0.75
finally:
await gitea_proxy.stop_client()
@pytest.mark.anyio
async def test_streaming_reads_share_the_same_bounded_read_capacity():
active = 0
saturated = asyncio.Event()
release = asyncio.Event()
async def handler(request):
nonlocal active
active += 1
if active == 2:
saturated.set()
try:
await release.wait()
return httpx.Response(200, content=b"diff")
finally:
active -= 1
gitea_proxy.start_client(
transport=httpx.MockTransport(handler),
max_concurrency=3,
admission_timeout=0.02,
)
streams = [
asyncio.create_task(gitea_proxy.fetch_text("first", 100)),
asyncio.create_task(gitea_proxy.fetch_text("second", 100)),
]
try:
await asyncio.wait_for(saturated.wait(), timeout=0.2)
with pytest.raises(gitea_proxy.GiteaOverloadedError):
await asyncio.wait_for(gitea_proxy.fetch("third"), timeout=0.1)
finally:
release.set()
await asyncio.gather(*streams)
await gitea_proxy.stop_client()
@pytest.mark.anyio
async def test_application_lifespan_opens_and_closes_gitea_transport(monkeypatch):
calls = []