Skip to content

Commit fe1011c

Browse files
committed
Add Netty memory leak detection config property
This commit adds a new `spring.netty.leak-detection` configuration property that selects the level of memory leak detection for the Netty engine. This configuration is applied statically to Netty; this means all (non-shaded) Netty usages as client or server will be impacted by this change. Developers might use this property during development or tests to find causes of memory leaks when dealing with Netty buffers. Closes gh-14338
1 parent 66e857f commit fe1011c

File tree

7 files changed

+186
-1
lines changed

7 files changed

+186
-1
lines changed

buildSrc/src/main/java/org/springframework/boot/build/context/properties/DocumentConfigurationProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void documentConfigurationProperties() throws IOException {
7272
.addSection("mail").withKeyPrefixes("spring.mail", "spring.sendgrid").addSection("cache")
7373
.withKeyPrefixes("spring.cache").addSection("server").withKeyPrefixes("server").addSection("web")
7474
.withKeyPrefixes("spring.hateoas", "spring.http", "spring.servlet", "spring.jersey", "spring.mvc",
75-
"spring.resources", "spring.session", "spring.web", "spring.webflux")
75+
"spring.netty", "spring.resources", "spring.session", "spring.web", "spring.webflux")
7676
.addSection("json").withKeyPrefixes("spring.jackson", "spring.gson").addSection("rsocket")
7777
.withKeyPrefixes("spring.rsocket").addSection("templating")
7878
.withKeyPrefixes("spring.freemarker", "spring.groovy", "spring.mustache", "spring.thymeleaf")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.netty;
18+
19+
import io.netty.util.NettyRuntime;
20+
import io.netty.util.ResourceLeakDetector;
21+
22+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
24+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
25+
import org.springframework.context.annotation.Configuration;
26+
27+
/**
28+
* {@link EnableAutoConfiguration Auto-configuration} for Netty.
29+
*
30+
* @author Brian Clozel
31+
* @since 2.5.0
32+
*/
33+
@Configuration(proxyBeanMethods = false)
34+
@ConditionalOnClass(NettyRuntime.class)
35+
@EnableConfigurationProperties(NettyProperties.class)
36+
public class NettyAutoConfiguration {
37+
38+
public NettyAutoConfiguration(NettyProperties properties) {
39+
if (properties.getLeakDetection() != null) {
40+
NettyProperties.LeakDetection leakDetection = properties.getLeakDetection();
41+
ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.valueOf(leakDetection.name()));
42+
}
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.netty;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* {@link ConfigurationProperties @ConfigurationProperties} for the Netty engine.
23+
* <p>
24+
* These properties apply globally to the Netty library, used as a client or a server.
25+
*
26+
* @author Brian Clozel
27+
* @since 2.5.0
28+
*/
29+
@ConfigurationProperties(prefix = "spring.netty")
30+
public class NettyProperties {
31+
32+
/**
33+
* Level of leak detection for reference-counted buffers.
34+
*/
35+
private LeakDetection leakDetection = LeakDetection.DISABLED;
36+
37+
public LeakDetection getLeakDetection() {
38+
return this.leakDetection;
39+
}
40+
41+
public void setLeakDetection(LeakDetection leakDetection) {
42+
this.leakDetection = leakDetection;
43+
}
44+
45+
public enum LeakDetection {
46+
47+
/**
48+
* Disable leak detection completely.
49+
*/
50+
DISABLED,
51+
52+
/**
53+
* Detect leaks for 1% of buffers.
54+
*/
55+
SIMPLE,
56+
57+
/**
58+
* Detect leaks for 1% of buffers and track where they were accessed.
59+
*/
60+
ADVANCED,
61+
62+
/**
63+
* Detect leaks for 100% of buffers and track where they were accessed.
64+
*/
65+
PARANOID
66+
67+
}
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Auto-configuration for the Netty library.
19+
*/
20+
package org.springframework.boot.autoconfigure.netty;

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,10 @@
17701770
"name": "spring.webservices.wsdl-locations",
17711771
"type": "java.util.List<java.lang.String>",
17721772
"description": "Comma-separated list of locations of WSDLs and accompanying XSDs to be exposed as beans."
1773+
},
1774+
{
1775+
"name": "spring.netty.leak-detection",
1776+
"defaultValue": "disabled"
17731777
}
17741778
],
17751779
"hints": [

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
105105
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\
106106
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
107107
org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration,\
108+
org.springframework.boot.autoconfigure.netty.NettyAutoConfiguration,\
108109
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
109110
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
110111
org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.netty;
18+
19+
import io.netty.util.ResourceLeakDetector;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.boot.autoconfigure.AutoConfigurations;
23+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* Tests for {@link NettyAutoConfiguration}.
29+
*
30+
* @author Brian Clozel
31+
*/
32+
public class NettyAutoConfigurationTests {
33+
34+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
35+
.withConfiguration(AutoConfigurations.of(NettyAutoConfiguration.class));
36+
37+
@Test
38+
void leakDetectionShouldBeConfigured() {
39+
this.contextRunner.withPropertyValues("spring.netty.leak-detection=paranoid").run((context) -> {
40+
assertThat(ResourceLeakDetector.getLevel()).isEqualTo(ResourceLeakDetector.Level.PARANOID);
41+
// reset configuration for the following tests.
42+
ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.DISABLED);
43+
});
44+
}
45+
46+
}

0 commit comments

Comments
 (0)