forked from mtusi2026/mtusi2026lab3
Исправление ошибок ЛР3
This commit is contained in:
+40
-12
@@ -64,19 +64,19 @@ class Storage:
|
||||
return dict(row) if row else None
|
||||
|
||||
async def create_chat(
|
||||
self, title: str, member_ids: list[str], creator_id: str,
|
||||
self, title: str, member_ids: list[str], creator_id: str,
|
||||
) -> dict:
|
||||
unique = list(dict.fromkeys(member_ids))
|
||||
if creator_id not in unique:
|
||||
if creator_id not in member_ids:
|
||||
raise ValueError("creator_not_member")
|
||||
|
||||
chat_id = str(uuid.uuid4())
|
||||
async with self._pool.acquire() as conn:
|
||||
async with conn.transaction():
|
||||
existing = await conn.fetchval(
|
||||
"SELECT count(*) FROM users WHERE id = ANY($1::uuid[])",
|
||||
unique,
|
||||
member_ids,
|
||||
)
|
||||
if existing != len(unique):
|
||||
if existing != len(member_ids):
|
||||
raise ValueError("invalid_member")
|
||||
await conn.execute(
|
||||
"INSERT INTO chats (id, title) VALUES ($1, $2)",
|
||||
@@ -84,7 +84,7 @@ class Storage:
|
||||
)
|
||||
await conn.executemany(
|
||||
"INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)",
|
||||
[(chat_id, uid) for uid in unique],
|
||||
[(chat_id, uid) for uid in member_ids],
|
||||
)
|
||||
created = await conn.fetchval(
|
||||
"SELECT created_at FROM chats WHERE id = $1", chat_id,
|
||||
@@ -92,7 +92,7 @@ class Storage:
|
||||
return {
|
||||
"id": chat_id,
|
||||
"title": title,
|
||||
"member_ids": unique,
|
||||
"member_ids": member_ids,
|
||||
"created_at": _now_iso(created),
|
||||
}
|
||||
|
||||
@@ -203,6 +203,11 @@ class Storage:
|
||||
raise ValueError("not_found")
|
||||
message = self._message_row(row)
|
||||
|
||||
await conn.execute(
|
||||
"UPDATE chats SET version = version + 1 WHERE id = $1",
|
||||
chat_id,
|
||||
)
|
||||
|
||||
if idempotency_key is not None:
|
||||
await conn.execute(
|
||||
"""
|
||||
@@ -263,9 +268,13 @@ class Storage:
|
||||
raise ValueError("not_found")
|
||||
|
||||
cache_key = f"messages:{chat_id}:{limit}:{cursor or ''}"
|
||||
|
||||
cached = await self._cache.get_json(cache_key)
|
||||
if cached is not None:
|
||||
return cached["items"], cached["next_cursor"]
|
||||
version_now = await self._get_chat_version(chat_id)
|
||||
if cached.get("version") == version_now:
|
||||
return cached["items"], cached["next_cursor"]
|
||||
await self._cache.delete(cache_key)
|
||||
|
||||
lock_key = f"lock:{cache_key}"
|
||||
got_lock = await self._cache.try_lock(lock_key, ttl=10)
|
||||
@@ -275,12 +284,18 @@ class Storage:
|
||||
await asyncio.sleep(0.1)
|
||||
cached = await self._cache.get_json(cache_key)
|
||||
if cached is not None:
|
||||
return cached["items"], cached["next_cursor"]
|
||||
version_now = await self._get_chat_version(chat_id)
|
||||
if cached.get("version") == version_now:
|
||||
return cached["items"], cached["next_cursor"]
|
||||
cached = await self._cache.get_json(cache_key)
|
||||
if cached is not None:
|
||||
return cached["items"], cached["next_cursor"]
|
||||
version_now = await self._get_chat_version(chat_id)
|
||||
if cached.get("version") == version_now:
|
||||
return cached["items"], cached["next_cursor"]
|
||||
|
||||
try:
|
||||
version_before = await self._get_chat_version(chat_id)
|
||||
|
||||
async with self._pool.acquire() as conn:
|
||||
params: list = [chat_id]
|
||||
where = ""
|
||||
@@ -312,16 +327,29 @@ class Storage:
|
||||
last = rows[limit - 1]
|
||||
next_cursor = f"{chat_id}|{_now_iso(last['created_at'])}|{last['id']}"
|
||||
|
||||
if got_lock:
|
||||
version_after = await self._get_chat_version(chat_id)
|
||||
if got_lock and version_before == version_after:
|
||||
await self._cache.set_json(
|
||||
cache_key,
|
||||
{"items": items, "next_cursor": next_cursor},
|
||||
{
|
||||
"items": items,
|
||||
"next_cursor": next_cursor,
|
||||
"version": version_after,
|
||||
},
|
||||
)
|
||||
return items, next_cursor
|
||||
finally:
|
||||
if got_lock:
|
||||
await self._cache.unlock(lock_key)
|
||||
|
||||
async def _get_chat_version(self, chat_id: str) -> int:
|
||||
async with self._pool.acquire() as conn:
|
||||
row = await conn.fetchval(
|
||||
"SELECT version FROM chats WHERE id = $1",
|
||||
chat_id,
|
||||
)
|
||||
return int(row) if row is not None else 0
|
||||
|
||||
@staticmethod
|
||||
def _user_row(row) -> dict:
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user