Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I limit the number of connections Jetty will accept?

I'm running Jetty 7.2.2 and want to limit the number of connections it will handle, such that when it reaches a limit (eg 5000), it will start refusing connections.

Unfortunately, all the Connectors appear to just go ahead and accept incoming connections as fast as they can and dispatch them to the configured thread pool.

My problem is that I'm running in a constrained environment, and I only have access to 8K file descriptors. If I get a bunch of connections coming in I can quickly run out of file descriptors and get into an inconsistent state.

One option I have is to return an HTTP 503 Service Unavailable, but that still requires me to accept and respond to the connection - and I'd have keep track of the number of incoming connections somewhere, perhaps by writing a servlet filter.

Is there a better solution to this?

like image 510
BigBen Avatar asked Apr 20 '11 23:04

BigBen


People also ask

How many connections can Jetty handle?

In HTTP/1.1, a destination holds a pool of connections (controlled by maxConnectionsPerDestination, by default 64). So if you send requests in a tight loop, assuming connection establishment takes zero time, you can have at most 64 (on the network) + 1024 (queued) outstanding requests. The next one will be rejected.

How does jetty handle multiple requests?

There is this server port that jetty listens on and some number of acceptor threads whose job it is to get connection objects made between the client and server side.


1 Answers

The thread pool has a queue associated with it. By default, it is unbounded. However, when creating a thread pool you can provide a bounded queue to base it on. For example:

Server server = new Server();
LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(maxQueueSize);
ExecutorThreadPool pool = new ExecutorThreadPool(minThreads, maxThreads, maxIdleTime, TimeUnit.MILLISECONDS, queue);
server.setThreadPool(pool);

This appears to have resolved the problem for me. Otherwise, with the unbounded queue the server ran out of file handles as it started up under heavy load.

like image 167
Darin Wright Avatar answered Sep 21 '22 05:09

Darin Wright