From cbcd7b939b798423840c09790fa251c25ad8eaf1 Mon Sep 17 00:00:00 2001 From: Saraswathy Hariharakrishnan Date: Fri, 16 Jul 2021 11:02:32 -0700 Subject: [PATCH 1/2] Add idle timeout property for Reactor Netty See gh-27371 --- .../autoconfigure/web/ServerProperties.java | 14 ++++++++++ .../NettyWebServerFactoryCustomizer.java | 6 +++++ .../web/ServerPropertiesTests.java | 6 +++++ .../NettyWebServerFactoryCustomizerTests.java | 27 ++++++++++++++++--- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 027551b6c7..2fe7e5dc4f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -1300,6 +1300,12 @@ public class ServerProperties { */ private boolean validateHeaders = true; + /** + * IdleTimeout for netty server .If an idletimeout is not specified, this + * indicates no timeout(infinite). + */ + private Duration idleTimeout; + public Duration getConnectionTimeout() { return this.connectionTimeout; } @@ -1348,6 +1354,14 @@ public class ServerProperties { this.validateHeaders = validateHeaders; } + public Duration getIdleTimeout() { + return this.idleTimeout; + } + + public void setIdleTimeout(Duration idleTimeout) { + this.idleTimeout = idleTimeout; + } + } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java index 2e807bd6f9..93a16e4204 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java @@ -60,6 +60,8 @@ public class NettyWebServerFactoryCustomizer ServerProperties.Netty nettyProperties = this.serverProperties.getNetty(); propertyMapper.from(nettyProperties::getConnectionTimeout).whenNonNull() .to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout)); + propertyMapper.from(nettyProperties::getIdleTimeout).whenNonNull() + .to((idleTimeout) -> customizeIdleTimeout(factory, idleTimeout)); customizeRequestDecoder(factory, propertyMapper); } @@ -98,4 +100,8 @@ public class NettyWebServerFactoryCustomizer })); } + private void customizeIdleTimeout(NettyReactiveWebServerFactory factory, Duration idleTimeout) { + factory.addServerCustomizers((httpServer) -> httpServer.idleTimeout(idleTimeout)); + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index 02c0ab6e69..af08e0dd23 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -535,6 +535,12 @@ class ServerPropertiesTests { .isEqualTo(HttpDecoderSpec.DEFAULT_INITIAL_BUFFER_SIZE); } + @Test + void testCustomizeNettyIdleTimeout() { + bind("server.netty.idle-timeout", "10s"); + assertThat(this.properties.getNetty().getIdleTimeout()).isEqualTo(Duration.ofSeconds(10)); + } + private Connector getDefaultConnector() { return new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java index 7bd0e43703..b876006908 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java @@ -104,12 +104,22 @@ class NettyWebServerFactoryCustomizerTests { @Test void setConnectionTimeout() { - setupConnectionTimeout(Duration.ofSeconds(1)); + setServerProperties(); + this.serverProperties.getNetty().setConnectionTimeout(Duration.ofSeconds(1)); NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class); this.customizer.customize(factory); verifyConnectionTimeout(factory, 1000); } + @Test + void setIdleTimeout() { + setServerProperties(); + this.serverProperties.getNetty().setIdleTimeout(Duration.ofSeconds(1)); + NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class); + this.customizer.customize(factory); + verifyIdleTimeout(factory, Duration.ofSeconds(1)); + } + @Test void configureHttpRequestDecoder() { ServerProperties.Netty nettyProperties = this.serverProperties.getNetty(); @@ -143,10 +153,21 @@ class NettyWebServerFactoryCustomizerTests { assertThat(options.get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(expected); } - private void setupConnectionTimeout(Duration connectionTimeout) { + private void verifyIdleTimeout(NettyReactiveWebServerFactory factory, Duration expected) { + if (expected == null) { + verify(factory, never()).addServerCustomizers(any(NettyServerCustomizer.class)); + return; + } + verify(factory, times(2)).addServerCustomizers(this.customizerCaptor.capture()); + NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0); + HttpServer httpServer = serverCustomizer.apply(HttpServer.create()); + Duration idleTimeout = httpServer.configuration().idleTimeout(); + assertThat(idleTimeout).isEqualTo(expected); + } + + private void setServerProperties() { this.serverProperties.setForwardHeadersStrategy(ForwardHeadersStrategy.NONE); this.serverProperties.setMaxHttpHeaderSize(null); - this.serverProperties.getNetty().setConnectionTimeout(connectionTimeout); } } From ed38ac6ff0a28eef8a7b2c330bd4dcd497110945 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 19 Jul 2021 14:58:15 +0100 Subject: [PATCH 2/2] Polish "Add idle timeout property for Reactor Netty" See gh-27371 --- .../boot/autoconfigure/web/ServerProperties.java | 4 ++-- .../embedded/NettyWebServerFactoryCustomizer.java | 2 +- .../autoconfigure/web/ServerPropertiesTests.java | 12 ++++++------ .../NettyWebServerFactoryCustomizerTests.java | 10 +--------- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 2fe7e5dc4f..9de2e454b2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -1301,8 +1301,8 @@ public class ServerProperties { private boolean validateHeaders = true; /** - * IdleTimeout for netty server .If an idletimeout is not specified, this - * indicates no timeout(infinite). + * Idle timeout of the Netty channel. When not specified, an infinite timeout is + * used. */ private Duration idleTimeout; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java index 93a16e4204..5309f5ee1e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index af08e0dd23..ea1ede9841 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -333,6 +333,12 @@ class ServerPropertiesTests { assertThat(jetty.getAccesslog().getIgnorePaths()).containsExactly("/a/path", "/b/path"); } + @Test + void testCustomizeNettyIdleTimeout() { + bind("server.netty.idle-timeout", "10s"); + assertThat(this.properties.getNetty().getIdleTimeout()).isEqualTo(Duration.ofSeconds(10)); + } + @Test void tomcatAcceptCountMatchesProtocolDefault() throws Exception { assertThat(this.properties.getTomcat().getAcceptCount()).isEqualTo(getDefaultProtocol().getAcceptCount()); @@ -535,12 +541,6 @@ class ServerPropertiesTests { .isEqualTo(HttpDecoderSpec.DEFAULT_INITIAL_BUFFER_SIZE); } - @Test - void testCustomizeNettyIdleTimeout() { - bind("server.netty.idle-timeout", "10s"); - assertThat(this.properties.getNetty().getIdleTimeout()).isEqualTo(Duration.ofSeconds(10)); - } - private Connector getDefaultConnector() { return new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java index b876006908..1ea1a95cc4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,7 +30,6 @@ import reactor.netty.http.server.HttpRequestDecoderSpec; import reactor.netty.http.server.HttpServer; import org.springframework.boot.autoconfigure.web.ServerProperties; -import org.springframework.boot.autoconfigure.web.ServerProperties.ForwardHeadersStrategy; import org.springframework.boot.context.properties.source.ConfigurationPropertySources; import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; import org.springframework.boot.web.embedded.netty.NettyServerCustomizer; @@ -104,7 +103,6 @@ class NettyWebServerFactoryCustomizerTests { @Test void setConnectionTimeout() { - setServerProperties(); this.serverProperties.getNetty().setConnectionTimeout(Duration.ofSeconds(1)); NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class); this.customizer.customize(factory); @@ -113,7 +111,6 @@ class NettyWebServerFactoryCustomizerTests { @Test void setIdleTimeout() { - setServerProperties(); this.serverProperties.getNetty().setIdleTimeout(Duration.ofSeconds(1)); NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class); this.customizer.customize(factory); @@ -165,9 +162,4 @@ class NettyWebServerFactoryCustomizerTests { assertThat(idleTimeout).isEqualTo(expected); } - private void setServerProperties() { - this.serverProperties.setForwardHeadersStrategy(ForwardHeadersStrategy.NONE); - this.serverProperties.setMaxHttpHeaderSize(null); - } - }