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:
Artyom
2026-10-28 11:43:43 +03:00
co-authored by Claude Opus 5.5
parent 7bf28fc6ed
commit dc2015473f
11 changed files with 219 additions and 102 deletions
+4 -4
View File
@@ -23,19 +23,19 @@ class MessengerWorker(UvicornWorker):
"access_log": False,
"server_header": False,
"date_header": False,
"timeout_graceful_shutdown": int(os.environ.get("SHUTDOWN_TIMEOUT_SECONDS", "10")),
"timeout_graceful_shutdown": int(os.environ.get("SHUTDOWN_TIMEOUT_SECONDS", "5")),
}
bind = f"{os.environ.get('HOST', '0.0.0.0')}:{os.environ.get('PORT', '8080')}"
workers = int(os.environ.get("WEB_CONCURRENCY", "4"))
workers = int(os.environ.get("WEB_CONCURRENCY", "2"))
worker_class = "messenger.gunicorn_conf.MessengerWorker"
logger_class = StructlogGunicornLogger
loglevel = LOG_LEVEL.lower()
accesslog = None
errorlog = "-"
timeout = int(float(os.environ.get("REQUEST_TIMEOUT_SECONDS", "15")) + 5)
graceful_timeout = int(os.environ.get("SHUTDOWN_TIMEOUT_SECONDS", "10"))
timeout = int(float(os.environ.get("REQUEST_TIMEOUT_SECONDS", "8")) + 5)
graceful_timeout = int(os.environ.get("SHUTDOWN_TIMEOUT_SECONDS", "5"))
keepalive = 5
preload_app = False
control_socket_disable = True
+2 -2
View File
@@ -6,5 +6,5 @@ from messenger.shared.config import Settings
class TransportSettings(Settings):
docs: bool = False
max_body_bytes: int = Field(default=1_048_576, ge=4_096)
request_timeout_seconds: float = Field(default=15.0, gt=0, le=300)
shutdown_timeout_seconds: int = Field(default=10, ge=1, le=10)
request_timeout_seconds: float = Field(default=8.0, gt=0, le=300)
shutdown_timeout_seconds: int = Field(default=5, ge=1, le=10)