From 6158c40f452cda09260e90d25acba97538441401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=B6=E6=97=A0=E4=B8=A4=E4=B8=B6?= <442367943@qq.com> Date: Fri, 10 Jan 2020 12:21:24 +0800 Subject: [PATCH] Introduce `needReport` for `ResourceLeakDetector`. (#9910) Motivation: We can extend `ResourceLeakDetector` through `ResourceLeakDetectorFactory`, and then report the leaked information by covering `reportTracedLeak` and `reportUntracedLeak`. However, the behavior of `reportTracedLeak` and `reportUntracedLeak` is controlled by `logger.isErrorEnabled()`, which is not reasonable. In the case of extending `ResourceLeakDetector`, we sometimes need `needReport` to always return true instead of relying on `logger.isErrorEnabled ()`. Modification: introduce `needReport` method and let it be `protected` Result: We can control the report leak behavior. --- .../java/io/netty/util/ResourceLeakDetector.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/io/netty/util/ResourceLeakDetector.java b/common/src/main/java/io/netty/util/ResourceLeakDetector.java index 3d73b6af2f..f3da55be38 100644 --- a/common/src/main/java/io/netty/util/ResourceLeakDetector.java +++ b/common/src/main/java/io/netty/util/ResourceLeakDetector.java @@ -256,8 +256,18 @@ public class ResourceLeakDetector { } } + /** + * When the return value is {@code true}, {@link #reportTracedLeak} and {@link #reportUntracedLeak} + * will be called once a leak is detected, otherwise not. + * + * @return {@code true} to enable leak reporting. + */ + protected boolean needReport() { + return logger.isErrorEnabled(); + } + private void reportLeak() { - if (!logger.isErrorEnabled()) { + if (!needReport()) { clearRefQueue(); return; }