# HAProxy in front of the Hour Advantage instances of the compose stack. # # Discovery: the compose service name "gateway" resolves, through Docker's # embedded DNS, to every running replica. server-template reserves 16 slots # and fills them from that answer, re-asked every few seconds, so replicas # added with --scale appear and removed ones disappear without a reload. # 16 slots hold 15 replicas, and the one more that rolling-update.sh starts # beside them; raise the number for more. # # Ports: exact ports, a frontend and backend for each (fe_http_, # be_http_), as `hourctl export lb haproxy` prints them. # Exposure is deploy config, listeners are runtime config: an endpoint added # on an existing port (by path) needs nothing here; a listener on a new port # needs a frontend and backend here, a reload, and the port in # docker-compose.yml. The backend names start with be_http_: the rolling # update scripts count the servers UP in them. # # Health: each slot is checked on the instance's admin port (9090) with # GET /api/ready. An instance that is draining for shutdown answers 503 and # is taken out of rotation (after 3 checks, about 4 s; it drains for 6). # Readiness does not depend on Postgres: a slow or lost database would fail # every instance's check at once, leaving no server here and turning every clock away. While the # database is down, the gateways answer 503 to the messages that need it, # and clocks send them again. # # Balancing: round robin. A clock's requests go to every instance in turn; # all of them share Postgres, so any instance handles any request, and the # live views (`hourctl wire`, `hourctl stream`) gather what every instance # sees. Hashing on the serial number is an optional optimisation only (each # instance then keeps fewer clocks in its cache); see be_http_8080. global log stdout format raw local0 info maxconn 4096 # Runtime API for "show servers state", "show stat" etc.: # docker compose exec haproxy sh -c 'echo "show servers state" | nc 127.0.0.1 9999' stats socket ipv4@127.0.0.1:9999 level admin stats timeout 10s resolvers docker nameserver dns1 127.0.0.11:53 # Docker returns every replica's address; accept a large answer. accepted_payload_size 8192 resolve_retries 3 timeout resolve 1s timeout retry 1s # Re-ask often, and forget an address that left the answer quickly, so # scale-down and replaced containers show within a few seconds. hold valid 5s hold obsolete 5s hold other 10s hold refused 10s hold nx 5s hold timeout 10s defaults mode http log global option dontlognull # Every request gets an id, sent to the gateway as X-Request-Id (which # puts it on its own log lines about the request and sends it back) and # logged here as the last field, so a request can be followed from this # log to the gateway's. The log is httplog's, but with the path only: # clocks put their key in the query string (ComKey=...). unique-id-format %{+X}o\ %ci:%cp_%fi:%fp_%Ts_%rt log-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %{+Q}HM %{+Q}HPO %ID" # Clocks send short requests and send them again when they get no # answer, so fail fast rather than queue long. timeout connect 1s timeout client 30s timeout http-request 10s timeout http-keep-alive 5s timeout server 20s timeout queue 2s timeout check 2s # A connection that fails, or a server that closes without answering # (an instance that was killed), is tried again on another instance. # The gateway stores an upload before answering and events carry stable # ids, so a retried upload at worst arrives twice and is deduplicated. retries 2 option redispatch 1 retry-on conn-failure empty-response # Wait for the (small) request body, so a request can be retried and a # slow sender does not hold a gateway connection. option http-buffer-request # ------------------------------------------------------------------ devices # One frontend and backend per device port. To add a port: copy both, # change the port everywhere in them, add the port to docker-compose.yml, # and recreate HAProxy (`docker compose up -d haproxy`: the new port has to # be published). A change to existing ports only needs a hitless reload: # `docker compose kill -s USR2 haproxy`. frontend fe_http_8080 bind :8080 maxconn 2000 # The gateway trusts X-Forwarded-For only from its trusted_proxies; # drop whatever a client sent so the only value is the one added here. http-request del-header X-Forwarded-For option forwardfor http-request set-header X-Request-Id %[unique-id] default_backend be_http_8080 backend be_http_8080 balance roundrobin # Optional: keep each clock on one instance (fewer clocks in each # instance's cache; only the clocks of an instance that comes or goes # move). Nothing depends on it. # balance url_param SN # hash-type consistent option httpchk http-check send meth GET uri /api/ready ver HTTP/1.1 hdr Host localhost http-check expect status 200 # Which slot answered (gw1, gw2...), for following a request in tests. http-response set-header X-HourAdvantage-Instance %s # check port 9090: health comes from the admin API, not the device port. # # No "observe layer4 ... on-error mark-down" here: a dead instance is # caught by the checks (about 2 s), and until then retries/redispatch # move its requests to another one. default-server check port 9090 inter 2s fastinter 1s downinter 2s fall 3 rise 2 maxconn 200 resolvers docker resolve-prefer ipv4 init-addr none server-template gw 16 gateway:8080 # ------------------------------------------------------------------ stats frontend stats # Published on 127.0.0.1 only (see docker-compose.yml). bind :8404 # Container health check. monitor-uri /healthz stats enable stats uri /stats stats refresh 5s stats show-legends stats show-node http-request set-log-level silent