1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
// <copyright file="ConnectionWatchdog.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>
using Microsoft.Extensions.Options;
namespace slskd
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Serilog;
using Soulseek;
/// <summary>
/// Monitors the connection to the Soulseek server and reconnects with exponential backoff, if necessary.
/// </summary>
public interface IConnectionWatchdog : IDisposable
{
/// <summary>
/// Gets a value indicating whether the watchdog is monitoring the server connection.
/// </summary>
bool IsEnabled { get; }
/// <summary>
/// Starts monitoring the server connection.
/// </summary>
/// <remarks>This should be called when the connection is disconnected.</remarks>
void Start();
/// <summary>
/// Stops monitoring the server connection.
/// </summary>
/// <remarks>This should be called when the application is reasonably certain that the connection is connected.</remarks>
void Stop();
}
/// <summary>
/// Monitors the connection to the Soulseek network and reconnects with exponential backoff, if necessary.
/// </summary>
public class ConnectionWatchdog : IConnectionWatchdog
{
private static readonly int ReconnectMaxDelayMilliseconds = 300000; // 5 minutes
/// <summary>
/// Initializes a new instance of the <see cref="ConnectionWatchdog"/> class.
/// </summary>
/// <param name="soulseekClient"></param>
/// <param name="optionsMonitor"></param>
/// <param name="state"></param>
public ConnectionWatchdog(
ISoulseekClient soulseekClient,
IOptionsMonitor<Options> optionsMonitor,
IStateMonitor<State> state)
{
Client = soulseekClient;
Options = optionsMonitor;
State = state;
WatchdogTimer = new System.Timers.Timer()
{
Interval = 100,
AutoReset = true,
Enabled = false,
};
WatchdogTimer.Elapsed += (sender, args) => _ = AttemptReconnect();
}
/// <summary>
/// Gets a value indicating whether the watchdog is monitoring the server connection.
/// </summary>
public bool IsEnabled => WatchdogTimer.Enabled;
private ISoulseekClient Client { get; }
private bool Disposed { get; set; }
private ILogger Log { get; } = Serilog.Log.ForContext<Application>();
private IOptionsMonitor<Options> Options { get; set; }
private IStateMonitor<State> State { get; }
private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1);
private System.Timers.Timer WatchdogTimer { get; }
/// <summary>
/// Disposes this instance.
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Starts monitoring the server connection.
/// </summary>
/// <remarks>This should be called when the connection is disconnected.</remarks>
public void Start()
{
WatchdogTimer.Enabled = true;
_ = AttemptReconnect();
}
/// <summary>
/// Stops monitoring the server connection.
/// </summary>
/// <remarks>This should be called when the application is reasonably certain that the connection is connected.</remarks>
public void Stop() => WatchdogTimer.Enabled = false;
/// <summary>
/// Disposes this instance.
/// </summary>
/// <param name="disposing"></param>
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();
}
}
}
}
}
|