In HAproxy my websocket connection close after 50 seconds. How to change it?
Asked Answered
F

2

6

I'm using Haproxy with tornado for websocket. My connection working fine if i directly connect to tornado but if i use HAproxy with below config then connection closed after 50 seconds. My Haproxy config file is below.

global
        daemon
        maxconn 4032
        pidfile /var/run/haproxy.pid

    defaults
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms
        option  http-server-close
        maxconn 4032

    frontend http-in
        bind *:80
        acl is_websocket hdr_end(host) -i WebSocket
        use_backend servers if is_websocket
        default_backend servers
        option  redispatch
        option  http-server-close
        maxconn  2000
        contimeout  500000
        clitimeout  500000
        srvtimeout  500000
        contimeout  500000        
        timeout contimeout  500000 
        timeout connect  500000

    backend servers
        server server1 127.0.0.1:8886 maxconn 4032

Now by using above config my websocket connection automatically lost after 50 seconds. I want to do persistent connection so is there is any way to make connection persistent in HAproxy ?

Flowerage answered 26/3, 2013 at 9:5 Comment(2)
There is a writeup of mine if you want to know more: medium.com/@lucjansuski/…. In short, instead of disabling timeout you should send a heartbeat as Phoenix does which will keep the connection alive with frequency lower than the timeout.Langston
Heartbeats are the best option because some mobile networks may also disconnect the TCP/IP connection if nothing is moving more often than about once every 35 seconds. Sending a short heartbeat message every 30 seconds should be okay for all devices.Thermoplastic
F
3

I found answer,

I changed timeout connect 0ms, timeout client 0ms, timeout server 0ms in defaults section then my connection is persistent connection because if i give value 0 then it will be infinite connection timeout value.

My final working config is below,

global
        daemon
        maxconn 4032
        pidfile /var/run/haproxy.pid

    defaults
        mode http
        timeout connect 0ms
        timeout client 0ms
        timeout server 0ms
        option  http-server-close
        maxconn 4032

    frontend http-in
        bind *:80
        acl is_websocket hdr_end(host) -i WebSocket
        use_backend servers if is_websocket
        default_backend servers
        option  redispatch
        option  http-server-close
        maxconn  2000
        contimeout  500000
        clitimeout  500000
        srvtimeout  500000
        contimeout  500000        
        timeout contimeout  500000 
        timeout connect  500000
        timeout client  500000

    backend servers
        server server1 127.0.0.1:8886 maxconn 4032
Flowerage answered 26/3, 2013 at 9:45 Comment(0)
I
9

I changed timeout connect 0ms, timeout client 0ms, timeout server 0ms in defaults section then my connection is persistent connection because if i give value 0 then it will be infinite connection timeout value.

You should not do this, because these options apply to usual HTTP traffic, too. Set timeout connect / client / server to appropriate values and use timeout tunnel for websockets.

The tunnel timeout applies when a bidirectional connection is established between a client and a server, and the connection remains inactive in both directions. This timeout supersedes both the client and server timeouts once the connection becomes a tunnel.

(See: http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#timeout%20tunnel)

Izawa answered 19/9, 2015 at 7:26 Comment(1)
See also: lucjan.medium.com/…Thermoplastic
F
3

I found answer,

I changed timeout connect 0ms, timeout client 0ms, timeout server 0ms in defaults section then my connection is persistent connection because if i give value 0 then it will be infinite connection timeout value.

My final working config is below,

global
        daemon
        maxconn 4032
        pidfile /var/run/haproxy.pid

    defaults
        mode http
        timeout connect 0ms
        timeout client 0ms
        timeout server 0ms
        option  http-server-close
        maxconn 4032

    frontend http-in
        bind *:80
        acl is_websocket hdr_end(host) -i WebSocket
        use_backend servers if is_websocket
        default_backend servers
        option  redispatch
        option  http-server-close
        maxconn  2000
        contimeout  500000
        clitimeout  500000
        srvtimeout  500000
        contimeout  500000        
        timeout contimeout  500000 
        timeout connect  500000
        timeout client  500000

    backend servers
        server server1 127.0.0.1:8886 maxconn 4032
Flowerage answered 26/3, 2013 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.