Skip to content

Commit 1416340

Browse files
authored
Fix flaky ConnectTimeout_MultipleCalls_AttemptReconnect test (#2460)
1 parent 64f6a09 commit 1416340

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/Grpc.Net.Client/Balancer/Internal/ConnectionManager.cs

+19-3
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,20 @@ public void UpdateState(BalancerState state)
312312
{
313313
case PickResultType.Complete:
314314
var subchannel = result.Subchannel!;
315-
var address = subchannel.CurrentAddress;
315+
var (address, state) = subchannel.GetAddressAndState();
316316

317317
if (address != null)
318318
{
319-
ConnectionManagerLog.PickResultSuccessful(Logger, subchannel.Id, address, subchannel.Transport.TransportStatus);
320-
return (subchannel, address, result.SubchannelCallTracker);
319+
if (state == ConnectivityState.Ready)
320+
{
321+
ConnectionManagerLog.PickResultSuccessful(Logger, subchannel.Id, address, subchannel.Transport.TransportStatus);
322+
return (subchannel, address, result.SubchannelCallTracker);
323+
}
324+
else
325+
{
326+
ConnectionManagerLog.PickResultSubchannelNotReady(Logger, subchannel.Id, address, state);
327+
previousPicker = currentPicker;
328+
}
321329
}
322330
else
323331
{
@@ -499,6 +507,9 @@ internal static class ConnectionManagerLog
499507
private static readonly Action<ILogger, Status, Exception?> _resolverServiceConfigFallback =
500508
LoggerMessage.Define<Status>(LogLevel.Debug, new EventId(12, "ResolverServiceConfigFallback"), "Falling back to previously loaded service config. Resolver failure when retreiving or parsing service config with status: {Status}");
501509

510+
private static readonly Action<ILogger, string, BalancerAddress, ConnectivityState, Exception?> _pickResultSubchannelNotReady =
511+
LoggerMessage.Define<string, BalancerAddress, ConnectivityState>(LogLevel.Debug, new EventId(13, "PickResultSubchannelNotReady"), "Picked subchannel id '{SubchannelId}' with address {CurrentAddress} doesn't have a ready state. Subchannel state: {State}");
512+
502513
public static void ResolverUnsupportedLoadBalancingConfig(ILogger logger, IList<LoadBalancingConfig> loadBalancingConfigs)
503514
{
504515
if (logger.IsEnabled(LogLevel.Warning))
@@ -562,5 +573,10 @@ public static void ResolverServiceConfigFallback(ILogger logger, Status status)
562573
{
563574
_resolverServiceConfigFallback(logger, status, null);
564575
}
576+
577+
public static void PickResultSubchannelNotReady(ILogger logger, string subchannelId, BalancerAddress currentAddress, ConnectivityState state)
578+
{
579+
_pickResultSubchannelNotReady(logger, subchannelId, currentAddress, state, null);
580+
}
565581
}
566582
#endif

src/Grpc.Net.Client/Balancer/Subchannel.cs

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public BalancerAddress? CurrentAddress
8686
/// </summary>
8787
public BalancerAttributes Attributes { get; }
8888

89+
internal (BalancerAddress? Address, ConnectivityState State) GetAddressAndState()
90+
{
91+
lock (Lock)
92+
{
93+
return (CurrentAddress, State);
94+
}
95+
}
96+
8997
internal Subchannel(ConnectionManager manager, IReadOnlyList<BalancerAddress> addresses)
9098
{
9199
Lock = new object();

0 commit comments

Comments
 (0)