level 4, changed to HAProxy

This commit is contained in:
2026-11-07 18:18:43 +03:00
parent 124c83e450
commit eb4e00e412
33 changed files with 3932 additions and 231 deletions
+79
View File
@@ -0,0 +1,79 @@
# Client -> HAProxy -> api x N. Replicas are discovered through Docker DNS and checked actively.
#
# Timing contract (keep in sync with compose.yaml and app settings):
# check period inter 1s, timeout check 1s
# marked DOWN after fall 2 failed checks -> hung/draining replica leaves the pool in <= 3 s
# marked UP after rise 2 good checks -> new replica gets traffic ~2 s after it is ready
# DNS refresh every 1 s (hold valid 1s); a removed container disappears within ~1-2 s
# crashed process connect refused -> request redispatched to another replica at once
# app DRAIN_SECONDS=4 > 3 s, so a replica receiving SIGTERM is out of rotation before it closes its socket.
global
log stdout format raw local0 info
maxconn 2048
# Runtime API for evidence (scripts/lb-status.sh); loopback inside the container only.
stats socket ipv4@127.0.0.1:9999 level operator
resolvers docker
nameserver dns 127.0.0.11:53
accepted_payload_size 8192
resolve_retries 3
timeout resolve 1s
timeout retry 1s
hold valid 1s
hold other 1s
hold refused 1s
hold nx 1s
hold timeout 1s
hold obsolete 0s
defaults
mode http
log global
option httplog
option dontlognull
option forwardfor
timeout connect 1s
timeout client 30s
timeout server 10s
timeout http-request 10s
timeout http-keep-alive 5s
timeout queue 2s
timeout check 1s
# A request whose connection to a replica failed is sent to another replica.
retries 2
option redispatch 1
frontend public
bind :8080
monitor-uri /lb/health
http-request deny deny_status 413 if { req.hdr_val(content-length) gt 1048576 }
# Message POST carries a mandatory Idempotency-Key, so replaying it on another replica is safe.
# Other POSTs (users, chats, sessions) are retried only when the connection was never established.
acl message_post method POST
acl message_post_path path_reg ^/api/v1/chats/[^/]+/messages$
http-request disable-l7-retry if message_post !message_post_path
default_backend api
backend api
# Round robin, no stickiness: any replica serves any session from PostgreSQL.
balance roundrobin
option httpchk
http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host lb
http-check expect status 200
retry-on conn-failure empty-response response-timeout
http-error status 503 content-type application/json string "{\"error\":{\"code\":\"unavailable\",\"message\":\"No API replica is available\",\"request_id\":\"lb\"}}"
http-error status 502 content-type application/json string "{\"error\":{\"code\":\"internal_error\",\"message\":\"Bad gateway\",\"request_id\":\"lb\"}}"
http-error status 504 content-type application/json string "{\"error\":{\"code\":\"internal_error\",\"message\":\"Gateway timeout\",\"request_id\":\"lb\"}}"
default-server check inter 1s fall 2 rise 2 resolvers docker resolve-prefer ipv4 init-addr none
server-template api 1-10 api:8080
# Read-only stats for evidence; not published, reachable only inside the container.
frontend stats
bind 127.0.0.1:8404
stats enable
stats uri /stats
stats refresh 5s
http-request set-log-level silent
-66
View File
@@ -1,66 +0,0 @@
worker_processes auto;
pid /tmp/nginx.pid;
events {
worker_connections 1024;
}
http {
access_log /dev/stdout;
error_log /dev/stderr warn;
# Docker embedded DNS. "api" resolves to every running replica of the Compose service;
# with "resolve" below nginx re-reads the set every 2 s, so stopped/scaled replicas leave and join the pool.
resolver 127.0.0.11 valid=2s ipv6=off;
resolver_timeout 2s;
log_format upstream '$remote_addr "$request" $status upstream=$upstream_addr '
'upstream_status=$upstream_status rt=$request_time';
upstream api {
zone api_upstream 64k;
# Round robin, no ip_hash/sticky: any replica serves any session from PostgreSQL.
server api:8080 resolve max_fails=1 fail_timeout=5s;
keepalive 32;
# Shorter than uvicorn's keep-alive (5 s), so nginx never reuses a connection the API is closing.
keepalive_timeout 4s;
}
server {
listen 8080;
access_log /dev/stdout upstream;
client_max_body_size 1m;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 1s;
proxy_send_timeout 10s;
proxy_read_timeout 10s;
# Not-yet-sent requests and idempotent methods go to the next replica on a dead backend.
# Non-idempotent POSTs (users, chats, sessions) are never replayed by the LB.
proxy_next_upstream error timeout http_502 http_504;
proxy_next_upstream_tries 3;
proxy_next_upstream_timeout 5s;
location = /lb/health {
access_log off;
default_type application/json;
return 200 '{"status":"ok"}';
}
# Message POST carries a mandatory Idempotency-Key, so replaying it on another replica is safe.
location ~ ^/api/v1/chats/[^/]+/messages$ {
proxy_next_upstream error timeout http_502 http_504 non_idempotent;
proxy_pass http://api;
}
location / {
proxy_pass http://api;
}
}
}