Реализовать общую идемпотентность отправки сообщения
Ключ Idempotency-Key с TTL (минимум 86400 c), таблица message_requests и advisory-лок на (пользователь, чат, ключ): конкурентные повторные запросы возвращают одно и то же сообщение, конфликт по body -> 409. Добавлена миграция 002 и сценарий проверки scripts/check_idempotency.py.
This commit is contained in:
+3
-2
@@ -15,7 +15,7 @@ def create_app(repository=None, settings=None):
|
|||||||
app.json.ensure_ascii = False
|
app.json.ensure_ascii = False
|
||||||
settings = settings or Settings.from_env()
|
settings = settings or Settings.from_env()
|
||||||
repository = repository if repository is not None else PostgresRepository(Database(settings.database_url))
|
repository = repository if repository is not None else PostgresRepository(Database(settings.database_url))
|
||||||
service = Messenger(repository, settings.session_ttl)
|
service = Messenger(repository, settings.session_ttl, settings.idempotency_ttl)
|
||||||
instance_id = str(uuid4())
|
instance_id = str(uuid4())
|
||||||
|
|
||||||
@app.before_request
|
@app.before_request
|
||||||
@@ -112,7 +112,8 @@ def create_app(repository=None, settings=None):
|
|||||||
@app.route('/api/v1/chats/<chat_id>/messages', methods=['GET', 'POST'])
|
@app.route('/api/v1/chats/<chat_id>/messages', methods=['GET', 'POST'])
|
||||||
def messages(chat_id):
|
def messages(chat_id):
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
return jsonify(service.send(g.user['id'], chat_id, body())), 201
|
service.chat(g.user['id'], chat_id)
|
||||||
|
return jsonify(service.send(g.user['id'], chat_id, body(), request.headers.get('Idempotency-Key'))), 201
|
||||||
return jsonify(service.history(g.user['id'], chat_id, *paging()))
|
return jsonify(service.history(g.user['id'], chat_id, *paging()))
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|||||||
+5
-1
@@ -11,6 +11,7 @@ class Settings:
|
|||||||
port: int = 8080
|
port: int = 8080
|
||||||
environment: str = 'lab'
|
environment: str = 'lab'
|
||||||
session_ttl: int = 3600
|
session_ttl: int = 3600
|
||||||
|
idempotency_ttl: int = 86400
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_env(cls):
|
def from_env(cls):
|
||||||
@@ -22,7 +23,10 @@ class Settings:
|
|||||||
raise ValueError('This lab uses SESSION_STORE=postgres')
|
raise ValueError('This lab uses SESSION_STORE=postgres')
|
||||||
port = int(os.getenv('APP_PORT', '8080'))
|
port = int(os.getenv('APP_PORT', '8080'))
|
||||||
ttl = int(os.getenv('SESSION_TTL_SECONDS', '3600'))
|
ttl = int(os.getenv('SESSION_TTL_SECONDS', '3600'))
|
||||||
|
idempotency_ttl = int(os.getenv('IDEMPOTENCY_TTL_SECONDS', '86400'))
|
||||||
|
if idempotency_ttl < 86400:
|
||||||
|
raise ValueError('IDEMPOTENCY_TTL_SECONDS must be at least 86400')
|
||||||
if not 1 <= port <= 65535 or ttl <= 0:
|
if not 1 <= port <= 65535 or ttl <= 0:
|
||||||
raise ValueError('Invalid APP_PORT or SESSION_TTL_SECONDS')
|
raise ValueError('Invalid APP_PORT or SESSION_TTL_SECONDS')
|
||||||
return cls(url, os.getenv('APP_HOST', '0.0.0.0'), port,
|
return cls(url, os.getenv('APP_HOST', '0.0.0.0'), port,
|
||||||
os.getenv('APP_ENV', 'lab'), ttl)
|
os.getenv('APP_ENV', 'lab'), ttl, idempotency_ttl)
|
||||||
|
|||||||
+22
-1
@@ -1,5 +1,7 @@
|
|||||||
"""PostgreSQL repository: all SQL values are bound parameters."""
|
"""PostgreSQL repository: all SQL values are bound parameters."""
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
from uuid import UUID, uuid4
|
from uuid import UUID, uuid4
|
||||||
import psycopg
|
import psycopg
|
||||||
from .domain import ApiError, invalid
|
from .domain import ApiError, invalid
|
||||||
@@ -112,11 +114,30 @@ class PostgresRepository:
|
|||||||
' ORDER BY c.created_at,c.id LIMIT %s', params).fetchall()
|
' ORDER BY c.created_at,c.id LIMIT %s', params).fetchall()
|
||||||
return [serialize(r) for r in rows]
|
return [serialize(r) for r in rows]
|
||||||
|
|
||||||
def create_message(self, actor, chat_id, text):
|
def create_message(self, actor, chat_id, text, key, ttl):
|
||||||
|
# The operation is POST /chats/{id}/messages only. Method is implicit in
|
||||||
|
# this dedicated table. A transaction lock serializes this exact scope
|
||||||
|
# across ALL processes; a hash collision only adds harmless contention.
|
||||||
|
scope = json.dumps([actor, chat_id, key], separators=(',', ':')).encode()
|
||||||
|
lock_id = int.from_bytes(hashlib.sha256(scope).digest()[:8], 'big', signed=True)
|
||||||
with self.db.transaction() as conn:
|
with self.db.transaction() as conn:
|
||||||
self._access(conn, actor, chat_id)
|
self._access(conn, actor, chat_id)
|
||||||
|
conn.execute('SELECT pg_advisory_xact_lock(%s)', (lock_id,))
|
||||||
|
previous = conn.execute("""SELECT r.request_text, m.* FROM message_requests r
|
||||||
|
JOIN messages m ON m.id=r.message_id
|
||||||
|
WHERE r.user_id=%s AND r.chat_id=%s AND r.idempotency_key=%s
|
||||||
|
AND r.expires_at>clock_timestamp()""", (actor, chat_id, key)).fetchone()
|
||||||
|
if previous:
|
||||||
|
if previous.pop('request_text') != text:
|
||||||
|
raise ApiError(409, 'conflict', 'Idempotency key already used with another message')
|
||||||
|
return serialize(previous)
|
||||||
|
conn.execute('DELETE FROM message_requests WHERE user_id=%s AND chat_id=%s AND idempotency_key=%s',
|
||||||
|
(actor, chat_id, key))
|
||||||
row = conn.execute('INSERT INTO messages(id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s) RETURNING *',
|
row = conn.execute('INSERT INTO messages(id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s) RETURNING *',
|
||||||
(uuid4(), chat_id, actor, text)).fetchone()
|
(uuid4(), chat_id, actor, text)).fetchone()
|
||||||
|
conn.execute("""INSERT INTO message_requests(user_id,chat_id,idempotency_key,request_text,message_id,expires_at)
|
||||||
|
VALUES (%s,%s,%s,%s,%s,clock_timestamp() + %s * interval '1 second')""",
|
||||||
|
(actor, chat_id, key, text, row['id'], ttl))
|
||||||
return serialize(row)
|
return serialize(row)
|
||||||
|
|
||||||
def messages(self, actor, chat_id, limit, after):
|
def messages(self, actor, chat_id, limit, after):
|
||||||
|
|||||||
@@ -13,5 +13,5 @@ class Repository(Protocol):
|
|||||||
def create_chat(self, actor, title, members): ...
|
def create_chat(self, actor, title, members): ...
|
||||||
def chat(self, actor, chat_id): ...
|
def chat(self, actor, chat_id): ...
|
||||||
def chats(self, actor, limit, after): ...
|
def chats(self, actor, limit, after): ...
|
||||||
def create_message(self, actor, chat_id, text): ...
|
def create_message(self, actor, chat_id, text, key, ttl): ...
|
||||||
def messages(self, actor, chat_id, limit, after): ...
|
def messages(self, actor, chat_id, limit, after): ...
|
||||||
|
|||||||
@@ -7,8 +7,9 @@ from .security import Authentication
|
|||||||
|
|
||||||
|
|
||||||
class Messenger:
|
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.repository = repository
|
||||||
|
self.idempotency_ttl = idempotency_ttl
|
||||||
self.pagination = Pagination(repository.cursor_key())
|
self.pagination = Pagination(repository.cursor_key())
|
||||||
self.auth = Authentication(repository, session_ttl)
|
self.auth = Authentication(repository, session_ttl)
|
||||||
|
|
||||||
@@ -35,10 +36,12 @@ class Messenger:
|
|||||||
def chat(self, actor, chat_id):
|
def chat(self, actor, chat_id):
|
||||||
return self.repository.chat(actor, uuid_value(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)
|
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',))
|
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):
|
def chats(self, actor, limit, cursor):
|
||||||
scope = 'chats:' + actor
|
scope = 'chats:' + actor
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
CREATE TABLE message_requests (
|
||||||
|
user_id uuid NOT NULL REFERENCES users(id),
|
||||||
|
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||||
|
idempotency_key text NOT NULL CHECK (idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||||
|
request_text text NOT NULL,
|
||||||
|
message_id uuid NOT NULL REFERENCES messages(id),
|
||||||
|
expires_at timestamptz NOT NULL,
|
||||||
|
PRIMARY KEY (user_id, chat_id, idempotency_key)
|
||||||
|
);
|
||||||
|
CREATE INDEX message_requests_expiry_idx ON message_requests(expires_at);
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
"""Concurrent HTTP contract check through LB, using one session on two replicas."""
|
||||||
|
import argparse
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
from pathlib import Path
|
||||||
|
import sys
|
||||||
|
from uuid import uuid4
|
||||||
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||||
|
from client import Client, check
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--base-url', default='http://localhost:8080')
|
||||||
|
args = parser.parse_args()
|
||||||
|
c = Client(args.base_url)
|
||||||
|
alice, bob, eve, chat = c.bootstrap()
|
||||||
|
key = uuid4().hex
|
||||||
|
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
||||||
|
def send(_):
|
||||||
|
return Client(args.base_url).message(alice, chat, 'Одно сообщение 👋', key=key)
|
||||||
|
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||||
|
responses = list(pool.map(send, range(24)))
|
||||||
|
first = responses[0]['json']
|
||||||
|
check(all(r['json'] == first and r['status'] == 201 for r in responses), 'Concurrent replay differs')
|
||||||
|
instances = {r['headers']['X-Instance-Id'] for r in responses}
|
||||||
|
check(len(instances) >= 2, 'Concurrent requests did not reach both replicas')
|
||||||
|
page = c.call('GET', path, actor=bob, schema='MessagePage')['json']
|
||||||
|
check(page['items'] == [first], 'Concurrent repeat created duplicate messages')
|
||||||
|
c.call('POST', path, 409, actor=alice, body={'text': 'Different'}, headers={'Idempotency-Key': key})
|
||||||
|
c.call('POST', path, 400, actor=alice, body={'text': 'Missing key'})
|
||||||
|
c.call('POST', path, 400, actor=alice, body={'text': 'Bad key'}, headers={'Idempotency-Key': 'bad key'})
|
||||||
|
c.call('POST', path, 404, actor=eve, body={'text': 'Одно сообщение 👋'}, headers={'Idempotency-Key': key})
|
||||||
|
other = c.message(bob, chat, 'Same key, different user', key=key)['json']
|
||||||
|
check(other['id'] != first['id'], 'User scope is not independent')
|
||||||
|
second_chat = c.call('POST', '/api/v1/chats', 201, actor=alice,
|
||||||
|
body={'title': 'Second', 'member_ids': [alice['id']]}, schema='Chat')['json']
|
||||||
|
check(c.message(alice, second_chat, 'Other chat', key=key)['json']['id'] != first['id'], 'Chat scope is not independent')
|
||||||
|
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
|
||||||
|
c.call('POST', path, 401, actor=alice, body={'text': 'Одно сообщение 👋'}, headers={'Idempotency-Key': key})
|
||||||
|
print('PASS: 24 concurrent requests / 8 clients / %d replicas -> one message; conflict, scopes, access and logout verified.' % len(instances))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user