Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ void closeAll(boolean logErrors) {
/**
* Trim any inactive connections that have not been used since usedSince.
*/
List<PooledConnection> trim(int minSize, long usedSince, long createdSince) {
List<PooledConnection> trim(int minSize, long usedSince, long createdSince, int maxTrim) {
var trimmed = new ArrayList<PooledConnection>();
ListIterator<PooledConnection> iterator = freeBuffer.listIterator(minSize);
while (iterator.hasNext()) {
while (iterator.hasNext() && trimmed.size() < maxTrim) {
PooledConnection pooledConnection = iterator.next();
if (pooledConnection.shouldTrim(usedSince, createdSince)) {
iterator.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,13 @@ private List<PooledConnection> trimInactiveConnections(long maxInactiveMillis, l
if (freeList.size() > minSize) {
// trim on maxInactive and maxAge
long usedSince = System.currentTimeMillis() - maxInactiveMillis;
trimmedConnections = freeList.trim(minSize, usedSince, createdSince);
int excess = freeList.size() - minSize;
// Progressively reduce excess idle connections rather than closing them all at once.
int maxTrim = Math.max(1, (excess + 3) / 4);
trimmedConnections = freeList.trim(minSize, usedSince, createdSince, maxTrim);
} else if (createdSince > 0) {
// trim only on maxAge
trimmedConnections = freeList.trim(0, createdSince, createdSince);
trimmedConnections = freeList.trim(0, createdSince, createdSince, Integer.MAX_VALUE);
} else {
trimmedConnections = List.of();
}
Expand Down
Loading