10

I'm developing a game server currently, to avoid developing the server from scratch, tomcat 7.0 is employed so that I can focus on the game logic.

Base on the requirement, I use websocket to communicate with the clients, but when many clients have connected to the server, new connection can't be established, I doubt the count of established connections has reached the maximum count. By the way, APR connector is used by tomcat.

So, my questions are:

  1. What's the maximum count of active websocket connections supported by tomcat 7.0.
  2. How to configure it.
  3. Is there any solution to load balancing of websocket, because apache and mod_jk can't be used for load balancing now.

Any help will be appreciated, thanks in advance!

Alanmars
  • 1,267
  • 1
  • 11
  • 21

4 Answers4

15

TO reach the max alive websocket connections in Tomcat, the following config changes need to be done.

  1. {CATALINA_HOME}/conf/server.xml

    <Connector connectionTimeout="-1" port="8080" 
           protocol="org.apache.coyote.http11.Http11NioProtocol" 
           redirectPort="8443" maxConnections="100000" acceptCount="300"/>
    
  2. Check the number of ports which are available for use on the machiine where Tomcat is deployed:

    $ cat /proc/sys/net/ipv4/ip_local_port_range
    

    Change this from 50 to 65535.

    $ sysctl -w net.ipv4.ip_local_port_range="500   65535"
    

The above configuration changes allow around ~50k live connections in a 2GB Intel Core i5 machine provided the server and client are running on different machines.

slm
  • 15,396
  • 12
  • 109
  • 124
Arijeet Saha
  • 1,118
  • 11
  • 23
  • This issue had been bugging us for some time. Changing the protocol to NIO did the trick. Thank you! – Bere Aug 30 '16 at 18:05
  • Glad it helped @Bere :) – Arijeet Saha Aug 31 '16 at 09:15
  • I disagree with this. The concurrent web socket connects doesn't depend on TCP port limitation. You can have as much connection as you want on the same port, its the limitation of resources & threads available on the server which becomes the bottleneck to serve the more request and hence we need to horizontally scale to suffice the connection requests. ref: https://stackoverflow.com/questions/17448061/how-many-system-resources-will-be-held-for-keeping-1-000-000-websocket-open – Prateek Gupta Jun 02 '21 at 17:34
3
  1. The upper limit is the number of TCP connections your server can support.
  2. The default enforced limit will depend on the connector you are using - information that you failed to provided. For NIO and APOR/native you'll want to increase maxConnections. For bIO you;'ll want to increase maxThreads.
  3. That depends on the load balancer you are using - again information you failed to provide.
Mark Thomas
  • 16,339
  • 1
  • 39
  • 60
0

The total number of websocket connections are determined by number of open file descriptors permitted by OS on which websocket server( tomcat instance in this case) is running.On a unix machine we have a hard limit of 75000 file descriptors but I am not sure if there is a Hard limit on tomcat's ability to spawn new sockets

Robin Varghese
  • 465
  • 1
  • 5
  • 11
  • With regards to your Websocket Apache handler. You can use the below plugin for loadbalancig websocket traffic.http://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html – Robin Varghese Dec 12 '14 at 16:22
-1

for your first two question i think the answer can be found in the the server.xml file in conf folder of tomcat (~/conf/server.xml) see the Executor tag and also the available attribute and parameters of connector the following links may help you (I brought them from server.xml file comments):

Tomcat Java HTTP Connector

As the documentation said you can use the attributes of connector or use the Executor and link it with connector, see the description of executor attribute in connector tag:

A reference to the name in an Executor element. If this attribute is set, 
and the named executor exists, the connector will use the executor, 
and all the other thread attributes will be ignored. Note that 
if a shared executor is not specified for a connector then 
the connector will use a private, internal executor 
to provide the thread pool.

Also you can override the number of connections for Java AJP connector: see the following documentation:

Java AJP Connector

for your last question, I think if can't use replication and loadbalancing tools for now, then the answer is to finish your work inside the server as soon as possible to release the resources and make them available to other clients.

mohamed sulibi
  • 526
  • 3
  • 12
  • The executor has no bearing on the number of connections. It only controls the number of threads available to process those connections. Threads != connections with non-blocking IO (which is the point of using non-blocking IO after all). Given that AJP does not support WebSocket that link is irrelevant. – Mark Thomas May 20 '14 at 19:38