- compose: один масштабируемый сервис api (deploy.replicas=2, без ports и container_name), HAProxy 3.2 LTS по digest — единственная точка входа (127.0.0.1), разовый migrate до старта реплик, CPU/RAM-лимиты у всех сервисов, каждому сервису только его secrets. - HAProxy: server-template + Docker DNS (re-resolve 1 с), активная проверка GET /health/ready каждую 1 с (fall 2 / rise 2), без backup-серверов, retry только conn-failure, ошибки самого LB — в формате контракта с X-Request-Id. - API: X-Instance-Id = hostname контейнера на каждом ответе (и на 500/503); Idempotency-Key обязателен; ключ, сообщение и результат пишутся в ОДНОЙ транзакции (миграция 0002), конкурентный повтор ждёт уникальный индекс; другой body — 409, посторонний — всегда 404; attachment_ids=[] равен отсутствию поля; дубли ключей JSON и тело у POST /auth/sessions — 400. - SIGTERM: readiness 503 + DRAIN_SECONDS=3 обслуживания, затем graceful stop. - scripts: stop-one.sh (+ .cmd для Windows), restore-api.sh, restart.sh перезапускает и LB, lbctl.py — состояние бэкендов HAProxy -> контейнеры. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
26 lines
1.6 KiB
SQL
26 lines
1.6 KiB
SQL
-- 0002 (lab 3): shared idempotency of POST /chats/{chat_id}/messages.
|
|
-- Additive only (new table), so the lab 2 API keeps working while this runs: it is safe
|
|
-- to apply before a rolling update of the API replicas (see REPORT.md, "Миграции").
|
|
--
|
|
-- Scope of a key (contracts/README.md): current user + HTTP method + chat path + key.
|
|
-- The method (POST) and the path template are fixed for this table, so the scope
|
|
-- is (user_id, chat_id, key). The key row, the message and the result are written in
|
|
-- ONE transaction (repo.post_message): there is no "pending" state that a crash could
|
|
-- leave behind, and the stored result is the message row itself.
|
|
|
|
CREATE TABLE idempotency_keys (
|
|
user_id uuid NOT NULL REFERENCES users (id),
|
|
chat_id uuid NOT NULL REFERENCES chats (id),
|
|
key text NOT NULL,
|
|
request_hash bytea NOT NULL, -- SHA-256 of the canonical JSON body
|
|
-- Inserted before the message in the same transaction, hence checked at COMMIT.
|
|
message_id uuid NOT NULL REFERENCES messages (id) DEFERRABLE INITIALLY DEFERRED,
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
PRIMARY KEY (user_id, chat_id, key),
|
|
CONSTRAINT idempotency_keys_key_format CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
|
CONSTRAINT idempotency_keys_hash_len CHECK (octet_length(request_hash) = 32),
|
|
CONSTRAINT idempotency_keys_message_key UNIQUE (message_id)
|
|
);
|
|
-- Housekeeping of keys older than IDEMPOTENCY_TTL_SECONDS (>= 24 h).
|
|
CREATE INDEX idempotency_keys_created_at ON idempotency_keys (created_at);
|