From b4b6a3c5de01740b54e55f32d4def230c2fc3c32 Mon Sep 17 00:00:00 2001 From: Sebastian Schepens Date: Mon, 30 Jan 2017 11:46:01 -0300 Subject: [PATCH] fix file descriptor leak on LinuxBrokerHostUsageImpl --- .../loadbalance/impl/LinuxBrokerHostUsageImpl.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pulsar-broker/src/main/java/com/yahoo/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java b/pulsar-broker/src/main/java/com/yahoo/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java index 5c7e5457a0980..31d6e06d6a19d 100644 --- a/pulsar-broker/src/main/java/com/yahoo/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java +++ b/pulsar-broker/src/main/java/com/yahoo/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java @@ -33,6 +33,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Class that will return the broker host usage. @@ -120,8 +121,8 @@ private double getTotalCpuLimit() { * this would include iowait, irq and steal. */ private CpuStat getTotalCpuUsage() { - try { - String[] words = Files.lines(Paths.get("/proc/stat")) + try (Stream stream = Files.lines(Paths.get("/proc/stat"))) { + String[] words = stream .findFirst() .get().split("\\s+"); @@ -146,8 +147,8 @@ private ResourceUsage getMemUsage() { } private List getNics() { - try { - return Files.list(Paths.get("/sys/class/net/")) + try (Stream stream = Files.list(Paths.get("/sys/class/net/"))) { + return stream .filter(this::isPhysicalNic) .map(path -> path.getFileName().toString()) .collect(Collectors.toList());