diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Compute.cs | 39 | ||||
| -rw-r--r-- | src/slskd/Core/Service.cs | 69 |
2 files changed, 85 insertions, 23 deletions
diff --git a/src/slskd/Common/Compute.cs b/src/slskd/Common/Compute.cs new file mode 100644 index 00000000..172de252 --- /dev/null +++ b/src/slskd/Common/Compute.cs @@ -0,0 +1,39 @@ +// <copyright file="Compute.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd +{ + using System; + + /// <summary> + /// Computational functions. + /// </summary> + public static class Compute + { + public static (int Delay, int Jitter) ExponentialBackoffDelay(int iteration, int maxDelayInMilliseconds = int.MaxValue) + { + iteration = Math.Min(100, iteration); + + var computedDelay = Math.Floor((Math.Pow(2, iteration) - 1) / 2) * 1000; + var clampedDelay = (int)Math.Min(computedDelay, maxDelayInMilliseconds); + + var jitter = new Random().Next(1000); + + return (clampedDelay, jitter); + } + } +}
\ No newline at end of file diff --git a/src/slskd/Core/Service.cs b/src/slskd/Core/Service.cs index 6df1ddcb..5800ffa0 100644 --- a/src/slskd/Core/Service.cs +++ b/src/slskd/Core/Service.cs @@ -38,8 +38,8 @@ namespace slskd public class Service : IHostedService { - private static readonly int MaxReconnectAttempts = 3; - private static int currentReconnectAttempts = 0; + private static readonly int ReconnectMaxDelayMilliseconds = 300000; // 5 minutes + private static (int Directories, int Files) sharedCounts = (0, 0); public Service( @@ -121,6 +121,7 @@ namespace slskd Client.RoomJoined += Client_RoomJoined; Client.RoomLeft += Client_RoomLeft; Client.Disconnected += Client_Disconnected; + Client.Connected += Client_Connected; SoulseekClient = Client; @@ -141,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) @@ -207,29 +206,53 @@ namespace slskd logger.Write(TranslateLogLevel(args.Level), "{@Message}", args.Message); } - private async void Client_Disconnected(object sender, SoulseekClientDisconnectedEventArgs args) + private void Client_Connected(object sender, EventArgs e) { - Console.WriteLine($"Disconnected from Soulseek server: {args.Message}"); + Logger.Information("Connected and logged in as {Username}", Options.Soulseek.Username); + } - // don't reconnect if the disconnecting Exception is either of these types. if KickedFromServerException, another - // client was most likely signed in, and retrying will cause a connect loop. if ObjectDisposedException, the client is - // shutting down. - if (!(args.Exception is KickedFromServerException || args.Exception is ObjectDisposedException)) + private async void Client_Disconnected(object sender, SoulseekClientDisconnectedEventArgs args) + { + if (args.Exception is ObjectDisposedException) + { + Logger.Debug("Disconnected from the Soulseek server: the client is shutting down"); + } + else if (args.Exception is LoginRejectedException) + { + Logger.Error("Disconnected from the Soulseek server: invalid username or password"); + } + else if (args.Exception is KickedFromServerException) + { + Logger.Error("Disconnected from the Soulseek server: another client logged in using the username {Username}", Options.Soulseek.Username); + } + else { - Interlocked.Increment(ref currentReconnectAttempts); + Logger.Error("Disconnected from the Soulseek server: {Message}", args.Exception.Message); - if (currentReconnectAttempts <= MaxReconnectAttempts) - { - var wait = (int)Math.Pow(currentReconnectAttempts, 3); - Console.WriteLine($"Waiting {wait} second(s) before reconnect..."); - await Task.Delay(wait); + var attempts = 1; - Console.WriteLine($"Attepting to reconnect..."); - await Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password); - } - else + while (true) { - Console.WriteLine($"Unable to reconnect after {currentReconnectAttempts} tries."); + 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); + } } } } |
