Skip to content

Commit 0e94b2c

Browse files
polarbear567snicoll
authored andcommitted
Add server.netty.max-keep-alive-requests
See gh-28875
1 parent 578855f commit 0e94b2c

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
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
@@ -68,6 +68,7 @@
6868
* @author Victor Mandujano
6969
* @author Chris Bono
7070
* @author Parviz Rozikov
71+
* @author Leo Li
7172
* @since 1.0.0
7273
*/
7374
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
@@ -1351,6 +1352,11 @@ public static class Netty {
13511352
*/
13521353
private Duration idleTimeout;
13531354

1355+
/**
1356+
* Maximum number of requests that can be made per connection.
1357+
*/
1358+
private int maxKeepAliveRequests = -1;
1359+
13541360
public Duration getConnectionTimeout() {
13551361
return this.connectionTimeout;
13561362
}
@@ -1407,6 +1413,14 @@ public void setIdleTimeout(Duration idleTimeout) {
14071413
this.idleTimeout = idleTimeout;
14081414
}
14091415

1416+
public int getMaxKeepAliveRequests() {
1417+
return this.maxKeepAliveRequests;
1418+
}
1419+
1420+
public void setMaxKeepAliveRequests(int maxKeepAliveRequests) {
1421+
this.maxKeepAliveRequests = maxKeepAliveRequests;
1422+
}
1423+
14101424
}
14111425

14121426
/**

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* @author Brian Clozel
3535
* @author Chentao Qu
3636
* @author Artsiom Yudovin
37+
* @author Leo Li
3738
* @since 2.1.0
3839
*/
3940
public class NettyWebServerFactoryCustomizer
@@ -62,6 +63,8 @@ public void customize(NettyReactiveWebServerFactory factory) {
6263
.to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout));
6364
propertyMapper.from(nettyProperties::getIdleTimeout).whenNonNull()
6465
.to((idleTimeout) -> customizeIdleTimeout(factory, idleTimeout));
66+
propertyMapper.from(nettyProperties::getMaxKeepAliveRequests).whenNonNull()
67+
.to((maxKeepAliveRequests) -> customizeMaxKeepAliveRequests(factory, maxKeepAliveRequests));
6568
customizeRequestDecoder(factory, propertyMapper);
6669
}
6770

@@ -104,4 +107,8 @@ private void customizeIdleTimeout(NettyReactiveWebServerFactory factory, Duratio
104107
factory.addServerCustomizers((httpServer) -> httpServer.idleTimeout(idleTimeout));
105108
}
106109

110+
private void customizeMaxKeepAliveRequests(NettyReactiveWebServerFactory factory, int maxKeepAliveRequests) {
111+
factory.addServerCustomizers((httpServer) -> httpServer.maxKeepAliveRequests(maxKeepAliveRequests));
112+
}
113+
107114
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
* @author Rafiullah Hamedy
8585
* @author Chris Bono
8686
* @author Parviz Rozikov
87+
* @author Leo Li
8788
*/
8889
class ServerPropertiesTests {
8990

@@ -339,6 +340,17 @@ void testCustomizeNettyIdleTimeout() {
339340
assertThat(this.properties.getNetty().getIdleTimeout()).isEqualTo(Duration.ofSeconds(10));
340341
}
341342

343+
@Test
344+
void testCustomizeNettyMaxKeepAliveRequests() {
345+
bind("server.netty.max-keep-alive-requests", "100");
346+
assertThat(this.properties.getNetty().getMaxKeepAliveRequests()).isEqualTo(100);
347+
}
348+
349+
@Test
350+
void testCustomizeNettyMaxKeepAliveRequestsDefault() {
351+
assertThat(this.properties.getNetty().getMaxKeepAliveRequests()).isEqualTo(-1);
352+
}
353+
342354
@Test
343355
void tomcatAcceptCountMatchesProtocolDefault() throws Exception {
344356
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
*
4949
* @author Brian Clozel
5050
* @author Artsiom Yudovin
51+
* @author Leo Li
5152
*/
5253
@ExtendWith(MockitoExtension.class)
5354
class NettyWebServerFactoryCustomizerTests {
@@ -117,6 +118,14 @@ void setIdleTimeout() {
117118
verifyIdleTimeout(factory, Duration.ofSeconds(1));
118119
}
119120

121+
@Test
122+
void setMaxKeepAliveRequests() {
123+
this.serverProperties.getNetty().setMaxKeepAliveRequests(100);
124+
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
125+
this.customizer.customize(factory);
126+
verifyMaxKeepAliveRequests(factory, 100);
127+
}
128+
120129
@Test
121130
void configureHttpRequestDecoder() {
122131
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
@@ -162,4 +171,12 @@ private void verifyIdleTimeout(NettyReactiveWebServerFactory factory, Duration e
162171
assertThat(idleTimeout).isEqualTo(expected);
163172
}
164173

174+
private void verifyMaxKeepAliveRequests(NettyReactiveWebServerFactory factory, int expected) {
175+
verify(factory, times(2)).addServerCustomizers(this.customizerCaptor.capture());
176+
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
177+
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
178+
int maxKeepAliveRequests = httpServer.configuration().maxKeepAliveRequests();
179+
assertThat(maxKeepAliveRequests).isEqualTo(expected);
180+
}
181+
165182
}

0 commit comments

Comments
 (0)