feat: put API replicas behind HAProxy

HAProxy 3.4 is the only published entry point; api runs as
API_REPLICAS replicas without ports, found through Docker DNS and
checked on /health/ready. Retries only on connection failure, LB errors
in the contract error format, resource limits and per-service secrets.
Adds scripts/stop-one.sh, LB-aware restart.sh, make replicas /
instances / lb_stats and replica tests driven by EXPECT_REPLICAS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-10 18:05:05 +03:00
co-authored by Claude Opus 5.5
parent 8e15e02898
commit ab71c3b51e
31 changed files with 1295 additions and 240 deletions
+40
View File
@@ -3,6 +3,8 @@
Сценарии, которым нужен особый стенд (сбой записи, остановка БД, короткий
срок ключа), — в `test_failures.py`; гонка одного ключа — в
`test_race.py`; повтор после рестарта API — исключение с evidence.
Повтору через другую реплику нужны ≥ 2 реплики за `BASE_URL` (фикстура
`replicas`), против одного процесса он пропускается.
"""
from typing import Any
@@ -20,7 +22,9 @@ from api_client import (
from contract import (
BEARER_CHALLENGE,
assert_error,
assert_instance_id,
assert_ok,
assert_replicas,
assert_unauthorized,
)
@@ -273,3 +277,39 @@ def test_invalid_request_keeps_key_free(
assert_error(resp, 400)
message = api.send_message(alice, chat["id"], "теперь валидно", key=key)
assert texts(api, alice, chat["id"]) == [message["text"]]
@pytest.mark.spec("idempotency", "Повтор через другую реплику")
def test_replay_on_other_replica(
api: Api,
alice: Actor,
chat: dict[str, Any],
replicas: int,
) -> None:
"""Повтор с тем же ключом, который обслужила другая реплика, — тот же
`Message`; другое тело с этим ключом — 409 от разных реплик."""
key = new_key()
body = {"text": "повтор через реплики"}
resp = api.post_message(alice, chat["id"], key=key, json=body)
first = assert_ok(resp, 201, "Message")
origin = assert_instance_id(resp)
replays: dict[str, Any] = {}
for _ in range(2 * replicas):
resp = api.post_message(alice, chat["id"], key=key, json=body)
replays[assert_instance_id(resp)] = assert_ok(resp, 201, "Message")
if replays.keys() - {origin}:
break
print(f"sent via {origin}, replayed via {sorted(replays)}")
# повтор через другую реплику: среди отправки и повторов ≥ 2 значения
assert_replicas({origin, *replays}, replicas)
assert all(message == first for message in replays.values())
conflicts = set()
for _ in range(2 * replicas):
resp = api.post_message(alice, chat["id"], key=key, json={"text": "?"})
assert_error(resp, 409)
conflicts.add(assert_instance_id(resp))
print(f"409 via {sorted(conflicts)}")
assert_replicas(conflicts, replicas)
assert texts(api, alice, chat["id"]) == [body["text"]]