-
Notifications
You must be signed in to change notification settings - Fork 815
Add additional connection and request logging to client #2047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JamesNK
merged 5 commits into
grpc:master
from
JamesNK:jamesnk/additional-connection-logging
Feb 13, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,10 +80,9 @@ public SocketConnectivitySubchannelTransport( | |
| _socketConnectedTimer = new Timer(OnCheckSocketConnection, state: null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); | ||
| } | ||
|
|
||
| public object Lock => _subchannel.Lock; | ||
| private object Lock => _subchannel.Lock; | ||
| public BalancerAddress? CurrentAddress => _currentAddress; | ||
| public TimeSpan? ConnectTimeout { get; } | ||
| public bool HasStream { get; } | ||
|
|
||
| // For testing. Take a copy under lock for thread-safety. | ||
| internal IReadOnlyList<ActiveStream> GetActiveStreams() | ||
|
|
@@ -264,13 +263,21 @@ public async ValueTask<Stream> GetStreamAsync(BalancerAddress address, Cancellat | |
| Socket? socket = null; | ||
| lock (Lock) | ||
| { | ||
| if (_initialSocket != null && | ||
| _initialSocketAddress != null && | ||
| Equals(_initialSocketAddress, address)) | ||
| if (_initialSocket != null) | ||
| { | ||
| var socketAddressMatch = Equals(_initialSocketAddress, address); | ||
|
|
||
| socket = _initialSocket; | ||
| _initialSocket = null; | ||
| _initialSocketAddress = null; | ||
|
|
||
| // Double check the address matches the socket address and only use socket on match. | ||
| // Not sure if this is possible in practice, but better safe than sorry. | ||
| if (!socketAddressMatch) | ||
| { | ||
| socket.Dispose(); | ||
| socket = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -288,6 +295,8 @@ public async ValueTask<Stream> GetStreamAsync(BalancerAddress address, Cancellat | |
|
|
||
| if (socket == null) | ||
| { | ||
| SocketConnectivitySubchannelTransportLog.ConnectingOnCreateStream(_logger, _subchannel.Id, address); | ||
|
|
||
| socket = new Socket(SocketType.Stream, ProtocolType.Tcp) { NoDelay = true }; | ||
| await socket.ConnectAsync(address.EndPoint, cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
@@ -300,6 +309,7 @@ public async ValueTask<Stream> GetStreamAsync(BalancerAddress address, Cancellat | |
| lock (Lock) | ||
| { | ||
| _activeStreams.Add(new ActiveStream(address, socket, stream)); | ||
| SocketConnectivitySubchannelTransportLog.StreamCreated(_logger, _subchannel.Id, address, _activeStreams.Count); | ||
| } | ||
|
|
||
| return stream; | ||
|
|
@@ -331,7 +341,7 @@ private void OnStreamDisposed(Stream streamWrapper) | |
| if (t.Stream == streamWrapper) | ||
| { | ||
| _activeStreams.RemoveAt(i); | ||
| SocketConnectivitySubchannelTransportLog.DisposingStream(_logger, _subchannel.Id, t.Address); | ||
| SocketConnectivitySubchannelTransportLog.DisposingStream(_logger, _subchannel.Id, t.Address, _activeStreams.Count); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmm, maybe ignore my last comment about lock; I can't see a nice / clean way of avoiding this one, so... if we're in that situation anyway, maybe just live with it |
||
|
|
||
| // If the last active streams is removed then there is no active connection. | ||
| disconnect = _activeStreams.Count == 0; | ||
|
|
@@ -399,15 +409,21 @@ internal static class SocketConnectivitySubchannelTransportLog | |
| private static readonly Action<ILogger, int, BalancerAddress, Exception?> _creatingStream = | ||
| LoggerMessage.Define<int, BalancerAddress>(LogLevel.Trace, new EventId(7, "CreatingStream"), "Subchannel id '{SubchannelId}' creating stream for {Address}."); | ||
|
|
||
| private static readonly Action<ILogger, int, BalancerAddress, Exception?> _disposingStream = | ||
| LoggerMessage.Define<int, BalancerAddress>(LogLevel.Trace, new EventId(8, "DisposingStream"), "Subchannel id '{SubchannelId}' disposing stream for {Address}."); | ||
| private static readonly Action<ILogger, int, BalancerAddress, int, Exception?> _disposingStream = | ||
| LoggerMessage.Define<int, BalancerAddress, int>(LogLevel.Trace, new EventId(8, "DisposingStream"), "Subchannel id '{SubchannelId}' disposing stream for {Address}. Transport has {ActiveStreams} active streams."); | ||
|
|
||
| private static readonly Action<ILogger, int, Exception?> _disposingTransport = | ||
| LoggerMessage.Define<int>(LogLevel.Trace, new EventId(9, "DisposingTransport"), "Subchannel id '{SubchannelId}' disposing transport."); | ||
|
|
||
| private static readonly Action<ILogger, int, Exception> _errorOnDisposingStream = | ||
| LoggerMessage.Define<int>(LogLevel.Error, new EventId(10, "ErrorOnDisposingStream"), "Subchannel id '{SubchannelId}' unexpected error when reacting to transport stream dispose."); | ||
|
|
||
| private static readonly Action<ILogger, int, BalancerAddress, Exception?> _connectingOnCreateStream = | ||
| LoggerMessage.Define<int, BalancerAddress>(LogLevel.Trace, new EventId(11, "ConnectingOnCreateStream"), "Subchannel id '{SubchannelId}' doesn't have a connected socket available. Connecting new stream socket for {Address}."); | ||
|
|
||
| private static readonly Action<ILogger, int, BalancerAddress, int, Exception?> _streamCreated = | ||
| LoggerMessage.Define<int, BalancerAddress, int>(LogLevel.Trace, new EventId(12, "StreamCreated"), "Subchannel id '{SubchannelId}' created stream for {Address}. Transport has {ActiveStreams} active streams."); | ||
|
|
||
| public static void ConnectingSocket(ILogger logger, int subchannelId, BalancerAddress address) | ||
| { | ||
| _connectingSocket(logger, subchannelId, address, null); | ||
|
|
@@ -443,9 +459,9 @@ public static void CreatingStream(ILogger logger, int subchannelId, BalancerAddr | |
| _creatingStream(logger, subchannelId, address, null); | ||
| } | ||
|
|
||
| public static void DisposingStream(ILogger logger, int subchannelId, BalancerAddress address) | ||
| public static void DisposingStream(ILogger logger, int subchannelId, BalancerAddress address, int activeStreams) | ||
| { | ||
| _disposingStream(logger, subchannelId, address, null); | ||
| _disposingStream(logger, subchannelId, address, activeStreams, null); | ||
| } | ||
|
|
||
| public static void DisposingTransport(ILogger logger, int subchannelId) | ||
|
|
@@ -457,6 +473,16 @@ public static void ErrorOnDisposingStream(ILogger logger, int subchannelId, Exce | |
| { | ||
| _errorOnDisposingStream(logger, subchannelId, ex); | ||
| } | ||
|
|
||
| public static void ConnectingOnCreateStream(ILogger logger, int subchannelId, BalancerAddress address) | ||
| { | ||
| _connectingOnCreateStream(logger, subchannelId, address, null); | ||
| } | ||
|
|
||
| public static void StreamCreated(ILogger logger, int subchannelId, BalancerAddress address, int activeStreams) | ||
| { | ||
| _streamCreated(logger, subchannelId, address, activeStreams, null); | ||
| } | ||
| } | ||
| #endif | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logging inside the lock troubles me; is this intentional? would it perhaps be better to snag the
Countinside the lock and log outside?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is a concern. There is logging inside locks throughout gRPC and ASP.NET Core.