1

I am trying to figure out how to get the maximum number of websockets that jetty will allow to open. It seems to peak at 254 from my laptop chrome. I am trying to determine if it's chrome limitation or jetty limitation or something else.

package jetty;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.websocket.server.WebSocketHandler;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;

public class WebSocketTest {

public static void main(String[] args) throws Exception {
    Server server = new Server(8383);
    WebSocketHandler wsHandler = new WebSocketHandler() {
        @Override
        public void configure(WebSocketServletFactory factory) {
          factory.getPolicy().setIdleTimeout(-1);
          factory.register(MyWebSocketHandler.class);
        }
    };

    server.setHandler(wsHandler);
    server.start();
    server.join();     

}

}
user603749
  • 1,638
  • 2
  • 24
  • 36

1 Answers1

1

This depends on your ThreadPool configuration.

By default the Server starts with a QueuedThreadPool with a maximum of 200 threads.

Note: there's no 1 to 1 relationship between number of threads in thread pool to number of websockets. If you have idle websockets, then the thread is returned the pool for other active websockets to use.

It is quite easy to have 40,000 active websockets being served from 200 max thread ThreadPool configuration (If you have low activity websocket connections. You use Jetty's native WebSocket implementation, not the javax.websocket implementation. You do not use Objects/Streams/streaming based message handling, but instead use normal String / ByteBuffer messaging. Return from onMessage dispatch immediately, don't start your own threads. etc)

For more about sizing Threads on ThreadPools see https://stackoverflow.com/a/44048711/775715

The ThreadPool is created and then passed in on the Server constructor, like this ...

    ThreadPool threadPool = new QueuedThreadPool(400);
    Server server = new Server(threadPool);
    ServerConnector http = new ServerConnector(server);
    http.setPort(8080);
    server.addConnector(http);
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Hi, Is there a way to validate and check this?? Like any jetty code snippet or their design doc that shows that number of connections are not linked to the QTP of server? Wanted to know how the ordering of requests are maintained from a connector to the handler on a same connection. (Which is supposed to be the guarantee of websockets). – polavishnu Apr 11 '17 at 06:01
  • Once an http request has been upgraded past http and into websocket, its not longer beholden to the http behaviors of a handler. The number of connections is a meaningless measurement here, focus on the number of active connections (ones with traffic), as that's how all of the threading and nio processing works in Jetty (only have thread allocated for I/O operation, from network selectors or blocking APIs). – Joakim Erdfelt Apr 11 '17 at 15:03
  • Sorry for troubling you more, please bare my ignorance. As per my understanding for any http request the connector would give the request to the handler with a thread to process that, is my understanding correct? If so, then how will requests from active connections be handled in case of websocket handlers as requests order need to be maintained across threads? Or is it that for a group of connections same handler thread processes? – polavishnu Apr 12 '17 at 03:46
  • The problem here is that the server will keep an TCP/IP connection permanently open. The protocol switches to WebSocket. It is like having a normal socket connection. It is not an emulation of a (logical) socket connection on top of http but a real permanent socket connection. See: https://stackoverflow.com/questions/34730334/how-websockets-are-implemented – Martin Kersten Aug 12 '18 at 07:47