Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
wanlwanl committed Oct 31, 2019
1 parent 60d944d commit af9fd4b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private set
{
lock (_statusLock)
{
if (_status != value && _status != ServiceConnectionStatus.Terminated)
if (_status != value)
{
var prev = _status;
_status = value;
Expand Down Expand Up @@ -173,7 +173,16 @@ public async Task StartAsync(string target = null)

public Task StopAsync()
{
return InternalStopAsync(ServiceConnectionStatus.Terminated);
try
{
_connectionContext?.Transport.Input.CancelPendingRead();
}
catch (Exception ex)
{
Log.UnexpectedExceptionInStop(Logger, ConnectionId, ex);
}

return Task.CompletedTask;
}

public virtual async Task WriteAsync(ServiceMessage serviceMessage)
Expand Down Expand Up @@ -503,7 +512,7 @@ private async Task KeepAliveAsync(TimerAwaitable timer)
if (Stopwatch.GetTimestamp() - Interlocked.Read(ref _lastReceiveTimestamp) > DefaultServiceTimeoutTicks)
{
Log.ServiceTimeout(Logger, DefaultServiceTimeout, ConnectionId);
await InternalStopAsync();
await StopAsync();
// We shouldn't get here twice.
continue;
}
Expand Down Expand Up @@ -539,21 +548,6 @@ private async ValueTask TrySendPingAsync()
}
}

private Task InternalStopAsync(ServiceConnectionStatus? newStatus = null)
{
try
{
_connectionContext?.Transport.Input.CancelPendingRead();
Status = newStatus ?? Status;
}
catch (Exception ex)
{
Log.UnexpectedExceptionInStop(Logger, ConnectionId, ex);
}

return Task.CompletedTask;
}

protected virtual ReadOnlyMemory<byte> GetPingMessage() => _cachedPingBytes;

private static class Log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ internal abstract class ServiceConnectionContainerBase : IServiceConnectionConta
private readonly object _statusLock = new object();

private readonly AckHandler _ackHandler;

private volatile List<IServiceConnection> _fixedServiceConnections;

private volatile ServiceConnectionStatus _status;

private volatile bool _terminated = false;

protected ILogger Logger { get; }

protected List<IServiceConnection> FixedServiceConnections
Expand Down Expand Up @@ -112,7 +115,11 @@ public Task StartAsync()
return Task.WhenAll(FixedServiceConnections.Select(c => StartCoreAsync(c)));
}

public virtual Task StopAsync() => Task.WhenAll(FixedServiceConnections.Select(c => c.StopAsync()));
public virtual Task StopAsync()
{
_terminated = true;
return Task.WhenAll(FixedServiceConnections.Select(c => c.StopAsync()));
}

/// <summary>
/// Start and manage the whole connection lifetime
Expand All @@ -126,7 +133,10 @@ protected async Task StartCoreAsync(IServiceConnection connection, string target
}
finally
{
await OnConnectionComplete(connection);
if (!_terminated)
{
await OnConnectionComplete(connection);
}
}
}

Expand Down Expand Up @@ -155,11 +165,6 @@ protected virtual async Task OnConnectionComplete(IServiceConnection serviceConn
throw new ArgumentNullException(nameof(serviceConnection));
}

if (serviceConnection.Status == ServiceConnectionStatus.Terminated)
{
return;
}

serviceConnection.ConnectionStatusChanged -= OnConnectionStatusChanged;

if (serviceConnection.Status == ServiceConnectionStatus.Connected)
Expand Down Expand Up @@ -288,22 +293,9 @@ protected virtual void Dispose(bool disposing)

protected virtual ServiceConnectionStatus GetStatus()
{
var statuses = (from conn in FixedServiceConnections
group conn by conn.Status into g
select g.Key
).ToDictionary(status => status, _ => 0);

if (statuses.ContainsKey(ServiceConnectionStatus.Connected))
{
return ServiceConnectionStatus.Connected;
}

if (statuses.ContainsKey(ServiceConnectionStatus.Terminated))
{
return ServiceConnectionStatus.Terminated;
}

return ServiceConnectionStatus.Disconnected;
return FixedServiceConnections.Any(s => s.Status == ServiceConnectionStatus.Connected)
? ServiceConnectionStatus.Connected
: ServiceConnectionStatus.Disconnected;
}

private Task WriteToRandomAvailableConnection(ServiceMessage serviceMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ internal enum ServiceConnectionStatus
Disconnected,
Connecting,
Connected,
Terminated
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ internal async Task StopServiceHubContextTest()
await Task.Delay(100);
var status = ((ServiceHubContext) serviceHubContext).GetConnectionStatus();
await ((ServiceHubContext) serviceHubContext).DisposeAsync();
Assert.Equal(ServiceConnectionStatus.Terminated, status);
Assert.Equal(ServiceConnectionStatus.Disconnected, status);
}
}

Expand Down

0 comments on commit af9fd4b

Please sign in to comment.