diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-11-16 09:37:37 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-16 09:37:37 -0600 |
| commit | 72cd1f5b6d98501efa8affa45c16c0fb7abd9a18 (patch) | |
| tree | 0535f6bb4822b6edd82ddf0a13242374699b74d2 /src/slskd | |
| parent | 538f26400b5054551456bfdd0b07382089bec8a3 (diff) | |
| parent | ea91a32c6d4d354f73de23bd1e2b8f9d6799803b (diff) | |
| download | slskd-72cd1f5b6d98501efa8affa45c16c0fb7abd9a18.tar.gz | |
Merge branch 'master' into dependabot/npm_and_yarn/src/web/loader-utils-2.0.4
Diffstat (limited to 'src/slskd')
| -rw-r--r-- | src/slskd/Relay/API/Controllers/RelayController.cs | 16 | ||||
| -rw-r--r-- | src/slskd/Relay/RelayClient.cs | 94 |
2 files changed, 71 insertions, 39 deletions
diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs index 8898e991..952507ad 100644 --- a/src/slskd/Relay/API/Controllers/RelayController.cs +++ b/src/slskd/Relay/API/Controllers/RelayController.cs @@ -47,6 +47,22 @@ namespace slskd.Relay private ILogger Log { get; } = Serilog.Log.ForContext<RelayController>(); private IRelayService Relay { get; } + [HttpPut("")] + [Authorize(Policy = AuthPolicy.Any)] + public async Task<IActionResult> Connect() + { + await Relay.Client.StartAsync(); + return Ok(); + } + + [HttpDelete("")] + [Authorize(Policy = AuthPolicy.Any)] + public async Task<IActionResult> Disconnect() + { + await Relay.Client.StopAsync(); + return NoContent(); + } + /// <summary> /// Uploads a file. /// </summary> diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs index 854399dd..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> @@ -181,6 +195,8 @@ namespace slskd.Relay LoggedIn = false; + State.SetValue(_ => TranslateState(HubConnectionState.Disconnected)); + Log.Information("Relay controller connection disconnected"); } } @@ -423,7 +439,7 @@ namespace slskd.Relay return; } - SyncRoot.Wait(); + ConfigurationSyncRoot.Wait(); try { @@ -480,7 +496,7 @@ namespace slskd.Relay } finally { - SyncRoot.Release(); + ConfigurationSyncRoot.Release(); } } |
