Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Timeouts


On Thu, Nov 23, 2023 at 7:29 AM Matthias Pfau via jetty-users
<jetty-users@xxxxxxxxxxx> wrote:
> Sorry for being too unspecific!
> We are using HTTP/2 with custom handler implementations. All requests are handled asynchronously (Request#startAsync). We are currently setting ServerConnector.idleTimeout to 20 seconds.
> If the client does not send any tcp messages to the server, we want to keep the tcp connection open for five minutes in order to prevent handshaking if the client issues a new request after 1 minute or so.
> If the request of a client stalls (e.g. only partial body has been sent) and no more body data is delivered to the server for 20s, we want to cancel that request.
> We are using Jetty 11 but intend to switch to 12.

It will be simpler in Jetty 12 (no Servlets) than in Jetty 10/11 due
to the required Servlet semantic to ignore idle timeouts when in async

In general you want to configure Jetty in this way:

serverConnector.setIdleTimeout(5 * 60 * 1000);
HTTP2ServerConnectionFactory h2 = ...;
h2.setStreamIdleTimeout(20 * 1000);

In this way, you have a stream idle timeout of 20 seconds for
request/response, and 5 minutes for the TCP connection.

However, in Jetty 10/11 after startAsync(), Servlets must ignore idle
timeouts (per specification), unless there is a pending read or write.
So if your application is pending in a read, the 20 second idle
timeout will be notified with an exception in the read.
Same for the pending writes.

However, if your application is waiting on something else (e.g. an
HTTP call to a 3rd party system), then the 20 seconds idle timeout
will be ignored due to respecting the Servlet specification.
Therefore the only way is to have a timeout at the application level
that you set up.
You can use AsyncContext.setTimeout(), or your own mechanism.

In Jetty 12, if you use the Handler APIs (see here:,
then you can do this:

request.addIdleTimeoutListener(x -> /* you logic here */);

The idle timeout logic may return false to ignore the idle timeout, or
true to make it a failure.
So in Jetty 12 your application is always notified of idle timeouts,
but you can decide whether to ignore them even if you are completely
idle (differently from Servlets where they are ignored).

We have recently cleaned up and strengthened the code for the idle
timeout handling, so I recommend using Jetty 12.0.4 (due in a few
days) or later.

Hope helped.
Simone Bordet
Developer advice, training, services and support
from the Jetty & CometD experts.

Back to the top