Hosting Microsites Behind HAProxy
After coding my shiny new microsites I installed HAProxy on my setup box to test the behaviour. It quickly became apparent that the WebSocket connection between my clients and servers were reconnecting every 50 seconds.
After a huge amount of wasted time debugging them it occurred to me that the problem might be the proxy. After inspecting the very detailed, but largely impenetrable documentation on HAProxy I had a eureka moment when I noticed the timeout for clients/servers was 50 seconds.
More googling and inspection of the library revealed the solution wasn’t to extend that timeout, but to add two options not present in the default file:
- timeout tunnel — the timeout to use with WebSocket and CONNECT
- timeout client-fin — The timeout used when a WebSocket client connection is terminated at one end.
My new “HAProxy.config” has the following:
defaults
…
timeout connect 5s # The default
timeout client 50s # The default
timeout server 50s # The default
timeout tunnel 1h
timeout client-fin 5s
This cleared up the problem. Obviously I will still need to reconnect every hour, but my clients and servers need to be robust to outages, so this seemed like a virtuous number.
Good luck with your coding.