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. Updates test. (helidon-io#9252)

Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas authored Sep 17, 2024
1 parent efa5a77 commit fec1bef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void initChannel(SocketChannel ch) {
directHandlers));

// 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() {
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,25 +63,29 @@ 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(Routing.builder().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",
null,
List.of("Connection: keep-alive"));
"/hello",
null,
List.of("Connection: keep-alive"));
String res = client.receive();
assertThat(res, containsString("Hello World!"));

Expand Down

0 comments on commit fec1bef

Please sign in to comment.