diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-06-13 14:04:29 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-13 14:04:29 -0500 |
| commit | bd173e1bcb8640744ac32fcfe9f114f2f0f31df0 (patch) | |
| tree | 813887e28dbf9d3b36d3489401af9cafdedbbec4 /src/slskd/Core | |
| parent | 4602c588e6bd6a607a23f35690d670837d87d4d1 (diff) | |
| parent | 5e4306f3dc77515401c24bf6d5a4c6780d494c84 (diff) | |
| download | slskd-bd173e1bcb8640744ac32fcfe9f114f2f0f31df0.tar.gz | |
Merge pull request #95 from slskd/reconnect
Attempt to reconnect automatically following a disconnect
Diffstat (limited to 'src/slskd/Core')
| -rw-r--r-- | src/slskd/Core/Service.cs | 69 |
1 files changed, 46 insertions, 23 deletions
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); + } } } } |
