Skip to content

Commit

Permalink
improve the entries list' sweep when the max usage count is changed
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <lorban@bitronix.be>
  • Loading branch information
lorban committed Dec 1, 2020
1 parent e2c4801 commit be5de28
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions jetty-util/src/main/java/org/eclipse/jetty/util/Pool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -190,18 +191,18 @@ public final void setMaxUsageCount(int maxUsageCount)
if (closed)
return;

List<Entry> copy;
// Iterate the entries, remove overused ones and collect a list of the closeable removed ones.
List<Closeable> 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);
}

/**
Expand Down

0 comments on commit be5de28

Please sign in to comment.