forked from mtusi2026/mtusi2026lab3
FastAPI + PostgreSQL как единый источник истины: пользователи, Basic/Bearer-сессии, чаты, сообщения. Сессии и идемпотентные ключи хранятся в БД, поэтому реплики взаимозаменяемы. Общая идемпотентность POST сообщения по Idempotency-Key, включая конкурентный повтор через разные реплики. compose.yaml: postgres, one-shot migrate, api1, api2 и nginx как единственная публичная точка входа. Добавлены scripts/stop-one.sh и scripts/restart.sh, student-tests, evidence и заполненный REPORT.md.
41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""One session, many requests, both replicas: proves shared session state."""
|
|
import json
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
|
|
from client import Client, LAB, check, run # noqa: E402
|
|
|
|
|
|
def main():
|
|
check(LAB >= 3, "shared sessions across replicas require lab3")
|
|
base = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080"
|
|
client = Client(base)
|
|
alice = client.user("replica")
|
|
|
|
# A just-restored replica can stay out of the nginx pool for up to
|
|
# fail_timeout=5s. Wait for normal two-replica operation before measuring.
|
|
deadline = time.monotonic() + 10
|
|
warm = set()
|
|
while time.monotonic() < deadline and len(warm) < 2:
|
|
warm.add(client.call("GET", "/health/live", schema="Health")["headers"]["X-Instance-Id"])
|
|
time.sleep(0.1)
|
|
check(len(warm) >= 2, "load balancer currently has only one healthy replica")
|
|
|
|
instances = []
|
|
for _ in range(12):
|
|
response = client.call("GET", "/api/v1/users/me", actor=alice, schema="User")
|
|
instances.append(response["headers"]["X-Instance-Id"])
|
|
|
|
check(len(set(instances)) >= 2, "one session was served by a single replica only")
|
|
print(json.dumps({"requests": len(instances), "instances": instances}, ensure_ascii=False))
|
|
print("PASS: one bearer session served by both replicas without re-login")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|