Skip to content

Commit 127213d

Browse files
committed
Clean up
1 parent 5452586 commit 127213d

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,28 +115,29 @@ private sealed class StateChangedRegistration : IDisposable
115115
{
116116
private readonly Subchannel _subchannel;
117117
private readonly Action<SubchannelState> _callback;
118-
private readonly string _registrationId;
118+
119+
public string RegistrationId { get; }
119120

120121
public StateChangedRegistration(Subchannel subchannel, Action<SubchannelState> callback)
121122
{
122123
_subchannel = subchannel;
123124
_callback = callback;
124-
_registrationId = subchannel.GetNextRegistrationId();
125+
RegistrationId = subchannel.GetNextRegistrationId();
125126

126-
SubchannelLog.StateChangedRegistrationCreated(_subchannel._logger, _subchannel.Id, _registrationId);
127+
SubchannelLog.StateChangedRegistrationCreated(_subchannel._logger, _subchannel.Id, RegistrationId);
127128
}
128129

129130
public void Invoke(SubchannelState state)
130131
{
131-
SubchannelLog.ExecutingStateChangedRegistration(_subchannel._logger, _subchannel.Id, _registrationId);
132+
SubchannelLog.ExecutingStateChangedRegistration(_subchannel._logger, _subchannel.Id, RegistrationId);
132133
_callback(state);
133134
}
134135

135136
public void Dispose()
136137
{
137138
if (_subchannel.RemoveStateChanged(this))
138139
{
139-
SubchannelLog.StateChangedRegistrationDisposed(_subchannel._logger, _subchannel.Id, _registrationId);
140+
SubchannelLog.StateChangedRegistrationRemoved(_subchannel._logger, _subchannel.Id, RegistrationId);
140141
}
141142
}
142143
}
@@ -401,11 +402,12 @@ public IReadOnlyList<BalancerAddress> GetAddresses()
401402
public void Dispose()
402403
{
403404
UpdateConnectivityState(ConnectivityState.Shutdown, "Subchannel disposed.");
404-
for (var i = _stateChangedRegistrations.Count - 1; i >= 0; i--)
405+
406+
foreach (var registration in _stateChangedRegistrations)
405407
{
406-
_stateChangedRegistrations[i].Dispose();
408+
SubchannelLog.StateChangedRegistrationRemoved(_logger, Id, registration.RegistrationId);
407409
}
408-
Debug.Assert(_stateChangedRegistrations.Count == 0, "Registrations should be removed from collection on dispose.");
410+
_stateChangedRegistrations.Clear();
409411

410412
CancelInProgressConnect();
411413
Transport.Dispose();
@@ -450,8 +452,8 @@ internal static class SubchannelLog
450452
private static readonly Action<ILogger, int, string, Exception?> _stateChangedRegistrationCreated =
451453
LoggerMessage.Define<int, string>(LogLevel.Trace, new EventId(12, "StateChangedRegistrationCreated"), "Subchannel id '{SubchannelId}' state changed registration '{RegistrationId}' created.");
452454

453-
private static readonly Action<ILogger, int, string, Exception?> _stateChangedRegistrationDisposed =
454-
LoggerMessage.Define<int, string>(LogLevel.Trace, new EventId(13, "StateChangedRegistrationDisposed"), "Subchannel id '{SubchannelId}' state changed registration '{RegistrationId}' disposed.");
455+
private static readonly Action<ILogger, int, string, Exception?> _stateChangedRegistrationRemoved =
456+
LoggerMessage.Define<int, string>(LogLevel.Trace, new EventId(13, "StateChangedRegistrationRemoved"), "Subchannel id '{SubchannelId}' state changed registration '{RegistrationId}' removed.");
455457

456458
private static readonly Action<ILogger, int, string, Exception?> _executingStateChangedRegistration =
457459
LoggerMessage.Define<int, string>(LogLevel.Trace, new EventId(14, "ExecutingStateChangedRegistration"), "Subchannel id '{SubchannelId}' executing state changed registration '{RegistrationId}'.");
@@ -536,9 +538,9 @@ public static void StateChangedRegistrationCreated(ILogger logger, int subchanne
536538
_stateChangedRegistrationCreated(logger, subchannelId, registrationId, null);
537539
}
538540

539-
public static void StateChangedRegistrationDisposed(ILogger logger, int subchannelId, string registrationId)
541+
public static void StateChangedRegistrationRemoved(ILogger logger, int subchannelId, string registrationId)
540542
{
541-
_stateChangedRegistrationDisposed(logger, subchannelId, registrationId, null);
543+
_stateChangedRegistrationRemoved(logger, subchannelId, registrationId, null);
542544
}
543545

544546
public static void SubchannelPreserved(ILogger logger, int subchannelId, BalancerAddress address)

test/FunctionalTests/Balancer/BalancerHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public TestSubchannelTransportFactory(TimeSpan socketPingInterval, TimeSpan? con
259259
public ISubchannelTransport Create(Subchannel subchannel)
260260
{
261261
#if NET5_0_OR_GREATER
262-
return new SocketConnectivitySubchannelTransport(subchannel, _socketPingInterval, _connectTimeout, NullLoggerFactory.Instance, subchannel._manager.LoggerFactory);
262+
return new SocketConnectivitySubchannelTransport(subchannel, _socketPingInterval, _connectTimeout, subchannel._manager.LoggerFactory);
263263
#else
264264
return new PassiveSubchannelTransport(subchannel);
265265
#endif

0 commit comments

Comments
 (0)