forked from mtusi2026/mtusi2026lab4
The pipeline now runs the guard first, so an anonymous upload with a too large Content-Length gets 401 with WWW-Authenticate, as the contract asks; the guard does not read the body, so 413 still comes before the first body byte for an authenticated client. The single request deadline cut slow but steady uploads and downloads: a client slower than about 1.3 MB/s could not move 10 MiB in 8 s. Each body chunk the application receives or sends now moves the deadline to min(now, start + moved / MIN_TRANSFER_RATE_BYTES) + REQUEST_TIMEOUT, 128 KiB/s by default, like mod_reqtimeout with a ceiling: a steady transfer lives as long as it moves, a stalled or crawling body is cut after the request timeout as before, and the work after the last body byte still gets at most the request timeout. The pipeline's own error responses bypass the pacing. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27
91 lines
3.2 KiB
Python
91 lines
3.2 KiB
Python
import asyncio
|
|
from collections.abc import AsyncIterator, Awaitable, Callable, Sequence
|
|
from datetime import UTC, datetime, timedelta
|
|
from typing import Any
|
|
|
|
from dishka import Provider, Scope, provide
|
|
|
|
from messenger.attachments.application.errors import ObjectMissingError
|
|
from messenger.attachments.application.ports import ObjectContent, ObjectStorage, StoredObject
|
|
from messenger.shared.application.errors import Unavailable
|
|
|
|
|
|
class NullLogger:
|
|
__slots__ = ()
|
|
|
|
def info(self, event: str, /, **fields: Any) -> None: ...
|
|
|
|
def warning(self, event: str, /, **fields: Any) -> None: ...
|
|
|
|
def error(self, event: str, /, **fields: Any) -> None: ...
|
|
|
|
|
|
class FrozenClock:
|
|
__slots__ = ("_current",)
|
|
|
|
def __init__(self, start: datetime) -> None:
|
|
self._current = start
|
|
|
|
def now(self) -> datetime:
|
|
self._current += timedelta(microseconds=1)
|
|
return self._current
|
|
|
|
|
|
async def pieces(data: bytes, size: int = 65_536, delay: float = 0.0) -> AsyncIterator[bytes]:
|
|
for start in range(0, len(data), size):
|
|
await asyncio.sleep(delay)
|
|
yield data[start : start + size]
|
|
|
|
|
|
class MemoryObjectStorage:
|
|
__slots__ = ("after_page", "after_put", "delay", "failing", "modified", "objects")
|
|
|
|
def __init__(self) -> None:
|
|
self.objects: dict[str, tuple[bytes, str]] = {}
|
|
self.modified: dict[str, datetime] = {}
|
|
self.failing = False
|
|
self.delay = 0.0
|
|
self.after_put: Callable[[str], Awaitable[None]] | None = None
|
|
self.after_page: Callable[[], Awaitable[None]] | None = None
|
|
|
|
async def put(self, key: str, data: bytes, content_type: str) -> None:
|
|
if self.failing:
|
|
raise Unavailable("S3 недоступно")
|
|
self.objects[key] = (bytes(data), content_type)
|
|
self.modified[key] = datetime.now(UTC)
|
|
if self.after_put is not None:
|
|
await self.after_put(key)
|
|
|
|
async def listing(self, prefix: str, page_size: int) -> AsyncIterator[Sequence[StoredObject]]:
|
|
if self.failing:
|
|
raise Unavailable("S3 недоступно")
|
|
keys = sorted(key for key in self.objects if key.startswith(prefix))
|
|
for start in range(0, len(keys), page_size):
|
|
yield [self._stored(key) for key in keys[start : start + page_size]]
|
|
if self.after_page is not None:
|
|
await self.after_page()
|
|
|
|
async def stat(self, key: str) -> StoredObject | None:
|
|
return self._stored(key) if key in self.objects else None
|
|
|
|
def _stored(self, key: str) -> StoredObject:
|
|
return StoredObject(key, len(self.objects[key][0]), self.modified[key])
|
|
|
|
async def open(self, key: str) -> ObjectContent:
|
|
if self.failing:
|
|
raise Unavailable("S3 недоступно")
|
|
if key not in self.objects:
|
|
raise ObjectMissingError(key)
|
|
data = self.objects[key][0]
|
|
return ObjectContent(size=len(data), chunks=pieces(data, delay=self.delay))
|
|
|
|
async def delete(self, key: str) -> None:
|
|
self.objects.pop(key, None)
|
|
self.modified.pop(key, None)
|
|
|
|
|
|
class MemoryStorageProvider(Provider):
|
|
@provide(scope=Scope.APP)
|
|
def storage(self) -> ObjectStorage:
|
|
return MemoryObjectStorage()
|