Skip to content
Open
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 @@ -22,6 +22,7 @@
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
Expand Down Expand Up @@ -63,7 +64,7 @@ Instant getNextAvailableBudget() {
return Instant.now();
}

return delayedCreationTokens.get(0);
return Collections.min(delayedCreationTokens);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While Collections.min() correctly finds the earliest timestamp, it has a time complexity of O(N) for a List, where N can be up to maxConcurrentRequests (default 1000). This could become a performance bottleneck if getNextAvailableBudget() is called frequently.

Consider using a java.util.PriorityQueue for delayedCreationTokens. This would make getNextAvailableBudget() an O(1) operation (peek()), and addCreationFailure() an O(log N) operation. drainCreationFailures() could also be implemented more efficiently by repeatedly polling expired items from the head of the queue (O(k*logN) where k is the number of expired items).

}

boolean tryReserveSession() {
Expand Down Expand Up @@ -96,10 +97,6 @@ private void drainCreationFailures() {
if (iter.next().isBefore(now)) {
concurrentRequests--;
iter.remove();
} else {
// The list should be roughly sorted. Exit early when we encounter
// something expires later.
break;
}
}
}
Expand Down
Loading