Skip to content

Commit 41f67ad

Browse files
authored
Log socket lifetime when closing unusable sockets (#2258)
1 parent a9e810c commit 41f67ad

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

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

+12-5
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,18 @@ private void OnCheckSocketConnection(object? state)
253253
BalancerAddress? socketAddress;
254254
var closeSocket = false;
255255
Exception? checkException = null;
256+
DateTime? socketCreatedTime;
256257

257258
lock (Lock)
258259
{
259260
socket = _initialSocket;
260261
socketAddress = _initialSocketAddress;
262+
socketCreatedTime = _initialSocketCreatedTime;
261263

262264
if (socket != null)
263265
{
264266
CompatibilityHelpers.Assert(socketAddress != null);
267+
CompatibilityHelpers.Assert(socketCreatedTime != null);
265268

266269
closeSocket = ShouldCloseSocket(socket, socketAddress, ref _initialSocketData, out checkException);
267270
}
@@ -278,6 +281,10 @@ private void OnCheckSocketConnection(object? state)
278281

279282
if (_initialSocket == socket)
280283
{
284+
CompatibilityHelpers.Assert(socketAddress != null);
285+
CompatibilityHelpers.Assert(socketCreatedTime != null);
286+
287+
SocketConnectivitySubchannelTransportLog.ClosingUnusableSocket(_logger, _subchannel.Id, socketAddress, DateTime.UtcNow - socketCreatedTime.Value);
281288
DisconnectUnsynchronized();
282289
}
283290
}
@@ -349,7 +356,7 @@ public async ValueTask<Stream> GetStreamAsync(BalancerAddress address, Cancellat
349356
}
350357
else if (ShouldCloseSocket(socket, address, ref socketData, out _))
351358
{
352-
SocketConnectivitySubchannelTransportLog.ClosingUnusableSocketOnCreateStream(_logger, _subchannel.Id, address);
359+
SocketConnectivitySubchannelTransportLog.ClosingUnusableSocket(_logger, _subchannel.Id, address, DateTime.UtcNow - socketCreatedTime.Value);
353360
closeSocket = true;
354361
}
355362

@@ -581,8 +588,8 @@ internal static class SocketConnectivitySubchannelTransportLog
581588
private static readonly Action<ILogger, string, BalancerAddress, int, Exception?> _socketReceivingAvailable =
582589
LoggerMessage.Define<string, BalancerAddress, int>(LogLevel.Trace, new EventId(15, "SocketReceivingAvailable"), "Subchannel id '{SubchannelId}' socket {Address} is receiving {ReadBytesAvailableCount} available bytes.");
583590

584-
private static readonly Action<ILogger, string, BalancerAddress, Exception?> _closingUnusableSocketOnCreateStream =
585-
LoggerMessage.Define<string, BalancerAddress>(LogLevel.Debug, new EventId(16, "ClosingUnusableSocketOnCreateStream"), "Subchannel id '{SubchannelId}' socket {Address} is being closed because it can't be used. The socket either can't receive data or it has received unexpected data.");
591+
private static readonly Action<ILogger, string, BalancerAddress, TimeSpan, Exception?> _closingUnusableSocket =
592+
LoggerMessage.Define<string, BalancerAddress, TimeSpan>(LogLevel.Debug, new EventId(16, "ClosingUnusableSocket"), "Subchannel id '{SubchannelId}' socket {Address} is being closed because it can't be used. Socket lifetime of {SocketLifetime}. The socket either can't receive data or it has received unexpected data.");
586593

587594
private static readonly Action<ILogger, string, BalancerAddress, TimeSpan, Exception?> _closingSocketFromIdleTimeoutOnCreateStream =
588595
LoggerMessage.Define<string, BalancerAddress, TimeSpan>(LogLevel.Debug, new EventId(16, "ClosingSocketFromIdleTimeoutOnCreateStream"), "Subchannel id '{SubchannelId}' socket {Address} is being closed because it exceeds the idle timeout of {SocketIdleTimeout}.");
@@ -662,9 +669,9 @@ public static void SocketReceivingAvailable(ILogger logger, string subchannelId,
662669
_socketReceivingAvailable(logger, subchannelId, address, readBytesAvailableCount, null);
663670
}
664671

665-
public static void ClosingUnusableSocketOnCreateStream(ILogger logger, string subchannelId, BalancerAddress address)
672+
public static void ClosingUnusableSocket(ILogger logger, string subchannelId, BalancerAddress address, TimeSpan socketLifetime)
666673
{
667-
_closingUnusableSocketOnCreateStream(logger, subchannelId, address, null);
674+
_closingUnusableSocket(logger, subchannelId, address, socketLifetime, null);
668675
}
669676

670677
public static void ClosingSocketFromIdleTimeoutOnCreateStream(ILogger logger, string subchannelId, BalancerAddress address, TimeSpan socketIdleTimeout)

0 commit comments

Comments
 (0)