I'm trying to add an endpoint to an existing application that sends Server Sent Events. There often may be no event for ~5 minutes. I'm hoping to configure that endpoint to not cut off my server even when the response has not been completed in ~1min, but all other endpoints to timeout if the server fails to respond.
Is there an easy way to support server sent events in HAProxy?
HAProxy Algorithms It works by using each server behind the load balancer in turns, according to their weights. It's also probably the smoothest and most fair algorithm as the servers' processing time stays equally distributed. As a dynamic algorithm, Round Robin allows server weights to be adjusted on the go.
This is the IP address that HAProxy listens on, which is normally the localhost specified by IP address: 127.0. 0.1. This is the port that HAProxy listens on, which is normally 85.
The /etc/haproxy/haproxy. cfg configuration file is divided into the following sections: global. Defines global settings such as the syslog facility and level to use for logging, the maximum number of concurrent connections allowed, and how many processes to start in daemon mode.
Here is my suggestion for HAProxy and SSE: you have plenty of custom timeout options in HAProxy, and there is 2 interesting options for you.
The timeout tunnel
specifies timeout for tunnel connection - used for Websockets, SSE or CONNECT. Bypass both server and client timeout.
The timeout client
handles the situation where a client looses their connection (network loss, disappear before the ACK of ending session, etc...)
In your haproxy.cfg
, this is what you should do, first in your defaults
section :
# Set the max time to wait for a connection attempt to a server to succeed
timeout connect 30s
# Set the max allowed time to wait for a complete HTTP request
timeout client 50s
# Set the maximum inactivity time on the server side
timeout server 50s
Nothing special until there.
Now, still in the defaults
section :
# handle the situation where a client suddenly disappears from the net
timeout client-fin 30s
Next, jump to your backend
definition and add this:
timeout tunnel 10h
I suggest a high value, 10 hours seems ok.
You should also avoid using the default http-keep-alive
option, SSE does not use it. Instead, use http-server-close
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With