Files

80 lines
3.2 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
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