aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-11-16 08:37:06 -0600
committerJP Dillingham <jp@dillingham.me>2022-11-16 08:37:06 -0600
commitb2b8042a5e722725ae077bf3312778977342bcdb (patch)
tree06f9bbba2f22ac772d4599f5d997ff9468e29219 /src
parentc0e5c4b3646a2294c50adddaa11ba8379f0d7a1b (diff)
downloadslskd-b2b8042a5e722725ae077bf3312778977342bcdb.tar.gz
synchronize start
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Relay/RelayClient.cs92
1 files changed, 53 insertions, 39 deletions
diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs
index 193f8cbb..df4a3e89 100644
--- a/src/slskd/Relay/RelayClient.cs
+++ b/src/slskd/Relay/RelayClient.cs
@@ -95,7 +95,8 @@ namespace slskd.Relay
private ManagedState<RelayClientState> State { get; } = new();
private IShareService Shares { get; }
- private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1);
+ private SemaphoreSlim ConfigurationSyncRoot { get; } = new SemaphoreSlim(1, 1);
+ private SemaphoreSlim StateSyncRoot { get; } = new SemaphoreSlim(1, 1);
private string LastOptionsHash { get; set; }
private IOptionsMonitor<Options> OptionsMonitor { get; }
private IHttpClientFactory HttpClientFactory { get; }
@@ -114,56 +115,69 @@ namespace slskd.Relay
/// <returns></returns>
public async Task StartAsync(CancellationToken cancellationToken = default)
{
- var mode = OptionsMonitor.CurrentValue.Relay.Mode.ToEnum<OperationMode>();
-
- if (mode != OperationMode.Agent && mode != OperationMode.Debug)
+ if (!StateSyncRoot.Wait(0, cancellationToken))
{
- throw new InvalidOperationException($"Relay client can only be started when operation mode is {OperationMode.Agent}");
+ // we're already attempting to connect, let the existing attempt handle it
+ return;
}
- StartCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
- LoggedInTaskCompletionSource = new TaskCompletionSource();
- StartRequested = true;
+ try
+ {
+ var mode = OptionsMonitor.CurrentValue.Relay.Mode.ToEnum<OperationMode>();
- Log.Information("Attempting to connect to the relay controller {Address}", OptionsMonitor.CurrentValue.Relay.Controller.Address);
+ if (mode != OperationMode.Agent && mode != OperationMode.Debug)
+ {
+ throw new InvalidOperationException($"Relay client can only be started when operation mode is {OperationMode.Agent}");
+ }
- State.SetValue(_ => TranslateState(HubConnectionState.Connecting));
+ StartCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
+ LoggedInTaskCompletionSource = new TaskCompletionSource();
+ StartRequested = true;
- // retry indefinitely
- await Retry.Do(
- task: () => HubConnection.StartAsync(StartCancellationTokenSource.Token),
- isRetryable: (attempts, ex) => true,
- onFailure: (attempts, ex) =>
- {
- Log.Debug(ex, "Relay hub connection failure");
- Log.Warning("Failed attempt #{Attempts} to connect to relay controller: {Message}", attempts, ex.Message);
- },
- maxAttempts: int.MaxValue,
- maxDelayInMilliseconds: 30000,
- StartCancellationTokenSource.Token);
+ Log.Information("Attempting to connect to the relay controller {Address}", OptionsMonitor.CurrentValue.Relay.Controller.Address);
- State.SetValue(_ => TranslateState(HubConnection.State));
- Log.Information("Relay controller connection established. Awaiting authentication...");
+ State.SetValue(_ => TranslateState(HubConnectionState.Connecting));
- await LoggedInTaskCompletionSource.Task;
+ // retry indefinitely
+ await Retry.Do(
+ task: () => HubConnection.StartAsync(StartCancellationTokenSource.Token),
+ isRetryable: (attempts, ex) => true,
+ onFailure: (attempts, ex) =>
+ {
+ Log.Debug(ex, "Relay hub connection failure");
+ Log.Warning("Failed attempt #{Attempts} to connect to relay controller: {Message}", attempts, ex.Message);
+ },
+ maxAttempts: int.MaxValue,
+ maxDelayInMilliseconds: 30000,
+ StartCancellationTokenSource.Token);
- LoggedIn = true;
+ State.SetValue(_ => TranslateState(HubConnection.State));
+ Log.Information("Relay controller connection established. Awaiting authentication...");
- Log.Information("Authenticated. Uploading shares...");
+ await LoggedInTaskCompletionSource.Task;
- try
- {
- await UploadSharesAsync();
+ LoggedIn = true;
+
+ Log.Information("Authenticated. Uploading shares...");
+
+ try
+ {
+ await UploadSharesAsync();
+ }
+ catch (Exception ex)
+ {
+ Log.Error(ex, ex.Message);
+ Log.Error("Disconnecting from the relay controller");
+ await StopAsync();
+ throw;
+ }
+
+ Log.Information("Shares uploaded. Ready to upload files.");
}
- catch (Exception ex)
+ finally
{
- Log.Error(ex, ex.Message);
- Log.Error("Disconnecting from the relay controller");
- await StopAsync();
- throw;
+ StateSyncRoot.Release();
}
-
- Log.Information("Shares uploaded. Ready to upload files.");
}
/// <summary>
@@ -425,7 +439,7 @@ namespace slskd.Relay
return;
}
- SyncRoot.Wait();
+ ConfigurationSyncRoot.Wait();
try
{
@@ -482,7 +496,7 @@ namespace slskd.Relay
}
finally
{
- SyncRoot.Release();
+ ConfigurationSyncRoot.Release();
}
}