Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure HAProxy to work with server sent events?

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?

like image 951
Josh Wilson Avatar asked Jan 29 '14 00:01

Josh Wilson


People also ask

How does HAProxy load balancing Work?

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.

What port does HAProxy listen on?

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.

What is HAProxy configuration file?

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.


1 Answers

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.

like image 86
Guillaume Avatar answered Sep 28 '22 04:09

Guillaume