aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Application.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-07-11 20:13:09 -0500
committerGitHub <noreply@github.com>2022-07-11 20:13:09 -0500
commit9d2ad97f48f773c72a15fbd0e18cff17fa86860e (patch)
tree7b807681f8ba77a898bfc0b79ddb80d4a47012ea /src/slskd/Application.cs
parente494253d3a40c38a15ac97a428bfdaba7f6a279d (diff)
parent2c327ec95b55721959d9e521a1205e8deedf15bb (diff)
downloadslskd-9d2ad97f48f773c72a15fbd0e18cff17fa86860e.tar.gz
Merge pull request #544 from slskd/reconnect
Move reconnect logic to ConnectionWatchdog
Diffstat (limited to 'src/slskd/Application.cs')
-rw-r--r--src/slskd/Application.cs45
1 files changed, 8 insertions, 37 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs
index 598c0c5e..af553475 100644
--- a/src/slskd/Application.cs
+++ b/src/slskd/Application.cs
@@ -76,6 +76,7 @@ namespace slskd
IOptionsMonitor<Options> optionsMonitor,
IManagedState<State> state,
ISoulseekClient soulseekClient,
+ IConnectionWatchdog connectionWatchdog,
ITransferTracker transferTracker,
ITransferService transferService,
IBrowseTracker browseTracker,
@@ -139,11 +140,14 @@ namespace slskd
Client.DistributedNetworkStateChanged += Client_DistributedNetworkStateChanged;
Client.DownloadDenied += (e, args) => Log.Information("Download of {Filename} from {Username} was denied: {Message}", args.Filename, args.Username, args.Message);
Client.DownloadFailed += (e, args) => Log.Information("Download of {Filename} from {Username} failed", args.Filename, args.Username);
+
+ ConnectionWatchdog = connectionWatchdog;
}
private ISoulseekClient Client { get; set; }
private IRoomService RoomService { get; set; }
private IBrowseTracker BrowseTracker { get; set; }
+ private IConnectionWatchdog ConnectionWatchdog { get; }
private IConversationTracker ConversationTracker { get; set; }
private ILogger Log { get; set; } = Serilog.Log.ForContext<Application>();
private ConcurrentDictionary<string, ILogger> Loggers { get; } = new ConcurrentDictionary<string, ILogger>();
@@ -355,6 +359,7 @@ namespace slskd
private void Client_Connected(object sender, EventArgs e)
{
+ ConnectionWatchdog.Stop();
Log.Information("Connected to the Soulseek server");
}
@@ -381,7 +386,7 @@ namespace slskd
logger.Write(TranslateLogLevel(args.Level), "{@Message}", args.Message);
}
- private async void Client_Disconnected(object sender, SoulseekClientDisconnectedEventArgs args)
+ private void Client_Disconnected(object sender, SoulseekClientDisconnectedEventArgs args)
{
if (State.CurrentValue.PendingReconnect)
{
@@ -402,46 +407,12 @@ namespace slskd
}
else if (args.Exception is KickedFromServerException)
{
- Log.Error("Disconnected from the Soulseek server: another client logged in using the username {Username}", Client.Username);
+ Log.Error("Disconnected from the Soulseek server: another client logged in using the same username");
}
else
{
Log.Error("Disconnected from the Soulseek server: {Message}", args.Exception?.Message ?? args.Message);
-
- if (string.IsNullOrEmpty(Options.Soulseek.Username) || string.IsNullOrEmpty(Options.Soulseek.Password))
- {
- Log.Warning($"Not reconnecting to the Soulseek server; username and/or password invalid. Specify valid credentials and manually connect, or update config and restart.");
- 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.
- await Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password);
- break;
- }
- catch (Exception ex)
- {
- attempts++;
- Log.Error("Failed to reconnect: {Message}", ex.Message);
- }
- }
+ ConnectionWatchdog.Start();
}
}