jetty.project icon indicating copy to clipboard operation
jetty.project copied to clipboard

Per request connection timeout

Open jacob-pro opened this issue 6 months ago • 3 comments

Jetty version(s)

Jetty 12+

Enhancement Description

Currently it is possible to override the idle timeout on a per-request basis but there doesn't seem to be a way to do the equivalent for connection timeouts:

        HttpClient httpClient = new HttpClient();
        httpClient.setIdleTimeout(DEFAULT_IDLE_TIMEOUT);
        httpClient.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);

        httpClient.newRequest("https://example.com")
            .idleTimeout(CUSTOM_IDLE_TIMEOUT, TimeUnit.MILLISECONDS)
            // .connectTimeout() - How to override the default connection timeout??
            .send();

Whilst it is possible to modify the connection timeout on an existing client, it would not be threadsafe if dispatching multiple requests concurrently.

It seems the only way to ensure a unique connection timeout right now is to create a new client (which adds overhead of a separate connection pool and other resources).

Expected behaviour:

  1. If there is already a connection in the pool, then then no timeout is applicable, it is just taken from the pool.
  2. If a new connection needs to be established, use either the request level connection timeout, or otherwise the client level request timeout.
  3. This should be customisable using the WebSocket client as well via the JettyUpgradeListener#onHandshakeRequest.

I would be happy to try and make a PR for this if it seems acceptable and feasible?

jacob-pro avatar May 08 '25 09:05 jacob-pro