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
This commit is contained in:
@@ -58,10 +58,12 @@ public class NettyWebServerFactoryCustomizer
|
||||
@Override
|
||||
public void customize(NettyReactiveWebServerFactory factory) {
|
||||
factory.setUseForwardHeaders(getOrDeduceUseForwardHeaders(this.serverProperties, this.environment));
|
||||
PropertyMapper propertyMapper = PropertyMapper.get();
|
||||
propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize).whenNonNull().asInt(DataSize::toBytes)
|
||||
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize).asInt(DataSize::toBytes)
|
||||
.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)));
|
||||
}
|
||||
|
||||
|
||||
@@ -16,21 +16,27 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.embedded;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
|
||||
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link NettyWebServerFactoryCustomizer}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Artsiom Yudovin
|
||||
*/
|
||||
public class NettyWebServerFactoryCustomizerTests {
|
||||
|
||||
@@ -48,6 +54,12 @@ public class NettyWebServerFactoryCustomizerTests {
|
||||
this.customizer = new NettyWebServerFactoryCustomizer(this.environment, this.serverProperties);
|
||||
}
|
||||
|
||||
private void clear() {
|
||||
this.serverProperties.setUseForwardHeaders(null);
|
||||
this.serverProperties.setMaxHttpHeaderSize(null);
|
||||
this.serverProperties.setConnectionTimeout(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deduceUseForwardHeaders() {
|
||||
this.environment.setProperty("DYNO", "-");
|
||||
@@ -71,4 +83,24 @@ public class NettyWebServerFactoryCustomizerTests {
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user