//
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see https://www.gnu.org/licenses/.
//
using Microsoft.Extensions.Options;
namespace slskd
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Serilog;
using Soulseek;
///
/// Monitors the connection to the Soulseek server and reconnects with exponential backoff, if necessary.
///
public interface IConnectionWatchdog : IDisposable
{
///
/// Gets a value indicating whether the watchdog is monitoring the server connection.
///
bool IsEnabled { get; }
///
/// Starts monitoring the server connection.
///
/// This should be called when the connection is disconnected.
void Start();
///
/// Stops monitoring the server connection.
///
/// This should be called when the application is reasonably certain that the connection is connected.
void Stop();
}
///
/// Monitors the connection to the Soulseek network and reconnects with exponential backoff, if necessary.
///
public class ConnectionWatchdog : IConnectionWatchdog
{
private static readonly int ReconnectMaxDelayMilliseconds = 300000; // 5 minutes
///
/// Initializes a new instance of the class.
///
///
///
///
public ConnectionWatchdog(
ISoulseekClient soulseekClient,
IOptionsMonitor optionsMonitor,
IStateMonitor state)
{
Client = soulseekClient;
Options = optionsMonitor;
State = state;
WatchdogTimer = new System.Timers.Timer()
{
Interval = 100,
AutoReset = true,
Enabled = false,
};
WatchdogTimer.Elapsed += (sender, args) => _ = AttemptReconnect();
}
///
/// Gets a value indicating whether the watchdog is monitoring the server connection.
///
public bool IsEnabled => WatchdogTimer.Enabled;
private ISoulseekClient Client { get; }
private bool Disposed { get; set; }
private ILogger Log { get; } = Serilog.Log.ForContext();
private IOptionsMonitor Options { get; set; }
private IStateMonitor State { get; }
private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1);
private System.Timers.Timer WatchdogTimer { get; }
///
/// Disposes this instance.
///
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
///
/// Starts monitoring the server connection.
///
/// This should be called when the connection is disconnected.
public void Start()
{
WatchdogTimer.Enabled = true;
_ = AttemptReconnect();
}
///
/// Stops monitoring the server connection.
///
/// This should be called when the application is reasonably certain that the connection is connected.
public void Stop() => WatchdogTimer.Enabled = false;
///
/// Disposes this instance.
///
///
protected virtual void Dispose(bool disposing)
{
if (!Disposed)
{
if (disposing)
{
WatchdogTimer?.Dispose();
}
Disposed = true;
}
}
private async Task AttemptReconnect()
{
if (await SyncRoot.WaitAsync(0))
{
try
{
if (State.CurrentValue.Server.IsConnected)
{
return;
}
var attempts = 1;
while (true)
{
var (delay, jitter) = Compute.ExponentialBackoffDelay(
iteration: attempts,
maxDelayInMilliseconds: ReconnectMaxDelayMilliseconds);
var approximateDelay = (int)Math.Ceiling((double)(delay + jitter) / 1000);
Log.Information($"Waiting about {(approximateDelay == 1 ? "a second" : $"{approximateDelay} seconds")} before reconnecting");
await Task.Delay(delay + jitter);
Log.Information("Attempting to reconnect (#{Attempts})...", attempts);
try
{
// reconnect with the latest configuration values we have for username and password, instead of the
// options that were captured at startup. if a user has updated these values prior to the disconnect,
// the changes will take effect now.
var opt = Options.CurrentValue.Soulseek;
await Client.ConnectAsync(
address: opt.Address,
port: opt.Port,
username: opt.Username,
password: opt.Password);
break;
}
catch (Exception ex)
{
attempts++;
Log.Error("Failed to reconnect: {Message}", ex.Message);
}
}
}
finally
{
SyncRoot.Release();
}
}
}
}
}