Run the API as replicas behind HAProxy
compose.yaml now runs one api service with API_REPLICAS replicas and no published ports, container name or hostname, behind lb, HAProxy 3.2.26 LTS pinned by digest and the only service with ports, bound to 127.0.0.1 (LB_HTTP_PORT for HTTP, LB_STATS_PORT for read-only stats). The balancer finds replicas through Docker DNS with a server template, checks GET /health/ready every second (fall 2, rise 2), balances round robin without stickiness, retries connection failures on another replica and allows a retry after a sent request only for GET and for message posts carrying Idempotency-Key, never for users, chats or login. Its own 502/503/504 answers use the contract error body with the forwarded X-Request-Id. The network is split into edge and an internal backend, every service gets CPU and memory limits, the API runs read-only with a tmpfs /tmp, two workers per replica, an 8 s request limit and a 5 s graceful stop inside an 8 s stop_grace_period. Only api builds the image, migrate reuses it once before the replicas start. PostgreSQL loads pg_stat_statements and allows 200 connections. The short TTL instance of lab 2 published the API around the balancer and is removed; its expiry check stays as an in-process test. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
7bf28fc6ed
commit
dc2015473f
@@ -9,7 +9,6 @@ from stand import (
|
||||
BASE_URL,
|
||||
BASIC,
|
||||
BEARER,
|
||||
SHORTTTL_URL,
|
||||
basic,
|
||||
bearer,
|
||||
compose,
|
||||
@@ -89,18 +88,6 @@ async def test_logout_then_token_is_dead(http: AsyncClient) -> None:
|
||||
assert again.headers["WWW-Authenticate"] == BEARER
|
||||
|
||||
|
||||
async def test_short_ttl_session_expires() -> None:
|
||||
async with AsyncClient(base_url=SHORTTTL_URL, timeout=10, trust_env=False) as short:
|
||||
_, token = await enroll(short, "expiry")
|
||||
fresh = await short.get("/api/v1/users/me", headers=bearer(token))
|
||||
await asyncio.sleep(3)
|
||||
stale = await short.get("/api/v1/users/me", headers=bearer(token))
|
||||
print(f"SESSION_TTL_SECONDS=2: сразу {fresh.status_code}, через 3 с {stale.status_code}")
|
||||
assert fresh.status_code == 200
|
||||
assert stale.status_code == 401
|
||||
assert stale.headers["WWW-Authenticate"] == BEARER
|
||||
|
||||
|
||||
async def test_valkey_serves_repeated_checks(http: AsyncClient) -> None:
|
||||
_, token = await enroll(http, "cache")
|
||||
await http.get("/api/v1/users/me", headers=bearer(token))
|
||||
|
||||
@@ -12,7 +12,6 @@ from httpx import AsyncClient, Response, TransportError
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
BASE_URL = os.environ.get("BASE_URL", "http://localhost:8080")
|
||||
SHORTTTL_URL = os.environ.get("SHORTTTL_URL", "http://localhost:8082")
|
||||
PASSWORD = "Пароль-для-стенда-01"
|
||||
BASIC = 'Basic realm="messenger", charset="UTF-8"'
|
||||
BEARER = 'Bearer realm="messenger"'
|
||||
@@ -43,7 +42,13 @@ def valkey(command: str) -> str:
|
||||
|
||||
def started_at(service: str) -> str:
|
||||
return subprocess.run(
|
||||
["docker", "inspect", "--format", "{{.State.StartedAt}}", compose("ps", "-q", service)],
|
||||
[
|
||||
"docker",
|
||||
"inspect",
|
||||
"--format",
|
||||
"{{.State.StartedAt}}",
|
||||
*compose("ps", "-q", service).split(),
|
||||
],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
|
||||
Reference in New Issue
Block a user