changes to use dispose

This commit is contained in:
Luke Brown
2022-05-16 22:09:41 -05:00
parent 8bb4cd017c
commit a64eebe79f
9 changed files with 84 additions and 58 deletions

View File

@@ -111,7 +111,7 @@ namespace Emby.Server.Implementations
/// <summary>
/// Class CompositionRoot.
/// </summary>
public abstract class ApplicationHost : IServerApplicationHost, IDisposable
public abstract class ApplicationHost : IServerApplicationHost, IAsyncDisposable, IDisposable
{
/// <summary>
/// The environment variable prefixes to log at server startup.
@@ -1230,5 +1230,24 @@ namespace Emby.Server.Implementations
_disposed = true;
}
public async ValueTask DisposeAsync()
{
await DisposeAsyncCore().ConfigureAwait(false);
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Used to perform asynchronous cleanup of managed resources or for cascading calls to <see cref="DisposeAsync"/>.
/// </summary>
/// <returns>A ValueTask.</returns>
protected virtual async ValueTask DisposeAsyncCore()
{
foreach (var session in _sessionManager.Sessions)
{
await session.DisposeAsync().ConfigureAwait(false);
}
}
}
}

View File

@@ -19,7 +19,7 @@ namespace Emby.Server.Implementations.HttpServer
/// <summary>
/// Class WebSocketConnection.
/// </summary>
public class WebSocketConnection : IWebSocketConnection, IDisposable
public class WebSocketConnection : IWebSocketConnection, IAsyncDisposable, IDisposable
{
/// <summary>
/// The logger.
@@ -231,12 +231,6 @@ namespace Emby.Server.Implementations.HttpServer
CancellationToken.None);
}
/// <inheritdoc />
public async Task CloseSocket(CancellationToken cancellationToken)
{
await _socket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "System Shutdown", cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc />
public void Dispose()
{
@@ -255,5 +249,25 @@ namespace Emby.Server.Implementations.HttpServer
_socket.Dispose();
}
}
/// <inheritdoc />
public async ValueTask DisposeAsync()
{
await DisposeAsyncCore().ConfigureAwait(false);
Dispose(false);
GC.SuppressFinalize(this);
}
/// <summary>
/// Used to perform asynchronous cleanup of managed resources or for cascading calls to <see cref="DisposeAsync"/>.
/// </summary>
/// <returns>A ValueTask.</returns>
protected virtual async ValueTask DisposeAsyncCore()
{
if (_socket.State == WebSocketState.Open)
{
await _socket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "System Shutdown", CancellationToken.None).ConfigureAwait(false);
}
}
}
}

View File

@@ -1363,26 +1363,9 @@ namespace Emby.Server.Implementations.Session
{
CheckDisposed();
await CloseAllWebSockets(cancellationToken).ConfigureAwait(false);
await SendMessageToSessions(Sessions, SessionMessageType.ServerShuttingDown, string.Empty, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gracefully closes all web sockets in all sessions.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
private async Task CloseAllWebSockets(CancellationToken cancellationToken)
{
foreach (var session in Sessions)
{
foreach (var sessionController in session.SessionControllers)
{
await sessionController.CloseAllWebSockets(cancellationToken).ConfigureAwait(false);
}
}
}
/// <summary>
/// Sends the server restart notification.
/// </summary>

View File

@@ -14,7 +14,7 @@ using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.Session
{
public sealed class WebSocketController : ISessionController, IDisposable
public sealed class WebSocketController : ISessionController, IAsyncDisposable, IDisposable
{
private readonly ILogger<WebSocketController> _logger;
private readonly ISessionManager _sessionManager;
@@ -88,15 +88,6 @@ namespace Emby.Server.Implementations.Session
cancellationToken);
}
/// <inheritdoc />
public async Task CloseAllWebSockets(CancellationToken cancellationToken)
{
foreach (var socket in _sockets)
{
await socket.CloseSocket(cancellationToken).ConfigureAwait(false);
}
}
/// <inheritdoc />
public void Dispose()
{
@@ -112,5 +103,25 @@ namespace Emby.Server.Implementations.Session
_disposed = true;
}
public async ValueTask DisposeAsync()
{
if (_disposed)
{
return;
}
foreach (var socket in _sockets)
{
socket.Closed -= OnConnectionClosed;
if (socket is IAsyncDisposable disposableAsync)
{
await disposableAsync.DisposeAsync().ConfigureAwait(false);
}
}
_disposed = true;
}
}
}