From be5de288ed0a13448d3183ce3b760bb07a23a370 Mon Sep 17 00:00:00 2001 From: Ludovic Orban Date: Tue, 1 Dec 2020 11:18:11 +0100 Subject: [PATCH] improve the entries list' sweep when the max usage count is changed Signed-off-by: Ludovic Orban --- .../main/java/org/eclipse/jetty/util/Pool.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/Pool.java b/jetty-util/src/main/java/org/eclipse/jetty/util/Pool.java index 86e0d5c1c499..f06c5326ed56 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/Pool.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/Pool.java @@ -29,6 +29,7 @@ import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Function; +import java.util.stream.Collectors; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.DumpableCollection; @@ -190,18 +191,18 @@ public final void setMaxUsageCount(int maxUsageCount) if (closed) return; - List copy; + // Iterate the entries, remove overused ones and collect a list of the closeable removed ones. + List copy; try (Locker.Lock l = locker.lock()) { - copy = new ArrayList<>(entries); + copy = entries.stream() + .filter(entry -> entry.isIdleAndOverUsed() && remove(entry) && entry.pooled instanceof Closeable) + .map(entry -> (Closeable)entry.pooled) + .collect(Collectors.toList()); } - // Iterate the copy and close overused entries. - for (Entry entry : copy) - { - if (entry.isIdleAndOverUsed() && remove(entry) && entry.pooled instanceof Closeable) - IO.close((Closeable)entry.pooled); - } + // Iterate the copy and close the collected entries. + copy.forEach(IO::close); } /**