简体   繁体   中英

Closing SockJS connection

I'm using HAProxy in front of my SockJS servers and am having troubles with SockJS connections not seeming to close properly.

My NodeJS code:

var sockjsSockets = {};


/**
 * SockJS Server
 */
var echo = sockjs.createServer({
    log: function (severity, message) {
        // Skip logging
    },
    disconnect_delay: 20
});
echo.on('connection', function (conn) {

    // Identify this connection
    sockjsSockets[conn.id] = conn;

    conn.on('data', function (message) {

        // Write...

    });
    conn.on('close', function () {

        try {
            delete sockjsSockets[conn.id];

            conn.close(); 
        } catch (er) {
            console.log("EXCEPTION CLOSE SOCKJS ::::::::>>>>>>> " + er.stack);
        }
    });
});

The sockjsSockets count can for example show: 400 But HAProxy is showing 3000ish current connections to that NodeJS instance

Am I missing something in the close process here?

Any ideas what could be wrong?

Edit

Here is my HAProxy config

global
      maxconn     400000
      ulimit-n    800019
      nbproc      1
      debug
      daemon
      log         127.0.0.1   local0 notice

  defaults
      mode        http
      option      httplog
      log         global
      stats       enable
      stats       refresh 60s
      stats uri   /stats
      maxconn     32768


  frontend  secured
      timeout     client 86400000

      mode http
      timeout client 120s

      option httpclose
      option forwardfor

      bind        0.0.0.0:443 ssl crt /etc/nginx/ssl/ssl-bundle.pem ciphers RC4-SHA:AES128-SHA:AES:!ADH:!aNULL:!DH:!EDH:!eNULL


      acl is_sockjs path_beg /echo /broadcast /close # this is sockjs
      use_backend sockjs if is_sockjs

There are different protocols can be used by SockJS, starting from WebSockets, falling down to Long Poll, AJAX and many others. Each of those will have different logic of keeping connection.

In case with WebSockets it is persistent TCP connection, and if it is closed, then it should be closed. While web server can still keep it as it behaves as proxy.

To address such problem you need to play with settings such as keep alive, timeouts and so on for your web server, and discover solution to make sure HAProxy respects node.js closing connection logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM