Реализовать общую идемпотентность отправки сообщения

Ключ Idempotency-Key с TTL (минимум 86400 c), таблица message_requests и
advisory-лок на (пользователь, чат, ключ): конкурентные повторные запросы
возвращают одно и то же сообщение, конфликт по body -> 409. Добавлена
миграция 002 и сценарий проверки scripts/check_idempotency.py.
This commit is contained in:
2026-11-16 22:18:42 +00:00
parent 9698fae3be
commit 0e20efe781
7 changed files with 91 additions and 8 deletions
+6 -3
View File
@@ -7,8 +7,9 @@ from .security import Authentication
class Messenger:
def __init__(self, repository: Repository, session_ttl=3600):
def __init__(self, repository: Repository, session_ttl=3600, idempotency_ttl=86400):
self.repository = repository
self.idempotency_ttl = idempotency_ttl
self.pagination = Pagination(repository.cursor_key())
self.auth = Authentication(repository, session_ttl)
@@ -35,10 +36,12 @@ class Messenger:
def chat(self, actor, chat_id):
return self.repository.chat(actor, uuid_value(chat_id))
def send(self, actor, chat_id, body):
def send(self, actor, chat_id, body, key):
self.chat(actor, chat_id)
if not isinstance(key, str) or not re.fullmatch(r'[A-Za-z0-9._:-]{1,128}', key):
invalid('Valid Idempotency-Key required')
fields(body, ('text',))
return self.repository.create_message(actor, uuid_value(chat_id), string(body['text'], 4000))
return self.repository.create_message(actor, uuid_value(chat_id), string(body['text'], 4000), key, self.idempotency_ttl)
def chats(self, actor, limit, cursor):
scope = 'chats:' + actor