diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/HttpInitializer.java b/webserver/webserver/src/main/java/io/helidon/webserver/HttpInitializer.java index 392b7ee5839..b1ac5c47df2 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/HttpInitializer.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/HttpInitializer.java @@ -198,13 +198,14 @@ public void initChannel(SocketChannel ch) { } // Set up idle handler to close inactive connections based on config - int idleTimeout = serverConfig.connectionIdleTimeout(); + int idleTimeout = soConfig.connectionIdleTimeout(); if (idleTimeout > 0) { p.addLast(new IdleStateHandler(idleTimeout, idleTimeout, idleTimeout)); p.addLast(new ChannelDuplexHandler() { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { if (evt instanceof IdleStateEvent) { + LOGGER.finer(() -> "Closing idle connection on channel" + ctx.channel()); ctx.close(); // async close of idle connection } } diff --git a/webserver/webserver/src/test/java/io/helidon/webserver/ConnectionIdleTest.java b/webserver/webserver/src/test/java/io/helidon/webserver/ConnectionIdleTest.java index e678923efd9..37d7dc259a3 100644 --- a/webserver/webserver/src/test/java/io/helidon/webserver/ConnectionIdleTest.java +++ b/webserver/webserver/src/test/java/io/helidon/webserver/ConnectionIdleTest.java @@ -23,6 +23,7 @@ import io.helidon.common.http.Http; import io.helidon.webserver.utils.SocketHttpClient; + import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -37,6 +38,7 @@ public class ConnectionIdleTest { private static final Logger LOGGER = Logger.getLogger(ConnectionIdleTest.class.getName()); private static final Duration TIMEOUT = Duration.ofSeconds(10); + private static final String SOCKET_NAME = "idle_socket"; private static final int IDLE_TIMEOUT = 1000; @@ -61,20 +63,24 @@ public static void close() throws Exception { */ private static void startServer(int port) { webServer = WebServer.builder() - .host("localhost") - .port(port) - .connectionIdleTimeout(IDLE_TIMEOUT / 1000) // in seconds - .routing(r -> r.get("/hello", (req, res) -> res.send("Hello World!"))) + .addSocket(SocketConfiguration.builder() + .name(SOCKET_NAME) + .connectionIdleTimeout(IDLE_TIMEOUT / 1000) // in seconds + .port(port) + .host("localhost") + .build(), + Routing.builder().get("/hello", + (req, res) -> res.send("Hello World!")).build()) .build() .start() .await(TIMEOUT); - LOGGER.info("Started server at: https://localhost:" + webServer.port()); + LOGGER.info("Started server at: https://localhost:" + webServer.port(SOCKET_NAME)); } @Test public void testIdleConnectionClosed() throws Exception { - try (SocketHttpClient client = new SocketHttpClient(webServer)) { + try (SocketHttpClient client = new SocketHttpClient(webServer.port(SOCKET_NAME))) { // initial request with keep-alive to open connection client.request(Http.Method.GET, "/hello", @@ -85,8 +91,8 @@ public void testIdleConnectionClosed() throws Exception { // second request while connection is active client.request(Http.Method.GET, - "/hello", - null); + "/hello", + null); res = client.receive(); assertThat(res, containsString("Hello World!")); @@ -96,8 +102,8 @@ public void testIdleConnectionClosed() throws Exception { // try again and either get nothing or an exception try { client.request(Http.Method.GET, - "/hello", - null); + "/hello", + null); res = client.receive(); assertThat(res, is("")); } catch (SocketException e) {