Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Apr 29, 2024
1 parent 2fd2dce commit edb9e7d
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Grpc.Net.Client/Balancer/Subchannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public sealed class Subchannel : IDisposable

internal readonly ConnectionManager _manager;
private readonly ILogger _logger;
private readonly SemaphoreSlim _semaphore;
private readonly SemaphoreSlim _connectSemaphore;

private ISubchannelTransport _transport = default!;
private ConnectContext? _connectContext;
Expand Down Expand Up @@ -89,7 +89,7 @@ internal Subchannel(ConnectionManager manager, IReadOnlyList<BalancerAddress> ad
{
Lock = new object();
_logger = manager.LoggerFactory.CreateLogger(GetType());
_semaphore = new SemaphoreSlim(1);
_connectSemaphore = new SemaphoreSlim(1);

Id = manager.GetNextId();
_addresses = addresses.ToList();
Expand Down Expand Up @@ -304,6 +304,7 @@ private async Task ConnectTransportAsync()
Task? waitSemaporeTask = null;
lock (Lock)
{
// Don't start connecting if the subchannel has been shutdown. Transport/semaphore will be disposed if shutdown.
if (_state == ConnectivityState.Shutdown)
{
return;
Expand All @@ -316,10 +317,10 @@ private async Task ConnectTransportAsync()
//
// Try to get semaphore without waiting. If semaphore is already taken then start a task to wait for it to be released.
// Start this inside a lock to make sure subchannel isn't shutdown before waiting for semaphore.
if (!_semaphore.Wait(0))
if (!_connectSemaphore.Wait(0))
{
SubchannelLog.QueuingConnect(_logger, Id);
waitSemaporeTask = _semaphore.WaitAsync(connectContext.CancellationToken);
waitSemaporeTask = _connectSemaphore.WaitAsync(connectContext.CancellationToken);
}
}

Expand Down Expand Up @@ -420,7 +421,7 @@ private async Task ConnectTransportAsync()
connectContext.Dispose();
}

_semaphore.Release();
_connectSemaphore.Release();
}
}

Expand Down Expand Up @@ -522,7 +523,7 @@ public void Dispose()
{
CancelInProgressConnect();
_transport.Dispose();
_semaphore.Dispose();
_connectSemaphore.Dispose();
}
}
}
Expand Down

0 comments on commit edb9e7d

Please sign in to comment.