From 23047e0d498501dc95bb58b010ed3c11f8cb3cf8 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Wed, 16 Nov 2022 15:10:09 +0800 Subject: [PATCH] Change channel transport property to readonly (#1947) --- .../Balancer/Internal/ConnectionManager.cs | 2 +- src/Grpc.Net.Client/Balancer/Subchannel.cs | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Grpc.Net.Client/Balancer/Internal/ConnectionManager.cs b/src/Grpc.Net.Client/Balancer/Internal/ConnectionManager.cs index 79715adea..ba61330f3 100644 --- a/src/Grpc.Net.Client/Balancer/Internal/ConnectionManager.cs +++ b/src/Grpc.Net.Client/Balancer/Internal/ConnectionManager.cs @@ -103,7 +103,7 @@ public void ConfigureBalancer(Func 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) { diff --git a/src/Grpc.Net.Client/Balancer/Subchannel.cs b/src/Grpc.Net.Client/Balancer/Subchannel.cs index 9aa1c1b72..7c37d15dd 100644 --- a/src/Grpc.Net.Client/Balancer/Subchannel.cs +++ b/src/Grpc.Net.Client/Balancer/Subchannel.cs @@ -46,7 +46,7 @@ public sealed class Subchannel : IDisposable { internal readonly List _addresses; internal readonly object Lock; - internal ISubchannelTransport Transport { get; set; } = default!; + internal ISubchannelTransport Transport => _transport; internal int Id { get; } /// @@ -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? _delayInterruptTcs; @@ -68,7 +69,7 @@ public sealed class Subchannel : IDisposable /// /// Gets the current connected address. /// - public BalancerAddress? CurrentAddress => Transport.CurrentAddress; + public BalancerAddress? CurrentAddress => _transport.CurrentAddress; /// /// Gets the metadata attributes. @@ -88,6 +89,11 @@ internal Subchannel(ConnectionManager manager, IReadOnlyList ad SubchannelLog.SubchannelCreated(_logger, Id, addresses); } + internal void SetTransport(ISubchannelTransport transport) + { + _transport = transport; + } + private readonly List _stateChangedRegistrations = new List(); /// @@ -190,7 +196,7 @@ public void UpdateAddresses(IReadOnlyList addresses) if (requireReconnect) { CancelInProgressConnect(); - Transport.Disconnect(); + _transport.Disconnect(); RequestConnection(); } } @@ -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(); @@ -267,7 +273,7 @@ private async Task ConnectTransportAsync() } } - if (await Transport.TryConnectAsync(connectContext).ConfigureAwait(false)) + if (await _transport.TryConnectAsync(connectContext).ConfigureAwait(false)) { return; } @@ -408,7 +414,7 @@ public void Dispose() _stateChangedRegistrations.Clear(); CancelInProgressConnect(); - Transport.Dispose(); + _transport.Dispose(); } }