{{- $servers := split (getenv "SERVERS") "," -}}
{{- $ports := split (getenv "PORTS") "," -}}

error_log stderr notice;

worker_processes auto;
events {
  multi_accept on;
  use epoll;
  worker_connections 1024;
}

stream {
  {{- range $port := $ports }}
  upstream master_nodes_{{ $port }} {
    {{- range $server := $servers }}
    server {{ $server }}:{{ $port }} max_fails=1 fail_timeout=10s;
    {{- end }}
  }

  server {
    listen        {{ $port }};
    proxy_pass    master_nodes_{{ $port }};
    proxy_timeout 600;
    proxy_connect_timeout 2s;
  }
  {{- end }}
}