diff --git a/src/Grpc.Net.Client/Balancer/Internal/ConnectionManager.cs b/src/Grpc.Net.Client/Balancer/Internal/ConnectionManager.cs index d19670674..d0084db24 100644 --- a/src/Grpc.Net.Client/Balancer/Internal/ConnectionManager.cs +++ b/src/Grpc.Net.Client/Balancer/Internal/ConnectionManager.cs @@ -316,7 +316,7 @@ public void UpdateState(BalancerState state) if (address != null) { - ConnectionManagerLog.PickResultSuccessful(Logger, subchannel.Id, address); + ConnectionManagerLog.PickResultSuccessful(Logger, subchannel.Id, address, subchannel.Transport.TransportStatus); return (subchannel, address, result.SubchannelCallTracker); } else @@ -478,8 +478,8 @@ internal static class ConnectionManagerLog private static readonly Action _pickStarted = LoggerMessage.Define(LogLevel.Trace, new EventId(5, "PickStarted"), "Pick started."); - private static readonly Action _pickResultSuccessful = - LoggerMessage.Define(LogLevel.Debug, new EventId(6, "PickResultSuccessful"), "Successfully picked subchannel id '{SubchannelId}' with address {CurrentAddress}."); + private static readonly Action _pickResultSuccessful = + LoggerMessage.Define(LogLevel.Debug, new EventId(6, "PickResultSuccessful"), "Successfully picked subchannel id '{SubchannelId}' with address {CurrentAddress}. Transport status: {TransportStatus}"); private static readonly Action _pickResultSubchannelNoCurrentAddress = LoggerMessage.Define(LogLevel.Debug, new EventId(7, "PickResultSubchannelNoCurrentAddress"), "Picked subchannel id '{SubchannelId}' doesn't have a current address."); @@ -528,9 +528,9 @@ public static void PickStarted(ILogger logger) _pickStarted(logger, null); } - public static void PickResultSuccessful(ILogger logger, string subchannelId, BalancerAddress currentAddress) + public static void PickResultSuccessful(ILogger logger, string subchannelId, BalancerAddress currentAddress, TransportStatus transportStatus) { - _pickResultSuccessful(logger, subchannelId, currentAddress, null); + _pickResultSuccessful(logger, subchannelId, currentAddress, transportStatus, null); } public static void PickResultSubchannelNoCurrentAddress(ILogger logger, string subchannelId) diff --git a/src/Grpc.Net.Client/Balancer/Internal/ISubchannelTransport.cs b/src/Grpc.Net.Client/Balancer/Internal/ISubchannelTransport.cs index ac9f468c5..82ed0f9f3 100644 --- a/src/Grpc.Net.Client/Balancer/Internal/ISubchannelTransport.cs +++ b/src/Grpc.Net.Client/Balancer/Internal/ISubchannelTransport.cs @@ -30,21 +30,22 @@ internal interface ISubchannelTransport : IDisposable { BalancerAddress? CurrentAddress { get; } TimeSpan? ConnectTimeout { get; } + TransportStatus TransportStatus { get; } -#if NET5_0_OR_GREATER ValueTask GetStreamAsync(BalancerAddress address, CancellationToken cancellationToken); -#endif - -#if !NETSTANDARD2_0 && !NET462 - ValueTask -#else - Task -#endif - TryConnectAsync(ConnectContext context); + ValueTask TryConnectAsync(ConnectContext context); void Disconnect(); } +internal enum TransportStatus +{ + NotConnected, + Passive, + InitialSocket, + ActiveStream +} + internal enum ConnectResult { Success, diff --git a/src/Grpc.Net.Client/Balancer/Internal/PassiveSubchannelTransport.cs b/src/Grpc.Net.Client/Balancer/Internal/PassiveSubchannelTransport.cs index f7bd0b047..06c0ea5ba 100644 --- a/src/Grpc.Net.Client/Balancer/Internal/PassiveSubchannelTransport.cs +++ b/src/Grpc.Net.Client/Balancer/Internal/PassiveSubchannelTransport.cs @@ -44,6 +44,7 @@ public PassiveSubchannelTransport(Subchannel subchannel) public BalancerAddress? CurrentAddress => _currentAddress; public TimeSpan? ConnectTimeout { get; } + public TransportStatus TransportStatus => TransportStatus.Passive; public void Disconnect() { @@ -51,13 +52,7 @@ public void Disconnect() _subchannel.UpdateConnectivityState(ConnectivityState.Idle, "Disconnected."); } - public -#if !NETSTANDARD2_0 && !NET462 - ValueTask -#else - Task -#endif - TryConnectAsync(ConnectContext context) + public ValueTask TryConnectAsync(ConnectContext context) { Debug.Assert(_subchannel._addresses.Count == 1); Debug.Assert(CurrentAddress == null); @@ -68,11 +63,7 @@ public void Disconnect() _currentAddress = currentAddress; _subchannel.UpdateConnectivityState(ConnectivityState.Ready, "Passively connected."); -#if !NETSTANDARD2_0 && !NET462 return new ValueTask(ConnectResult.Success); -#else - return Task.FromResult(ConnectResult.Success); -#endif } public void Dispose() @@ -80,11 +71,9 @@ public void Dispose() _currentAddress = null; } -#if NET5_0_OR_GREATER public ValueTask GetStreamAsync(BalancerAddress address, CancellationToken cancellationToken) { throw new NotSupportedException(); } -#endif } #endif diff --git a/src/Grpc.Net.Client/Balancer/Internal/SocketConnectivitySubchannelTransport.cs b/src/Grpc.Net.Client/Balancer/Internal/SocketConnectivitySubchannelTransport.cs index 390c852d7..4e8216cf2 100644 --- a/src/Grpc.Net.Client/Balancer/Internal/SocketConnectivitySubchannelTransport.cs +++ b/src/Grpc.Net.Client/Balancer/Internal/SocketConnectivitySubchannelTransport.cs @@ -82,6 +82,24 @@ public SocketConnectivitySubchannelTransport( private object Lock => _subchannel.Lock; public BalancerAddress? CurrentAddress => _currentAddress; public TimeSpan? ConnectTimeout { get; } + public TransportStatus TransportStatus + { + get + { + lock (Lock) + { + if (_initialSocket != null) + { + return TransportStatus.InitialSocket; + } + if (_activeStreams.Count > 0) + { + return TransportStatus.ActiveStream; + } + return TransportStatus.NotConnected; + } + } + } // For testing. Take a copy under lock for thread-safety. internal IReadOnlyList GetActiveStreams() diff --git a/test/Grpc.Net.Client.Tests/Infrastructure/Balancer/TestSubChannelTransport.cs b/test/Grpc.Net.Client.Tests/Infrastructure/Balancer/TestSubChannelTransport.cs index f831c8682..67543a5fb 100644 --- a/test/Grpc.Net.Client.Tests/Infrastructure/Balancer/TestSubChannelTransport.cs +++ b/test/Grpc.Net.Client.Tests/Infrastructure/Balancer/TestSubChannelTransport.cs @@ -1,4 +1,4 @@ -#region Copyright notice and license +#region Copyright notice and license // Copyright 2019 The gRPC Authors // @@ -40,6 +40,7 @@ internal class TestSubchannelTransport : ISubchannelTransport public BalancerAddress? CurrentAddress { get; private set; } public TimeSpan? ConnectTimeout => _factory.ConnectTimeout; + public TransportStatus TransportStatus => TransportStatus.Passive; public Task TryConnectTask => _connectTcs.Task;