On Tue, 6 Sep 2022 13:56:15 GMT, Conor Cleary <ccle...@openjdk.org> wrote:
> **Issue** > When using HTTP/2 with the HttpClient, it can often be necessary to close an > idle Http2 Connection before a server sends a GOAWAY frame. For example, a > server or cloud based tool could close a TCP connection silently when it is > idle for too long resulting in ConnectionResetException being thrown by the > HttpClient. > > **Proposed Solution** > A new system property, `jdk.httpclient.idleConnectionTimeout`, was added and > is used to specify in Milliseconds how long an idle connection (idle > connections are those which have no currently active streams) for the > HttpClient before the connection is closed. src/java.net.http/share/classes/jdk/internal/net/http/Http2Connection.java line 1037: > 1035: } else { > 1036: // Start timer if property present and not already created > 1037: synchronized (this) { I think we can avoid this `synchronized` block every time we close a non-final stream, by slightly changing this code to something like: final Optional<Duration> idleTimeoutDuration = client().idleConnectionTimeout(); if (idleTimeoutDuration.isPresent()) { synchronized(this) { // idleConnectionTimerEvent is always accessed within a synchronized block if (streams.isEmpty() && idleConnectionTimeoutEvent == null) { idleConnectionTimeoutEvent = idleTimeoutDuration.map(IdleConnectionTimeoutEvent::new).get(); client().registerTimer(idleConnectionTimeoutEvent); } } } This would avoid the `synchronized` block when no idle timeout is configured. src/java.net.http/share/classes/jdk/internal/net/http/Http2Connection.java line 1222: > 1220: if (idleConnectionTimeoutEvent != null) { > 1221: client().cancelTimer(idleConnectionTimeoutEvent); > 1222: System.err.println("Http2Connection: Timer Event > Cancelled"); I suspect this is a left over `System.err` message? ------------- PR: https://git.openjdk.org/jdk/pull/10183