Files
mtusi2026lab5/infra/haproxy/haproxy.cfg
T

77 lines
3.2 KiB
INI

# The single entry point: round-robin over the API replicas found through
# Docker DNS, with active readiness checks and no client stickiness.
global
log stdout format raw local0 info
maxconn 20000
# The container has at most one CPU; by default HAProxy starts a thread
# per host CPU and fights the quota.
nbthread 1
defaults
mode http
log global
option dontlognull
timeout connect 1s
timeout client 30s
# Above the API's 5 s request deadline: the API answers a slow request
# itself, this only cuts off a hung replica.
timeout server 7s
timeout http-request 10s
timeout http-keep-alive 10s
timeout check 2s
# Below the 500 ms latency objective: excess load gets a fast 503.
timeout queue 300ms
# Re-sent only when nothing reached the API: a timeout or an empty
# response may follow a committed write.
retries 2
option redispatch 1
retry-on conn-failure
# Errors produced by the balancer itself use the contract's envelope.
http-error status 502 content-type application/json lf-string '{"error":{"code":"unavailable","message":"the API replica failed to answer","request_id":"%[unique-id]"}}' hdr X-Request-Id "%[unique-id]"
http-error status 503 content-type application/json lf-string '{"error":{"code":"unavailable","message":"no API replica is available","request_id":"%[unique-id]"}}' hdr X-Request-Id "%[unique-id]"
http-error status 504 content-type application/json lf-string '{"error":{"code":"unavailable","message":"the API replica did not answer in time","request_id":"%[unique-id]"}}' hdr X-Request-Id "%[unique-id]"
# `api` resolves to every running replica, re-resolved every second; an
# address that disappears leaves at once.
resolvers docker
nameserver dns 127.0.0.11:53
resolve_retries 3
timeout resolve 1s
timeout retry 1s
hold valid 10s
hold obsolete 0s
accepted_payload_size 8192
frontend entry
bind :8080
option httplog
unique-id-format %[uuid(4),regsub(-,,g)]
# Replaces any client value; the API keeps it, so one id spans both logs.
http-request set-header X-Request-Id %[unique-id]
capture response header X-Instance-Id len 32
capture response header X-Request-Id len 40
http-after-response set-log-level silent if { status lt 500 } { env(LB_QUIET) -m str 1 }
default_backend replicas
backend replicas
# Binary handlers have a 30s budget, ordinary API handlers retain 5s.
http-request set-timeout server 35s if { path_reg ^/api/v1/chats/[^/]+/attachments$|^/api/v1/attachments/[^/]+/content$ }
balance roundrobin
option httpchk
http-check send meth GET uri /health/ready
http-check expect status 200
# Out within 1.5 s (inter + fastinter), less than the API's 2 s drain delay.
# maxconn 20: the replica's 10 pooled database connections plus requests
# that need no database; the rest waits in the queue.
default-server maxconn "${API_MAXCONN-20}" inter 1s fastinter 500ms downinter 1s fall 2 rise 2 check resolvers docker init-addr none resolve-prefer ipv4
# Up to five replicas, doubled while docker rollout replaces them.
server-template api 10 api:8080
listen stats
bind :8404
no log
stats enable
stats uri /
stats refresh 2s