feature: Implement Dispose for types which have disposable data (#1171)
* Initial set of dispose implementations Not handled yet: - Discord.Net.Websocket/Entities/SocketGuild - Discord.Net.Tests * Refactor DiscordSocketClient init into ctor This way we remove an IDisposableAnalyzer warning for not disposing the client when we set the client variable. * Dispose of clients when disposing sharded client * Finish implementing IDisposable where appropriate I opted to use NoWarn in the Tests project as it wasn't really necessary considering that our tests only run once * Tweak samples after feedback
This commit is contained in:
@@ -6,7 +6,7 @@ using Discord.Net;
|
||||
|
||||
namespace Discord
|
||||
{
|
||||
internal class ConnectionManager
|
||||
internal class ConnectionManager : IDisposable
|
||||
{
|
||||
public event Func<Task> Connected { add { _connectedEvent.Add(value); } remove { _connectedEvent.Remove(value); } }
|
||||
private readonly AsyncEvent<Func<Task>> _connectedEvent = new AsyncEvent<Func<Task>>();
|
||||
@@ -23,10 +23,12 @@ namespace Discord
|
||||
private CancellationTokenSource _combinedCancelToken, _reconnectCancelToken, _connectionCancelToken;
|
||||
private Task _task;
|
||||
|
||||
private bool _isDisposed;
|
||||
|
||||
public ConnectionState State { get; private set; }
|
||||
public CancellationToken CancelToken { get; private set; }
|
||||
|
||||
internal ConnectionManager(SemaphoreSlim stateLock, Logger logger, int connectionTimeout,
|
||||
internal ConnectionManager(SemaphoreSlim stateLock, Logger logger, int connectionTimeout,
|
||||
Func<Task> onConnecting, Func<Exception, Task> onDisconnecting, Action<Func<Exception, Task>> clientDisconnectHandler)
|
||||
{
|
||||
_stateLock = stateLock;
|
||||
@@ -55,6 +57,7 @@ namespace Discord
|
||||
{
|
||||
await AcquireConnectionLock().ConfigureAwait(false);
|
||||
var reconnectCancelToken = new CancellationTokenSource();
|
||||
_reconnectCancelToken?.Dispose();
|
||||
_reconnectCancelToken = reconnectCancelToken;
|
||||
_task = Task.Run(async () =>
|
||||
{
|
||||
@@ -67,16 +70,16 @@ namespace Discord
|
||||
try
|
||||
{
|
||||
await ConnectAsync(reconnectCancelToken).ConfigureAwait(false);
|
||||
nextReconnectDelay = 1000; //Reset delay
|
||||
nextReconnectDelay = 1000; //Reset delay
|
||||
await _connectionPromise.Task.ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException ex)
|
||||
{
|
||||
catch (OperationCanceledException ex)
|
||||
{
|
||||
Cancel(); //In case this exception didn't come from another Error call
|
||||
await DisconnectAsync(ex, !reconnectCancelToken.IsCancellationRequested).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
catch (Exception ex)
|
||||
{
|
||||
Error(ex); //In case this exception didn't come from another Error call
|
||||
if (!reconnectCancelToken.IsCancellationRequested)
|
||||
{
|
||||
@@ -113,6 +116,8 @@ namespace Discord
|
||||
|
||||
private async Task ConnectAsync(CancellationTokenSource reconnectCancelToken)
|
||||
{
|
||||
_connectionCancelToken?.Dispose();
|
||||
_combinedCancelToken?.Dispose();
|
||||
_connectionCancelToken = new CancellationTokenSource();
|
||||
_combinedCancelToken = CancellationTokenSource.CreateLinkedTokenSource(_connectionCancelToken.Token, reconnectCancelToken.Token);
|
||||
CancelToken = _combinedCancelToken.Token;
|
||||
@@ -120,7 +125,7 @@ namespace Discord
|
||||
_connectionPromise = new TaskCompletionSource<bool>();
|
||||
State = ConnectionState.Connecting;
|
||||
await _logger.InfoAsync("Connecting").ConfigureAwait(false);
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var readyPromise = new TaskCompletionSource<bool>();
|
||||
@@ -206,5 +211,25 @@ namespace Discord
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_combinedCancelToken?.Dispose();
|
||||
_reconnectCancelToken?.Dispose();
|
||||
_connectionCancelToken?.Dispose();
|
||||
}
|
||||
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user