File: Backchannel\DashboardInfoLookup.cs
Web Access
Project: src\src\Aspire.Cli\Aspire.Cli.csproj (aspire)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using Microsoft.Extensions.Logging;
 
namespace Aspire.Cli.Backchannel;
 
/// <summary>
/// Reads an AppHost's dashboard info for one-shot listings, which use it for dashboard links and the telemetry status.
/// </summary>
internal static class DashboardInfoLookup
{
    /// <summary>
    /// The longest a listing waits for the dashboard info before it continues without dashboard links or telemetry status.
    /// </summary>
    /// <remarks>
    /// The AppHost replies only once the dashboard is healthy, so a dashboard that is still starting, or an AppHost that
    /// stopped responding, would otherwise hold up <c>describe</c> and <c>list_resources</c>. This matches the budget
    /// of the telemetry request that follows.
    /// </remarks>
    internal static TimeSpan Timeout { get; } = TimeSpan.FromSeconds(5);
 
    /// <summary>
    /// Gets the dashboard info, or <see langword="null"/> when the AppHost doesn't reply within <paramref name="timeout"/>.
    /// </summary>
    /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was canceled.</exception>
    internal static async Task<GetDashboardInfoResponse?> GetAsync(
        IAppHostAuxiliaryBackchannel connection,
        TimeSpan timeout,
        ILogger logger,
        CancellationToken cancellationToken)
    {
        using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
        timeoutCts.CancelAfter(timeout);
 
        var infoTask = connection.GetDashboardInfoV2Async(timeoutCts.Token);
 
        // Canceling a StreamJsonRpc call only sends $/cancelRequest to the AppHost; the client keeps waiting for the
        // reply. WaitAsync stops the local wait at the deadline so an AppHost that never replies can't hold the listing.
        // The abandoned call is observed so its eventual failure isn't reported as an unobserved task exception. That
        // applies even when the call has already faulted, because WaitAsync then surfaces the cancellation instead.
        try
        {
            return await infoTask.WaitAsync(timeoutCts.Token).ConfigureAwait(false);
        }
        catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
        {
            ObserveFaults(infoTask);
            logger.LogDebug("The AppHost didn't return dashboard info within {Timeout}; continuing without dashboard links or telemetry status.", timeout);
            return null;
        }
        catch (OperationCanceledException)
        {
            ObserveFaults(infoTask);
            throw;
        }
    }
 
    private static void ObserveFaults(Task task)
    {
        _ = task.ContinueWith(
            static completedTask => _ = completedTask.Exception,
            CancellationToken.None,
            TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously,
            TaskScheduler.Default);
    }
}