Skip to content

Commit 894daa9

Browse files
committed
Merge pull request #27371 from saraswathy-krish
* gh-27371: Polish "Add idle timeout property for Reactor Netty" Add idle timeout property for Reactor Netty Closes gh-27371
2 parents 9d461a0 + ed38ac6 commit 894daa9

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,12 @@ public static class Netty {
13001300
*/
13011301
private boolean validateHeaders = true;
13021302

1303+
/**
1304+
* Idle timeout of the Netty channel. When not specified, an infinite timeout is
1305+
* used.
1306+
*/
1307+
private Duration idleTimeout;
1308+
13031309
public Duration getConnectionTimeout() {
13041310
return this.connectionTimeout;
13051311
}
@@ -1348,6 +1354,14 @@ public void setValidateHeaders(boolean validateHeaders) {
13481354
this.validateHeaders = validateHeaders;
13491355
}
13501356

1357+
public Duration getIdleTimeout() {
1358+
return this.idleTimeout;
1359+
}
1360+
1361+
public void setIdleTimeout(Duration idleTimeout) {
1362+
this.idleTimeout = idleTimeout;
1363+
}
1364+
13511365
}
13521366

13531367
/**

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,6 +60,8 @@ public void customize(NettyReactiveWebServerFactory factory) {
6060
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
6161
propertyMapper.from(nettyProperties::getConnectionTimeout).whenNonNull()
6262
.to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout));
63+
propertyMapper.from(nettyProperties::getIdleTimeout).whenNonNull()
64+
.to((idleTimeout) -> customizeIdleTimeout(factory, idleTimeout));
6365
customizeRequestDecoder(factory, propertyMapper);
6466
}
6567

@@ -98,4 +100,8 @@ private void customizeRequestDecoder(NettyReactiveWebServerFactory factory, Prop
98100
}));
99101
}
100102

103+
private void customizeIdleTimeout(NettyReactiveWebServerFactory factory, Duration idleTimeout) {
104+
factory.addServerCustomizers((httpServer) -> httpServer.idleTimeout(idleTimeout));
105+
}
106+
101107
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,12 @@ void testCustomizeJettyAccessLog() {
333333
assertThat(jetty.getAccesslog().getIgnorePaths()).containsExactly("/a/path", "/b/path");
334334
}
335335

336+
@Test
337+
void testCustomizeNettyIdleTimeout() {
338+
bind("server.netty.idle-timeout", "10s");
339+
assertThat(this.properties.getNetty().getIdleTimeout()).isEqualTo(Duration.ofSeconds(10));
340+
}
341+
336342
@Test
337343
void tomcatAcceptCountMatchesProtocolDefault() throws Exception {
338344
assertThat(this.properties.getTomcat().getAcceptCount()).isEqualTo(getDefaultProtocol().getAcceptCount());

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,6 @@
3030
import reactor.netty.http.server.HttpServer;
3131

3232
import org.springframework.boot.autoconfigure.web.ServerProperties;
33-
import org.springframework.boot.autoconfigure.web.ServerProperties.ForwardHeadersStrategy;
3433
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
3534
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
3635
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
@@ -104,12 +103,20 @@ void forwardHeadersWhenStrategyIsNoneShouldNotConfigureValve() {
104103

105104
@Test
106105
void setConnectionTimeout() {
107-
setupConnectionTimeout(Duration.ofSeconds(1));
106+
this.serverProperties.getNetty().setConnectionTimeout(Duration.ofSeconds(1));
108107
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
109108
this.customizer.customize(factory);
110109
verifyConnectionTimeout(factory, 1000);
111110
}
112111

112+
@Test
113+
void setIdleTimeout() {
114+
this.serverProperties.getNetty().setIdleTimeout(Duration.ofSeconds(1));
115+
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
116+
this.customizer.customize(factory);
117+
verifyIdleTimeout(factory, Duration.ofSeconds(1));
118+
}
119+
113120
@Test
114121
void configureHttpRequestDecoder() {
115122
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
@@ -143,10 +150,16 @@ private void verifyConnectionTimeout(NettyReactiveWebServerFactory factory, Inte
143150
assertThat(options.get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(expected);
144151
}
145152

146-
private void setupConnectionTimeout(Duration connectionTimeout) {
147-
this.serverProperties.setForwardHeadersStrategy(ForwardHeadersStrategy.NONE);
148-
this.serverProperties.setMaxHttpHeaderSize(null);
149-
this.serverProperties.getNetty().setConnectionTimeout(connectionTimeout);
153+
private void verifyIdleTimeout(NettyReactiveWebServerFactory factory, Duration expected) {
154+
if (expected == null) {
155+
verify(factory, never()).addServerCustomizers(any(NettyServerCustomizer.class));
156+
return;
157+
}
158+
verify(factory, times(2)).addServerCustomizers(this.customizerCaptor.capture());
159+
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
160+
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
161+
Duration idleTimeout = httpServer.configuration().idleTimeout();
162+
assertThat(idleTimeout).isEqualTo(expected);
150163
}
151164

152165
}

0 commit comments

Comments
 (0)