Commit b0e4c716 authored by ayudovin's avatar ayudovin Committed by Phillip Webb

Fix connection timeout configuration for Netty

Update `NettyWebServerFactoryCustomizer` to deal with the fact that
Netty treats `0` and negative connection timeout values differently to
Tomcat, Undertow and Jetty.

See gh-16535
parent c2d1cb2c
...@@ -58,10 +58,12 @@ public class NettyWebServerFactoryCustomizer ...@@ -58,10 +58,12 @@ public class NettyWebServerFactoryCustomizer
@Override @Override
public void customize(NettyReactiveWebServerFactory factory) { public void customize(NettyReactiveWebServerFactory factory) {
factory.setUseForwardHeaders(getOrDeduceUseForwardHeaders(this.serverProperties, this.environment)); factory.setUseForwardHeaders(getOrDeduceUseForwardHeaders(this.serverProperties, this.environment));
PropertyMapper propertyMapper = PropertyMapper.get(); PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize).whenNonNull().asInt(DataSize::toBytes) propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize).asInt(DataSize::toBytes)
.to((maxHttpRequestHeaderSize) -> customizeMaxHttpHeaderSize(factory, maxHttpRequestHeaderSize)); .to((maxHttpRequestHeaderSize) -> customizeMaxHttpHeaderSize(factory, maxHttpRequestHeaderSize));
propertyMapper.from(this.serverProperties::getConnectionTimeout).whenNonNull().asInt(Duration::toMillis) propertyMapper.from(this.serverProperties::getConnectionTimeout).asInt(Duration::toMillis)
.whenNot((connectionTimout) -> connectionTimout.equals(0))
.as((connectionTimeout) -> connectionTimeout.equals(-1) ? 0 : connectionTimeout)
.to((duration) -> factory.addServerCustomizers(getConnectionTimeOutCustomizer(duration))); .to((duration) -> factory.addServerCustomizers(getConnectionTimeOutCustomizer(duration)));
} }
......
...@@ -16,21 +16,27 @@ ...@@ -16,21 +16,27 @@
package org.springframework.boot.autoconfigure.web.embedded; package org.springframework.boot.autoconfigure.web.embedded;
import java.time.Duration;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources; import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link NettyWebServerFactoryCustomizer}. * Tests for {@link NettyWebServerFactoryCustomizer}.
* *
* @author Brian Clozel * @author Brian Clozel
* @author Artsiom Yudovin
*/ */
public class NettyWebServerFactoryCustomizerTests { public class NettyWebServerFactoryCustomizerTests {
...@@ -48,6 +54,12 @@ public class NettyWebServerFactoryCustomizerTests { ...@@ -48,6 +54,12 @@ public class NettyWebServerFactoryCustomizerTests {
this.customizer = new NettyWebServerFactoryCustomizer(this.environment, this.serverProperties); this.customizer = new NettyWebServerFactoryCustomizer(this.environment, this.serverProperties);
} }
private void clear() {
this.serverProperties.setUseForwardHeaders(null);
this.serverProperties.setMaxHttpHeaderSize(null);
this.serverProperties.setConnectionTimeout(null);
}
@Test @Test
public void deduceUseForwardHeaders() { public void deduceUseForwardHeaders() {
this.environment.setProperty("DYNO", "-"); this.environment.setProperty("DYNO", "-");
...@@ -71,4 +83,24 @@ public class NettyWebServerFactoryCustomizerTests { ...@@ -71,4 +83,24 @@ public class NettyWebServerFactoryCustomizerTests {
verify(factory).setUseForwardHeaders(true); verify(factory).setUseForwardHeaders(true);
} }
@Test
public void setConnectionTimeoutAsZero() {
clear();
this.serverProperties.setConnectionTimeout(Duration.ZERO);
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
this.customizer.customize(factory);
verify(factory, times(0)).addServerCustomizers(any(NettyServerCustomizer.class));
}
@Test
public void setConnectionTimeoutAsMinusOne() {
clear();
this.serverProperties.setConnectionTimeout(Duration.ofNanos(-1));
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
this.customizer.customize(factory);
verify(factory, times(1)).addServerCustomizers(any(NettyServerCustomizer.class));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment