Skip to content

Commit f54bd28

Browse files
committed
fixing connection timeout configuration for Netty
1 parent 054c8ee commit f54bd28

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,19 @@ public int getOrder() {
6060
public void customize(NettyReactiveWebServerFactory factory) {
6161
factory.setUseForwardHeaders(
6262
getOrDeduceUseForwardHeaders(this.serverProperties, this.environment));
63-
PropertyMapper propertyMapper = PropertyMapper.get();
64-
propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize).whenNonNull()
63+
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
64+
65+
propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize)
6566
.asInt(DataSize::toBytes)
6667
.to((maxHttpRequestHeaderSize) -> customizeMaxHttpHeaderSize(factory,
6768
maxHttpRequestHeaderSize));
68-
propertyMapper.from(this.serverProperties::getConnectionTimeout).whenNonNull()
69-
.asInt(Duration::toMillis).to((duration) -> factory
69+
70+
propertyMapper.from(this.serverProperties::getConnectionTimeout)
71+
.asInt(Duration::toMillis)
72+
.whenNot((connectionTimout) -> connectionTimout.equals(0))
73+
.as((connectionTimeout) -> connectionTimeout.equals(-1) ? 0
74+
: connectionTimeout)
75+
.to((duration) -> factory
7076
.addServerCustomizers(getConnectionTimeOutCustomizer(duration)));
7177
}
7278

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

@@ -49,6 +55,12 @@ public void setup() {
4955
this.serverProperties);
5056
}
5157

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

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

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* {@link Source#toInstance(Function) new instance}.
5151
*
5252
* @author Phillip Webb
53+
* @author Artsiom Yudovin
5354
* @since 2.0.0
5455
*/
5556
public final class PropertyMapper {
@@ -289,7 +290,7 @@ public <R extends T> Source<R> whenInstanceOf(Class<R> target) {
289290
*/
290291
public Source<T> whenNot(Predicate<T> predicate) {
291292
Assert.notNull(predicate, "Predicate must not be null");
292-
return new Source<>(this.supplier, predicate.negate());
293+
return when(predicate.negate());
293294
}
294295

295296
/**
@@ -300,7 +301,13 @@ public Source<T> whenNot(Predicate<T> predicate) {
300301
*/
301302
public Source<T> when(Predicate<T> predicate) {
302303
Assert.notNull(predicate, "Predicate must not be null");
303-
return new Source<>(this.supplier, predicate);
304+
305+
if (Objects.nonNull(this.predicate)) {
306+
return new Source<>(this.supplier, this.predicate.and(predicate));
307+
}
308+
else {
309+
return new Source<>(this.supplier, predicate);
310+
}
304311
}
305312

306313
/**

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Tests for {@link PropertyMapper}.
2929
*
3030
* @author Phillip Webb
31+
* @author Artsiom Yudovin
3132
*/
3233
public class PropertyMapperTests {
3334

@@ -201,6 +202,18 @@ public void alwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() {
201202
this.map.alwaysApplyingWhenNonNull().from(() -> null).toCall(Assert::fail);
202203
}
203204

205+
@Test
206+
public void whenWhenValueNotMatchesShouldCheckTwoWhen() {
207+
this.map.from("123").when("456"::equals).when("123"::equals).toCall(Assert::fail);
208+
}
209+
210+
@Test
211+
public void whenWhenValueMatchesShouldCheckTwoWhen() {
212+
String result = this.map.from("123").when((s) -> s.contains("2"))
213+
.when("123"::equals).toInstance(String::new);
214+
assertThat(result).isEqualTo("123");
215+
}
216+
204217
private static class Count<T> implements Supplier<T> {
205218

206219
private final Supplier<T> source;

0 commit comments

Comments
 (0)