Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Aspire.Dashboard/Aspire.Dashboard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<ItemGroup>
<Using Include="Aspire.Dashboard.Components.Resize" />
<Using Include="Aspire.Dashboard.Model.BrowserStorage" />
<Using Include="Aspire.Dashboard.ServiceClient" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using Aspire.Dashboard.Model;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;

Expand Down
577 changes: 310 additions & 267 deletions src/Aspire.Dashboard/Components/Interactions/InteractionsProvider.cs

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/Aspire.Dashboard/Components/Layout/DesktopNavMenu.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@using Aspire.Dashboard.Model
@using Aspire.Dashboard.Resources
@using Aspire.Dashboard.Resources
@using Aspire.Dashboard.Utils
@inject IStringLocalizer<Layout> Loc
@inject IDashboardClient DashboardClient
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Dashboard/Components/Layout/MainLayout.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public async Task LaunchSettingsAsync()
{
Title = Loc[nameof(Resources.Layout.MainLayoutSettingsDialogTitle)],
DismissTitle = DialogsLoc[nameof(Resources.Dialogs.DialogCloseButtonText)],
PrimaryAction = Loc[nameof(Resources.Layout.MainLayoutSettingsDialogClose)].Value,
PrimaryAction = Loc[nameof(Resources.Layout.MainLayoutSettingsDialogClose)].Value,
SecondaryAction = null,
TrapFocus = true,
Modal = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

using System.Text.RegularExpressions;
using Aspire.Dashboard.Components.CustomIcons;
using Aspire.Dashboard.Model;
using Aspire.Dashboard.Utils;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using Icons = Microsoft.FluentUI.AspNetCore.Components.Icons;
using Microsoft.JSInterop;
using Icons = Microsoft.FluentUI.AspNetCore.Components.Icons;

namespace Aspire.Dashboard.Components.Layout;

Expand Down
2 changes: 2 additions & 0 deletions src/Aspire.Dashboard/Components/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
@using Aspire.Dashboard.Components
@using Aspire.Dashboard.Components.Controls
@using Aspire.Dashboard.Components.Layout
@using Aspire.Dashboard.Model
@using Aspire.Dashboard.ServiceClient
@using Microsoft.Extensions.Localization

@* Require authorization for all pages of the web app *@
Expand Down
75 changes: 66 additions & 9 deletions src/Aspire.Dashboard/ServiceClient/DashboardClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
using System.Security.Cryptography.X509Certificates;
using System.Threading.Channels;
using Aspire.Dashboard.Configuration;
using Aspire.Dashboard.Model;
using Aspire.Dashboard.Utils;
using Aspire.Hosting;
using Aspire.DashboardService.Proto.V1;
using Aspire.Hosting;
using Grpc.Core;
using Grpc.Net.Client;
using Grpc.Net.Client.Configuration;
using Microsoft.Extensions.Options;
using ResourceCommandResponseKind = Aspire.Dashboard.Model.ResourceCommandResponseKind;

namespace Aspire.Dashboard.Model;
namespace Aspire.Dashboard.ServiceClient;

/// <summary>
/// Implements gRPC client that communicates with a resource server, populating data for the dashboard.
Expand Down Expand Up @@ -48,6 +50,8 @@ internal sealed class DashboardClient : IDashboardClient
private readonly TaskCompletionSource _initialDataReceivedTcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly Channel<WatchInteractionsRequestUpdate> _incomingInteractionChannel = Channel.CreateUnbounded<WatchInteractionsRequestUpdate>();
private readonly object _lock = new();
private readonly TaskCompletionSource _resourceWatchCompleteTcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly TaskCompletionSource _interactionWatchCompleteTcs = new(TaskCreationOptions.RunContinuationsAsynchronously);

private readonly ILoggerFactory _loggerFactory;
private readonly IKnownPropertyLookup _knownPropertyLookup;
Expand All @@ -65,7 +69,7 @@ internal sealed class DashboardClient : IDashboardClient
private int _state = StateNone;

private readonly GrpcChannel? _channel;
private readonly Aspire.DashboardService.Proto.V1.DashboardService.DashboardServiceClient? _client;
internal Aspire.DashboardService.Proto.V1.DashboardService.DashboardServiceClient? _client;
private readonly Metadata _headers = [];

private Task? _connection;
Expand Down Expand Up @@ -216,6 +220,9 @@ internal sealed class KeyStoreProperties
// For testing purposes
internal int OutgoingResourceSubscriberCount => _outgoingResourceChannels.Count;
internal int OutgoingInteractionSubscriberCount => _outgoingInteractionChannels.Count;
internal void SetDashboardServiceClient(Aspire.DashboardService.Proto.V1.DashboardService.DashboardServiceClient client) => _client = client;
internal Task ResourceWatchCompleteTask => _resourceWatchCompleteTcs.Task;
internal Task InteractionWatchCompleteTask => _interactionWatchCompleteTcs.Task;

public bool IsEnabled => _state is not StateDisabled;

Expand Down Expand Up @@ -244,8 +251,16 @@ async Task ConnectAndWatchAsync(CancellationToken cancellationToken)
await ConnectAsync().ConfigureAwait(false);

await Task.WhenAll(
Task.Run(() => WatchWithRecoveryAsync(cancellationToken, WatchResourcesAsync), cancellationToken),
Task.Run(() => WatchWithRecoveryAsync(cancellationToken, WatchInteractionsAsync), cancellationToken)).ConfigureAwait(false);
Task.Run(async () =>
{
await WatchWithRecoveryAsync(cancellationToken, WatchResourcesAsync).ConfigureAwait(false);
_resourceWatchCompleteTcs.TrySetResult();
}, cancellationToken),
Task.Run(async () =>
{
await WatchWithRecoveryAsync(cancellationToken, WatchInteractionsAsync).ConfigureAwait(false);
_interactionWatchCompleteTcs.TrySetResult();
}, cancellationToken)).ConfigureAwait(false);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
Expand Down Expand Up @@ -279,7 +294,7 @@ private class RetryContext
public int ErrorCount { get; set; }
}

private async Task WatchWithRecoveryAsync(CancellationToken cancellationToken, Func<RetryContext, CancellationToken, Task> action)
private async Task WatchWithRecoveryAsync(CancellationToken cancellationToken, Func<RetryContext, CancellationToken, Task<RetryResult>> action)
{
// Track the number of errors we've seen since the last successfully received message.
// As this number climbs, we extend the amount of time between reconnection attempts, in
Expand All @@ -303,7 +318,10 @@ private async Task WatchWithRecoveryAsync(CancellationToken cancellationToken, F

try
{
await action(retryContext, cancellationToken).ConfigureAwait(false);
if (await action(retryContext, cancellationToken).ConfigureAwait(false) == RetryResult.DoNotRetry)
{
return;
}
}
catch (ObjectDisposedException) when (cancellationToken.IsCancellationRequested)
{
Expand All @@ -325,7 +343,7 @@ static TimeSpan ExponentialBackOff(int errorCount, double maxSeconds)
}
}

private async Task WatchResourcesAsync(RetryContext retryContext, CancellationToken cancellationToken)
private async Task<RetryResult> WatchResourcesAsync(RetryContext retryContext, CancellationToken cancellationToken)
{
var call = _client!.WatchResources(new WatchResourcesRequest { IsReconnect = retryContext.ErrorCount != 0 }, headers: _headers, cancellationToken: cancellationToken);

Expand Down Expand Up @@ -405,15 +423,25 @@ private async Task WatchResourcesAsync(RetryContext retryContext, CancellationTo
}
}
}

return RetryResult.Retry;
}

private async Task WatchInteractionsAsync(RetryContext retryContext, CancellationToken cancellationToken)
private async Task<RetryResult> WatchInteractionsAsync(RetryContext retryContext, CancellationToken cancellationToken)
{
// Create the watch interactions call. This is a bidirectional streaming call.
// Responses are streamed out to all watchers. Requests are sent from the incoming interaction channel.
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
using var call = _client!.WatchInteractions(headers: _headers, cancellationToken: cts.Token);

if (await IsUnimplemented(call).ConfigureAwait(false))
{
// The server does not support this method.
_logger.LogWarning("Server does not support interactions.");

return RetryResult.DoNotRetry;
}

// Send
_ = Task.Run(async () =>
{
Expand Down Expand Up @@ -473,6 +501,29 @@ private async Task WatchInteractionsAsync(RetryContext retryContext, Cancellatio
// Ensure the write task is cancelled if we exit the loop.
cts.Cancel();
}

return RetryResult.Retry;
}

private static async Task<bool> IsUnimplemented(AsyncDuplexStreamingCall<WatchInteractionsRequestUpdate, WatchInteractionsResponseUpdate> call)
{
// Wait for the server to respond with initial headers. Require before calling GetStatus.
await call.ResponseHeadersAsync.ConfigureAwait(false);

try
{
var status = call.GetStatus();
if (status.StatusCode == StatusCode.Unimplemented)
{
return true;
}
}
catch (InvalidOperationException)
{
// Expected from GetStatus when the method is still in progress.
}

return false;
}

public async Task SendInteractionRequestAsync(WatchInteractionsRequestUpdate request, CancellationToken cancellationToken)
Expand Down Expand Up @@ -737,4 +788,10 @@ private class InteractionCollection : KeyedCollection<int, WatchInteractionsResp
{
protected override int GetKeyForItem(WatchInteractionsResponseUpdate item) => item.InteractionId;
}

private enum RetryResult
{
Retry,
DoNotRetry
}
}
3 changes: 2 additions & 1 deletion src/Aspire.Dashboard/ServiceClient/IDashboardClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using Aspire.Dashboard.Model;
using Aspire.DashboardService.Proto.V1;

namespace Aspire.Dashboard.Model;
namespace Aspire.Dashboard.ServiceClient;

/// <summary>
/// Provides data about active resources to external components, such as the dashboard.
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Hosting/ApplicationModel/InteractionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ internal sealed class InteractionCompletionState
[DebuggerDisplay("InteractionId = {InteractionId}, State = {State}, Title = {Title}")]
internal class Interaction
{
private static int s_nextInteractionId = 1;
private static int s_nextInteractionId;

public int InteractionId { get; }
public InteractionState State { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
<Compile Include="$(TestsSharedDir)Telemetry\*.cs" LinkBase="shared/Telemetry" />
<Compile Include="$(TestsSharedDir)Logging\*.cs" LinkBase="shared/Logging" />
<Compile Include="$(TestsSharedDir)DashboardModel\*.cs" LinkBase="shared/DashboardModel" />
<Compile Include="..\Aspire.Dashboard.Tests\Model\MockKnownPropertyLookup.cs" Link="Shared\MockKnownPropertyLookup.cs" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Aspire.Dashboard.Tests\Model\MockKnownPropertyLookup.cs" Link="Shared\MockKnownPropertyLookup.cs" />
<Using Include="Aspire.Dashboard.ServiceClient" />
</ItemGroup>

</Project>
Loading
Loading