Reputation: 419
I'm using spring+jetty. I'm configuring jetty:
@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
final JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(port);
factory.addServerCustomizers((Server server) -> {
final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class);
threadPool.setMinThreads(minThreads);
threadPool.setMaxThreads(maxThreads);
threadPool.setIdleTimeout(1000);
});
return factory;
}
It works, but strange.
So, where are my 5 threads?
Upvotes: 4
Views: 3420
Reputation: 49555
You are not accounting for how each of your connectors allocates selectors and acceptors into the same threadpool.
You are also not taking into account the system/hardware effects into the thread pool (how many cpu cores do you have? yes it matters).
You are also defining a rediculously tiny threadpool. Do you plan on only serving 1 http connection in a row? on 1 connector, using HTTP/1.0, with keep-alive, and no compression, with perfect network conditions that never fail or timeout?
Are you 100% sure that your user-agents (clients) will follow those rules?
Hint: the web page you are looking at right now, on stackoverflow, using a modern web browser, would have used from 9 to 18 active threads from the thread pool, grown the thread pool maximum to about 35, and would have been done with them all in 399ms.
Consider that any of the following future decisions would increase your thread pool pressures.
Your settings should have thrown an IllegalStateException indicating your very low configuration.
java.lang.IllegalStateException: Insufficient threads: max=8 < needed(acceptors=1 + selectors=8 + request=1)
Since you said you are using Spring, consider following the open issue about this at
https://github.com/spring-projects/spring-boot/issues/8917
Here's some general (large hand waving) advice about tuning for maximum threads ...
The smallest web site we know of using Jetty uses 19MB of memory, and has a configured 8 maximum threads in its thread pool. (its a weather station in a remote part of Norway)
The largest single machine web site we know of using Jetty uses 189GB of memory, on a 30 core machine, with 30,000 thread maximum configured, with 8 network interfaces, HTTP/1.x and HTTP/2 enabled on each, doing SSL + Gzip + WebSockets, with an average of 280,000 active connections at any one moment (peaks at just shy of 800,000 active connections at certain times of day, every day)
Upvotes: 5