forked from mtusi2026/mtusi2026lab2
A pass without --deep released the quarantine of a missing or resized attachment as soon as an object of the same size appeared under its key, so other bytes of the same length went back to /content. The pass now streams and hashes the object of every quarantined row whose size matches again, deep or not, and releases it only when the SHA-256 matches; other bytes keep the row quarantined. Consistent data still costs no GET. The keyset reads page + 1 rows, so a pass takes exactly ceil(N/page) SQL pages instead of one more for the empty tail. The reconciler is assembled by dishka from PlatformProvider and the attachment storage provider that the API uses too, and it publishes the events of the intents it drops, so audit.attachment_discarded reason=reconcile reaches the journal. Any failure of a pass, not only Unavailable, ends as reconcile.failed with the error type and exit code 1. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27
101 lines
3.4 KiB
Python
101 lines
3.4 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()
|
|
|
|
|
|
class RecordingPublisher:
|
|
__slots__ = ("events",)
|
|
|
|
def __init__(self) -> None:
|
|
self.events: list[Any] = []
|
|
|
|
async def publish(self, events: Sequence[Any]) -> None:
|
|
self.events.extend(events)
|