# Notes:
# Why I deleted `balance roundrobin`:
#
# https://www.haproxy.com/documentation/haproxy-configuration-manual/latest/#4-balance:
# The load balancing algorithm of a backend is set to roundrobin when no other
# algorithm, mode nor option have been set. The algorithm may only be set once
# for each backend.
#
# ---
# Why I removed `option http-keep-alive`: This is on by default

# https://git.nadeko.net/Fijxu/docker-compose-configs/src/branch/master/invidious/invidious-all/haproxy.cfg

global
    # This because haproxy is dumb and likes to use the maxconnection (somaxconn) from the kernel and that is super mega huge making haproxy oom
    maxconn 8192

resolvers docker
    # To be able to use multiple replicas of invidious
    # using domains
    nameserver dns1 127.0.0.11:53

defaults
  mode http
  # https://www.haproxy.com/documentation/haproxy-configuration-tutorials/service-reliability/retries/
  # Use another server if one takes too long to reply
  retries 4
  option redispatch
  timeout client 60s
  timeout connect 5s
  # The default timeout of http request on invidious is 10 seconds.
  # If this is less than 10 seconds, the reverse proxy may return
  # "503 service unavailable", not letting invidious respond to 
  # the client.
  timeout server 60s
  timeout queue 30s
  # https://www.haproxy.com/documentation/haproxy-configuration-manual/latest/#4.2-http-reuse
  http-reuse safe

# Prometheus for graphs
frontend prometheus
    bind *:8404
    mode http
    http-request use-service prometheus-exporter if { path /metrics }
    no log

frontend invidious
    bind *:8001
	
	# Use captcha only for /watch path
    use_backend captcha if { path /watch } || { path /watch/ } || { path_beg /.well-known/.git.gammaspectra.live }
    default_backend invidious

backend captcha
    server srv1 captcha:4000 check resolvers docker init-addr libc,none

backend invidious
    server-template srv 4 invidious:3000 check resolvers docker init-addr libc,none

