Skip to content

Commit

Permalink
fix: disconnecting clients should not use client factory cancellation…
Browse files Browse the repository at this point in the history
… token source
  • Loading branch information
Fresa committed Aug 8, 2022
1 parent 51ac317 commit 5d3fb85
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 22 deletions.
1 change: 0 additions & 1 deletion Test.It.With.Amqp/NetworkClient/SocketClientServer.cs
Expand Up @@ -39,7 +39,6 @@ internal static IServer StartForwardingClients(SocketServer server, SocketNetwor
{
_clientSessions,
_networkClientServer,
_socketNetworkClientFactory,
_socketServer,
}.DisposeAllAsync());

Expand Down
23 changes: 2 additions & 21 deletions Test.It.With.Amqp/NetworkClient/SocketNetworkClientFactory.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -9,16 +8,14 @@

namespace Test.It.With.Amqp.NetworkClient
{
internal class SocketNetworkClientFactory : IAsyncDisposable
internal class SocketNetworkClientFactory
{
private readonly IProtocolResolver _protocolResolver;
private readonly IConfiguration _configuration;
private readonly Func<AmqpConnectionSession, IDisposable> _subscribe;
private readonly CancellationTokenSource _cancellationTokenSource =
new CancellationTokenSource();

private readonly List<Task> _tasks = new List<Task>();

public SocketNetworkClientFactory(
IProtocolResolver protocolResolver,
IConfiguration configuration,
Expand Down Expand Up @@ -92,7 +89,6 @@ void OnClientDisconnected(object sender, EventArgs args)
}
}
});
_tasks.Add(clientReceivingTask);

// ReSharper disable once MethodSupportsCancellation
// Will be handled by the disposable returned
Expand Down Expand Up @@ -124,7 +120,6 @@ await disconnectSessionAsync(token)
}
}
);
_tasks.Add(clientsDisconnectingTask);

return new ClientSessions(activeSessions, new AsyncDisposableAction(async () =>
{
Expand All @@ -134,27 +129,13 @@ await Task.WhenAll(clientReceivingTask, clientsDisconnectingTask)
await activeSessions.Keys.Select(id =>
activeSessions.TryRemove(id, out var disconnectSessionAsync)
? disconnectSessionAsync(_cancellationTokenSource.Token)
? disconnectSessionAsync(CancellationToken.None)
: new ValueTask())
.WhenAllAsync()
.ConfigureAwait(false);
cts.Dispose();
}));
}

public async ValueTask DisposeAsync()
{
_cancellationTokenSource.Cancel();
try
{
await Task.WhenAll(_tasks)
.ConfigureAwait(false);
}
catch
{
}
_cancellationTokenSource.Dispose();
}
}
}

0 comments on commit 5d3fb85

Please sign in to comment.