Skip to content

Commit

Permalink
Change channel transport property to readonly (#1947)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Nov 16, 2022
1 parent 91ac775 commit 23047e0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Grpc.Net.Client/Balancer/Internal/ConnectionManager.cs
Expand Up @@ -103,7 +103,7 @@ public void ConfigureBalancer(Func<IChannelControlHelper, LoadBalancer> configur
Subchannel IChannelControlHelper.CreateSubchannel(SubchannelOptions options)
{
var subchannel = new Subchannel(this, options.Addresses);
subchannel.Transport = _subchannelTransportFactory.Create(subchannel);
subchannel.SetTransport(_subchannelTransportFactory.Create(subchannel));

lock (_subchannels)
{
Expand Down
18 changes: 12 additions & 6 deletions src/Grpc.Net.Client/Balancer/Subchannel.cs
Expand Up @@ -46,7 +46,7 @@ public sealed class Subchannel : IDisposable
{
internal readonly List<BalancerAddress> _addresses;
internal readonly object Lock;
internal ISubchannelTransport Transport { get; set; } = default!;
internal ISubchannelTransport Transport => _transport;
internal int Id { get; }

/// <summary>
Expand All @@ -60,6 +60,7 @@ public sealed class Subchannel : IDisposable
internal readonly ConnectionManager _manager;
private readonly ILogger _logger;

private ISubchannelTransport _transport = default!;
private ConnectContext? _connectContext;
private ConnectivityState _state;
private TaskCompletionSource<object?>? _delayInterruptTcs;
Expand All @@ -68,7 +69,7 @@ public sealed class Subchannel : IDisposable
/// <summary>
/// Gets the current connected address.
/// </summary>
public BalancerAddress? CurrentAddress => Transport.CurrentAddress;
public BalancerAddress? CurrentAddress => _transport.CurrentAddress;

/// <summary>
/// Gets the metadata attributes.
Expand All @@ -88,6 +89,11 @@ internal Subchannel(ConnectionManager manager, IReadOnlyList<BalancerAddress> ad
SubchannelLog.SubchannelCreated(_logger, Id, addresses);
}

internal void SetTransport(ISubchannelTransport transport)
{
_transport = transport;
}

private readonly List<StateChangedRegistration> _stateChangedRegistrations = new List<StateChangedRegistration>();

/// <summary>
Expand Down Expand Up @@ -190,7 +196,7 @@ public void UpdateAddresses(IReadOnlyList<BalancerAddress> addresses)
if (requireReconnect)
{
CancelInProgressConnect();
Transport.Disconnect();
_transport.Disconnect();
RequestConnection();
}
}
Expand Down Expand Up @@ -249,7 +255,7 @@ private async Task ConnectTransportAsync()
// There shouldn't be a previous connect in progress, but cancel the CTS to ensure they're no longer running.
CancelInProgressConnect();

var connectContext = _connectContext = new ConnectContext(Transport.ConnectTimeout ?? Timeout.InfiniteTimeSpan);
var connectContext = _connectContext = new ConnectContext(_transport.ConnectTimeout ?? Timeout.InfiniteTimeSpan);

var backoffPolicy = _manager.BackoffPolicyFactory.Create();

Expand All @@ -267,7 +273,7 @@ private async Task ConnectTransportAsync()
}
}

if (await Transport.TryConnectAsync(connectContext).ConfigureAwait(false))
if (await _transport.TryConnectAsync(connectContext).ConfigureAwait(false))
{
return;
}
Expand Down Expand Up @@ -408,7 +414,7 @@ public void Dispose()
_stateChangedRegistrations.Clear();

CancelInProgressConnect();
Transport.Dispose();
_transport.Dispose();
}
}

Expand Down

0 comments on commit 23047e0

Please sign in to comment.