File tree Expand file tree Collapse file tree
grpc-gcp-java/src/main/java/com/google/cloud/grpc Expand file tree Collapse file tree Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments