Skip to content

Commit a6e28bf

Browse files
committed
address review comments
1 parent 40ff0d5 commit a6e28bf

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

grpc-gcp-java/src/main/java/com/google/cloud/grpc/GcpManagedChannel.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,18 +1720,25 @@ protected ChannelRef getChannelRefByAffinityRef(ChannelAffinityRef affinityRef)
17201720

17211721
private ChannelRef pickLeastBusyChannelDifferentFrom(@Nullable ChannelRef excludedChannelRef) {
17221722
ChannelRef channelRef = pickLeastBusyChannel(/* forFallback= */ false);
1723-
if (excludedChannelRef == null
1724-
|| channelRef != excludedChannelRef
1725-
|| !excludedChannelRef.isActive()
1726-
|| channelRefs.size() <= 1) {
1723+
if (excludedChannelRef == null || channelRefs.size() <= 1) {
17271724
return channelRef;
17281725
}
1726+
if (channelRef != excludedChannelRef && channelRef.isActive()) {
1727+
return channelRef;
1728+
}
1729+
ChannelRef leastBusyChannelRef = null;
1730+
int leastBusyStreams = Integer.MAX_VALUE;
17291731
for (ChannelRef candidate : channelRefs) {
1730-
if (candidate != excludedChannelRef) {
1731-
return candidate;
1732+
if (candidate == excludedChannelRef || !candidate.isActive()) {
1733+
continue;
1734+
}
1735+
int streams = candidate.getActiveStreamsCount();
1736+
if (leastBusyChannelRef == null || streams < leastBusyStreams) {
1737+
leastBusyChannelRef = candidate;
1738+
leastBusyStreams = streams;
17321739
}
17331740
}
1734-
return channelRef;
1741+
return leastBusyChannelRef == null ? channelRef : leastBusyChannelRef;
17351742
}
17361743

17371744
// Create a new channel and add it to channelRefs.

0 commit comments

Comments
 (0)