Skip to content

Commit b9a7c2f

Browse files
committed
Merge pull request #28875 from polarbear567
* pr/28875: Polish "Add server.netty.max-keep-alive-requests" Add server.netty.max-keep-alive-requests Closes gh-28875
2 parents 578855f + 076ddc8 commit b9a7c2f

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -1340,6 +1340,12 @@ public static class Netty {
13401340
*/
13411341
private DataSize maxInitialLineLength = DataSize.ofKilobytes(4);
13421342

1343+
/**
1344+
* Maximum number of requests that can be made per connection. By default, a
1345+
* connection serves unlimited number of requests.
1346+
*/
1347+
private Integer maxKeepAliveRequests;
1348+
13431349
/**
13441350
* Whether to validate headers when decoding requests.
13451351
*/
@@ -1391,6 +1397,14 @@ public void setMaxInitialLineLength(DataSize maxInitialLineLength) {
13911397
this.maxInitialLineLength = maxInitialLineLength;
13921398
}
13931399

1400+
public Integer getMaxKeepAliveRequests() {
1401+
return this.maxKeepAliveRequests;
1402+
}
1403+
1404+
public void setMaxKeepAliveRequests(Integer maxKeepAliveRequests) {
1405+
this.maxKeepAliveRequests = maxKeepAliveRequests;
1406+
}
1407+
13941408
public boolean isValidateHeaders() {
13951409
return this.validateHeaders;
13961410
}

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-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -62,6 +62,8 @@ public void customize(NettyReactiveWebServerFactory factory) {
6262
.to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout));
6363
propertyMapper.from(nettyProperties::getIdleTimeout).whenNonNull()
6464
.to((idleTimeout) -> customizeIdleTimeout(factory, idleTimeout));
65+
propertyMapper.from(nettyProperties::getMaxKeepAliveRequests)
66+
.to((maxKeepAliveRequests) -> customizeMaxKeepAliveRequests(factory, maxKeepAliveRequests));
6567
customizeRequestDecoder(factory, propertyMapper);
6668
}
6769

@@ -104,4 +106,8 @@ private void customizeIdleTimeout(NettyReactiveWebServerFactory factory, Duratio
104106
factory.addServerCustomizers((httpServer) -> httpServer.idleTimeout(idleTimeout));
105107
}
106108

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

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -339,6 +339,12 @@ void testCustomizeNettyIdleTimeout() {
339339
assertThat(this.properties.getNetty().getIdleTimeout()).isEqualTo(Duration.ofSeconds(10));
340340
}
341341

342+
@Test
343+
void testCustomizeNettyMaxKeepAliveRequests() {
344+
bind("server.netty.max-keep-alive-requests", "100");
345+
assertThat(this.properties.getNetty().getMaxKeepAliveRequests()).isEqualTo(100);
346+
}
347+
342348
@Test
343349
void tomcatAcceptCountMatchesProtocolDefault() throws Exception {
344350
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: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -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)