Skip to content

Commit b0e4c71

Browse files
ayudovinphilwebb
authored andcommitted
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
1 parent c2d1cb2 commit b0e4c71

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ public int getOrder() {
5858
@Override
5959
public void customize(NettyReactiveWebServerFactory factory) {
6060
factory.setUseForwardHeaders(getOrDeduceUseForwardHeaders(this.serverProperties, this.environment));
61-
PropertyMapper propertyMapper = PropertyMapper.get();
62-
propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize).whenNonNull().asInt(DataSize::toBytes)
61+
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
62+
propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize).asInt(DataSize::toBytes)
6363
.to((maxHttpRequestHeaderSize) -> customizeMaxHttpHeaderSize(factory, maxHttpRequestHeaderSize));
64-
propertyMapper.from(this.serverProperties::getConnectionTimeout).whenNonNull().asInt(Duration::toMillis)
64+
propertyMapper.from(this.serverProperties::getConnectionTimeout).asInt(Duration::toMillis)
65+
.whenNot((connectionTimout) -> connectionTimout.equals(0))
66+
.as((connectionTimeout) -> connectionTimeout.equals(-1) ? 0 : connectionTimeout)
6567
.to((duration) -> factory.addServerCustomizers(getConnectionTimeOutCustomizer(duration)));
6668
}
6769

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,27 @@
1616

1717
package org.springframework.boot.autoconfigure.web.embedded;
1818

19+
import java.time.Duration;
20+
1921
import org.junit.Before;
2022
import org.junit.Test;
2123

2224
import org.springframework.boot.autoconfigure.web.ServerProperties;
2325
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
2426
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
27+
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
2528
import org.springframework.mock.env.MockEnvironment;
2629

30+
import static org.mockito.Mockito.any;
2731
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.times;
2833
import static org.mockito.Mockito.verify;
2934

3035
/**
3136
* Tests for {@link NettyWebServerFactoryCustomizer}.
3237
*
3338
* @author Brian Clozel
39+
* @author Artsiom Yudovin
3440
*/
3541
public class NettyWebServerFactoryCustomizerTests {
3642

@@ -48,6 +54,12 @@ public void setup() {
4854
this.customizer = new NettyWebServerFactoryCustomizer(this.environment, this.serverProperties);
4955
}
5056

57+
private void clear() {
58+
this.serverProperties.setUseForwardHeaders(null);
59+
this.serverProperties.setMaxHttpHeaderSize(null);
60+
this.serverProperties.setConnectionTimeout(null);
61+
}
62+
5163
@Test
5264
public void deduceUseForwardHeaders() {
5365
this.environment.setProperty("DYNO", "-");
@@ -71,4 +83,24 @@ public void setUseForwardHeaders() {
7183
verify(factory).setUseForwardHeaders(true);
7284
}
7385

86+
@Test
87+
public void setConnectionTimeoutAsZero() {
88+
clear();
89+
this.serverProperties.setConnectionTimeout(Duration.ZERO);
90+
91+
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
92+
this.customizer.customize(factory);
93+
verify(factory, times(0)).addServerCustomizers(any(NettyServerCustomizer.class));
94+
}
95+
96+
@Test
97+
public void setConnectionTimeoutAsMinusOne() {
98+
clear();
99+
this.serverProperties.setConnectionTimeout(Duration.ofNanos(-1));
100+
101+
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
102+
this.customizer.customize(factory);
103+
verify(factory, times(1)).addServerCustomizers(any(NettyServerCustomizer.class));
104+
}
105+
74106
}

0 commit comments

Comments
 (0)