Files
2026-11-10 19:24:33 +03:00

93 lines
4.4 KiB
INI

# 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
# Body arrival for a 10 MiB attachment; JSON posts finish well under this.
timeout http-request 30s
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
# Attachment uploads are raw bytes, up to 10 MiB. A larger Content-Length is
# rejected here, before the body is read or an API replica is contacted.
# The body matches the API error envelope; request_id echoes a safe client
# X-Request-Id and otherwise stays "lb", like the other gateway errors.
acl upload_too_large req.hdr_val(content-length) gt 10485760
acl safe_request_id req.hdr(X-Request-Id) -m reg ^[A-Za-z0-9._:-]{1,128}$
http-request set-var(txn.reqid) req.hdr(X-Request-Id) if upload_too_large safe_request_id
http-request set-var(txn.reqid) str(lb) if upload_too_large !safe_request_id
http-request return status 413 content-type application/json lf-string "{\"error\":{\"code\":\"payload_too_large\",\"message\":\"Файл больше 10 МиБ\",\"request_id\":\"%[var(txn.reqid)]\"}}" hdr X-Request-Id %[var(txn.reqid)] hdr X-Instance-Id %[str(lb)] if upload_too_large
# 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.
# Attachment PUT to S3 can outlast the default 10 s server timeout under concurrent load.
acl attachment_upload method POST
acl attachment_upload_path path_reg ^/api/v1/chats/[^/]+/attachments$
http-request set-timeout server 30s if attachment_upload attachment_upload_path
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