From 1db2661ea930a31e573f41ff0d8f2dec639b286d Mon Sep 17 00:00:00 2001 From: SKProCH Date: Thu, 12 Sep 2024 01:11:09 +0300 Subject: [PATCH] Implements a degraded state detection for LavalinkClusterNode --- .../Nodes/LavalinkClusterNode.cs | 20 ++++++++++++++++++- src/Lavalink4NET/LavalinkNode.cs | 9 +++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Lavalink4NET.Cluster/Nodes/LavalinkClusterNode.cs b/src/Lavalink4NET.Cluster/Nodes/LavalinkClusterNode.cs index 6b97babf..3aeaedba 100644 --- a/src/Lavalink4NET.Cluster/Nodes/LavalinkClusterNode.cs +++ b/src/Lavalink4NET.Cluster/Nodes/LavalinkClusterNode.cs @@ -1,4 +1,6 @@ -namespace Lavalink4NET.Cluster; +using Lavalink4NET.Events; + +namespace Lavalink4NET.Cluster; using System.Collections.Immutable; using Lavalink4NET.Clients; @@ -145,6 +147,9 @@ private async Task RunInternalAsync(CancellationToken shutdownCancellationToken var clientInformation = await _readyTask .WaitAsync(shutdownCancellationToken) .ConfigureAwait(false); + + _node.SocketConnectionClosed += OnNodeSocketConnectionClosed; + _node.SocketConnectionReady += OnNodeSocketConnectionReady; var nodeTask = _node.RunAsync(clientInformation, _shutdownCancellationToken).AsTask(); @@ -158,6 +163,9 @@ await _node } finally { + _node.SocketConnectionClosed -= OnNodeSocketConnectionClosed; + _node.SocketConnectionReady -= OnNodeSocketConnectionReady; + var exitStatus = _shutdownCancellationToken.IsCancellationRequested ? LavalinkNodeStatus.OnDemand : LavalinkNodeStatus.Unavailable; @@ -165,4 +173,14 @@ await _node await UpdateStatusAsync(exitStatus, shutdownCancellationToken); } } + + private async Task OnNodeSocketConnectionReady(object sender, EventArgs eventargs) + { + await UpdateStatusAsync(LavalinkNodeStatus.Available, _shutdownCancellationToken); + } + + private async Task OnNodeSocketConnectionClosed(object sender, ConnectionClosedEventArgs eventargs) + { + await UpdateStatusAsync(LavalinkNodeStatus.Degraded, _shutdownCancellationToken); + } } diff --git a/src/Lavalink4NET/LavalinkNode.cs b/src/Lavalink4NET/LavalinkNode.cs index 0dcbcef0..2f41fa1e 100644 --- a/src/Lavalink4NET/LavalinkNode.cs +++ b/src/Lavalink4NET/LavalinkNode.cs @@ -39,6 +39,9 @@ internal sealed class LavalinkNode : IAsyncDisposable private TaskCompletionSource _readyTaskCompletionSource; private Task? _executeTask; private bool _disposed; + + internal event AsyncEventHandler? SocketConnectionClosed; + internal event AsyncEventHandler? SocketConnectionReady; public LavalinkNode( LavalinkNodeServiceContext serviceContext, @@ -167,6 +170,7 @@ await _apiClient } _logger.Ready(Label, SessionId); + await SocketConnectionReady.InvokeAsync(this, EventArgs.Empty); } if (SessionId is null) @@ -491,12 +495,13 @@ private async Task ReceiveInternalAsync(ClientInformation clientInformation, Can } } - private Task InvokeConnectionClosedAsync(object sender, ConnectionClosedEventArgs eventArgs) + private async Task InvokeConnectionClosedAsync(object sender, ConnectionClosedEventArgs eventArgs) { ArgumentNullException.ThrowIfNull(sender); ArgumentNullException.ThrowIfNull(eventArgs); - return _serviceContext.NodeListener.OnConnectionClosedAsync(eventArgs).AsTask(); + await SocketConnectionClosed.InvokeAsync(this, eventArgs); + await _serviceContext.NodeListener.OnConnectionClosedAsync(eventArgs).AsTask(); } private async ValueTask ReceiveInternalAsync(ILavalinkSocket socket, CancellationToken cancellationToken)