Skip to content

Commit

Permalink
Uses socket config instead of server config to configure idle connect…
Browse files Browse the repository at this point in the history
…ion handler. Also adds logging and updates test. (#9253)

Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas authored Sep 17, 2024
1 parent 5d3e1be commit eabcf7a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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",
Expand All @@ -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!"));

Expand All @@ -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) {
Expand Down

0 comments on commit eabcf7a

Please sign in to comment.