aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-06-12 17:56:06 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-06-12 17:56:06 -0500
commit1f5c1557da3a1dc7d5727456994ae6222f83b6b4 (patch)
tree9c1ff4c6b63e00a41f4341ed7ba1e44a2598b160 /src
parent9704a8eb12576494a283b5c44ffe4a42b29653aa (diff)
downloadslskd-1f5c1557da3a1dc7d5727456994ae6222f83b6b4.tar.gz
make it actually reconnect indefinitely
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Core/Service.cs47
1 files changed, 30 insertions, 17 deletions
diff --git a/src/slskd/Core/Service.cs b/src/slskd/Core/Service.cs
index a8324a75..5800ffa0 100644
--- a/src/slskd/Core/Service.cs
+++ b/src/slskd/Core/Service.cs
@@ -39,7 +39,6 @@ namespace slskd
public class Service : IHostedService
{
private static readonly int ReconnectMaxDelayMilliseconds = 300000; // 5 minutes
- private static int reconnectAttempts = 0;
private static (int Directories, int Files) sharedCounts = (0, 0);
@@ -143,16 +142,14 @@ namespace slskd
public async Task StartAsync(CancellationToken cancellationToken)
{
- await Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password).ConfigureAwait(false);
-
if (UsingProxy)
{
Logger.Information($"Using Proxy {Options.Soulseek.Connection.Proxy.Address}:{Options.Soulseek.Connection.Proxy.Port}");
}
- Logger.Information("Connected and logged in as {Username}", Options.Soulseek.Username);
- Logger.Information("Listening on port {Port}", Options.Soulseek.ListenPort);
Logger.Information("Client started");
+ Logger.Information("Listening on port {Port}", Options.Soulseek.ListenPort);
+ await Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password).ConfigureAwait(false);
}
public Task StopAsync(CancellationToken cancellationToken)
@@ -211,7 +208,7 @@ namespace slskd
private void Client_Connected(object sender, EventArgs e)
{
- Interlocked.Exchange(ref reconnectAttempts, 0);
+ Logger.Information("Connected and logged in as {Username}", Options.Soulseek.Username);
}
private async void Client_Disconnected(object sender, SoulseekClientDisconnectedEventArgs args)
@@ -230,17 +227,33 @@ namespace slskd
}
else
{
- var (delay, jitter) = Compute.ExponentialBackoffDelay(
- iteration: reconnectAttempts,
- maxDelayInMilliseconds: ReconnectMaxDelayMilliseconds);
-
- Logger.Information($"Waiting {delay + jitter} millisecond(s) before reconnecting...");
- await Task.Delay(delay + jitter);
-
- Interlocked.Increment(ref reconnectAttempts);
-
- Logger.Information($"Attepting to reconnect (#{reconnectAttempts})...", reconnectAttempts);
- await Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password);
+ Logger.Error("Disconnected from the Soulseek server: {Message}", args.Exception.Message);
+
+ var attempts = 1;
+
+ while (true)
+ {
+ var (delay, jitter) = Compute.ExponentialBackoffDelay(
+ iteration: attempts,
+ maxDelayInMilliseconds: ReconnectMaxDelayMilliseconds);
+
+ var approximateDelay = (int)Math.Ceiling((double)(delay + jitter) / 1000);
+ Logger.Information($"Waiting about {(approximateDelay == 1 ? "a second" : $"{approximateDelay} seconds")} before reconnecting");
+ await Task.Delay(delay + jitter);
+
+ Logger.Information($"Attempting to reconnect (#{attempts})...", attempts);
+
+ try
+ {
+ await Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password);
+ break;
+ }
+ catch (Exception ex)
+ {
+ attempts++;
+ Logger.Error("Failed to reconnect: {Message}", ex.Message);
+ }
+ }
}
}