feat: bound upload memory with per-replica slots and a body deadline

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-22 00:23:37 +03:00
co-authored by Claude Opus 5.5
parent fc1524d0d6
commit 6cab9d3c41
55 changed files with 4133 additions and 209 deletions
+53
View File
@@ -7,9 +7,12 @@
"""
import hashlib
import threading
import time
import urllib.parse
import uuid
from collections.abc import Iterator
from concurrent.futures import ThreadPoolExecutor
from typing import Any
import pytest
@@ -32,6 +35,10 @@ from contract import (
BIG_BYTES = 30 * 1024 * 1024
# сколько скачиваний ждать ответа другой реплики (round-robin LB)
REPLICA_ATTEMPTS = 20
# одновременные загрузки сценария «Загрузки в пределах лимита»
CONCURRENT_UPLOADS = 10
# срок клиента загрузки: больше ожидания места (5 с) и сроков шагов
CONCURRENT_CLIENT_TIMEOUT_S = 30
def chunks(data: bytes, size: int = 1024 * 1024) -> Iterator[bytes]:
@@ -645,6 +652,52 @@ def test_repeated_upload(
)
@pytest.mark.spec("attachments", "Загрузки в пределах лимита")
def test_concurrent_uploads(api: Api, base_url: str) -> None:
"""10 участников одного чата загружают по PNG ~1 MiB одновременно: при
параметрах загрузок по умолчанию (4 места и 16 в очереди на процесс)
все помещаются даже в один процесс (design change `upload-memory`,
решение 11)."""
actors = [
api.register(display_name=f"Uploader {number}")
for number in range(CONCURRENT_UPLOADS)
]
chat = api.create_chat(actors[0], *actors[1:], title="Uploads")
bodies = [
images.png_noise(seed=number) for number in range(CONCURRENT_UPLOADS)
]
barrier = threading.Barrier(CONCURRENT_UPLOADS)
def upload(number: int) -> tuple[dict[str, Any], float, float]:
with Api(base_url, timeout=CONCURRENT_CLIENT_TIMEOUT_S) as own:
# соединение открыто до барьера: после него сразу загрузка
own.request("GET", "/health/live")
barrier.wait()
started = time.monotonic()
attachment = own.upload(
actors[number],
chat["id"],
bodies[number],
filename=f"noise-{number}.png",
)
return attachment, started, time.monotonic()
with ThreadPoolExecutor(max_workers=CONCURRENT_UPLOADS) as pool:
results = list(pool.map(upload, range(CONCURRENT_UPLOADS)))
attachments = [attachment for attachment, _, _ in results]
assert len({item["id"] for item in attachments}) == CONCURRENT_UPLOADS
# загрузки правда шли одновременно, а не по очереди клиента
spans = sorted((started, ended) for _, started, ended in results)
overlapping = sum(started < spans[0][1] for started, _ in spans)
assert overlapping >= 2, f"uploads did not overlap: {spans}"
for number, attachment in enumerate(attachments):
assert_content(
api.attachment_content(actors[0], attachment["id"]),
bodies[number],
"image/png",
)
@pytest.mark.spec("attachments", "Скачивание через другую реплику")
def test_download_through_other_replica(
api: Api,