This is an automated email from the ASF dual-hosted git repository.

songxiaosheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-spi-extensions.git


The following commit(s) were added to refs/heads/master by this push:
     new b3033de  fix: dubbo-registry-dns resource leak (#308)
b3033de is described below

commit b3033debb4e0277ad865b8b78f54652e24af9100
Author: aofall <[email protected]>
AuthorDate: Wed Apr 10 20:29:34 2024 +0800

    fix: dubbo-registry-dns resource leak (#308)
    
    * fix: dubbo-registry-dns memory leak && add try catch
    
    * revert junit
---
 .../org/apache/dubbo/registry/dns/DNSServiceDiscovery.java    | 11 +++++++++--
 .../java/org/apache/dubbo/registry/dns/util/DNSResolver.java  |  1 +
 .../apache/dubbo/registry/dns/{util => }/DNSResolverTest.java |  8 +++++---
 3 files changed, 15 insertions(+), 5 deletions(-)

diff --git 
a/dubbo-registry-extensions/dubbo-registry-dns/src/main/java/org/apache/dubbo/registry/dns/DNSServiceDiscovery.java
 
b/dubbo-registry-extensions/dubbo-registry-dns/src/main/java/org/apache/dubbo/registry/dns/DNSServiceDiscovery.java
index 3c26786..72224ba 100644
--- 
a/dubbo-registry-extensions/dubbo-registry-dns/src/main/java/org/apache/dubbo/registry/dns/DNSServiceDiscovery.java
+++ 
b/dubbo-registry-extensions/dubbo-registry-dns/src/main/java/org/apache/dubbo/registry/dns/DNSServiceDiscovery.java
@@ -42,6 +42,7 @@ import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 
 public class DNSServiceDiscovery extends ReflectionBasedServiceDiscovery {
+
     private static final Logger logger = 
LoggerFactory.getLogger(DNSServiceDiscovery.class);
 
     /**
@@ -111,7 +112,13 @@ public class DNSServiceDiscovery extends 
ReflectionBasedServiceDiscovery {
     public void 
addServiceInstancesChangedListener(ServiceInstancesChangedListener listener) 
throws NullPointerException, IllegalArgumentException {
         listener.getServiceNames().forEach(serviceName -> {
             ScheduledFuture<?> scheduledFuture = 
pollingExecutorService.scheduleAtFixedRate(() -> {
-                    List<ServiceInstance> instances = 
getInstances(serviceName);
+                    List<ServiceInstance> instances;
+                    try {
+                        instances = getInstances(serviceName);
+                    } catch (Throwable throwable) {
+                        logger.error("Failed to get instances for " + 
serviceName, throwable);
+                        return;
+                    }
                     
instances.sort(Comparator.comparingInt(ServiceInstance::hashCode));
                     notifyListener(serviceName, listener, instances);
                 },
@@ -133,7 +140,7 @@ public class DNSServiceDiscovery extends 
ReflectionBasedServiceDiscovery {
 
         int port;
 
-        if (resolveResult.getPort().size() > 0) {
+        if (!resolveResult.getPort().isEmpty()) {
             // use first as default
             port = resolveResult.getPort().get(0);
         } else {
diff --git 
a/dubbo-registry-extensions/dubbo-registry-dns/src/main/java/org/apache/dubbo/registry/dns/util/DNSResolver.java
 
b/dubbo-registry-extensions/dubbo-registry-dns/src/main/java/org/apache/dubbo/registry/dns/util/DNSResolver.java
index eb520d2..0bbd866 100644
--- 
a/dubbo-registry-extensions/dubbo-registry-dns/src/main/java/org/apache/dubbo/registry/dns/util/DNSResolver.java
+++ 
b/dubbo-registry-extensions/dubbo-registry-dns/src/main/java/org/apache/dubbo/registry/dns/util/DNSResolver.java
@@ -81,6 +81,7 @@ public class DNSResolver {
                     // Port
                     int port = buf.readUnsignedShort();
                     recordList.getPort().add(port);
+                    buf.release();
                 }
 
             } catch (InterruptedException e) {
diff --git 
a/dubbo-registry-extensions/dubbo-registry-dns/src/test/java/org/apache/dubbo/registry/dns/util/DNSResolverTest.java
 
b/dubbo-registry-extensions/dubbo-registry-dns/src/test/java/org/apache/dubbo/registry/dns/DNSResolverTest.java
similarity index 77%
rename from 
dubbo-registry-extensions/dubbo-registry-dns/src/test/java/org/apache/dubbo/registry/dns/util/DNSResolverTest.java
rename to 
dubbo-registry-extensions/dubbo-registry-dns/src/test/java/org/apache/dubbo/registry/dns/DNSResolverTest.java
index 17088ac..d3911a4 100644
--- 
a/dubbo-registry-extensions/dubbo-registry-dns/src/test/java/org/apache/dubbo/registry/dns/util/DNSResolverTest.java
+++ 
b/dubbo-registry-extensions/dubbo-registry-dns/src/test/java/org/apache/dubbo/registry/dns/DNSResolverTest.java
@@ -14,8 +14,10 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.dubbo.registry.dns.util;
+package org.apache.dubbo.registry.dns;
 
+import org.apache.dubbo.registry.dns.util.DNSResolver;
+import org.apache.dubbo.registry.dns.util.ResolveResult;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
@@ -24,7 +26,7 @@ public class DNSResolverTest {
     @Test
     public void testResolve() {
         DNSResolver dnsResolver = new DNSResolver("8.8.8.8", 53, 1);
-        ResolveResult resolve = dnsResolver.resolve("aliyun.com");
-        Assertions.assertTrue(resolve.getHostnameList().size() > 0);
+        ResolveResult resolve = dnsResolver.resolve("dubbo.apache.org");
+        Assertions.assertFalse(resolve.getHostnameList().isEmpty());
     }
 }

Reply via email to