mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-05-22 12:32:14 +00:00
Some checks are pending
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Read metadata (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
81 lines
2.8 KiB
C#
81 lines
2.8 KiB
C#
using AIStudio.Settings;
|
|
|
|
namespace AIStudio.Tools;
|
|
|
|
/// <summary>
|
|
/// Provides utility methods to standardize the management of HTTP client timeouts
|
|
/// across various components in the application.
|
|
/// </summary>
|
|
public static class ExternalHttpClientTimeout
|
|
{
|
|
public const int MIN_HTTP_CLIENT_TIMEOUT_SECONDS = 120;
|
|
public const int MAX_HTTP_CLIENT_TIMEOUT_SECONDS = 3600;
|
|
public const int DEFAULT_HTTP_CLIENT_TIMEOUT_SECONDS = 3600;
|
|
|
|
private static readonly Lazy<SettingsManager> SETTINGS_MANAGER = new(() => Program.SERVICE_PROVIDER.GetRequiredService<SettingsManager>());
|
|
|
|
public static HttpClient CreateHttpClient(Uri? baseAddress = null)
|
|
{
|
|
var httpClient = new HttpClient();
|
|
Configure(httpClient, baseAddress);
|
|
return httpClient;
|
|
}
|
|
|
|
public static string GetTimeoutDescription()
|
|
{
|
|
var timeout = GetTimeout();
|
|
|
|
if (timeout.TotalHours >= 1 && timeout.TotalMinutes % 60 == 0)
|
|
{
|
|
var hours = (int)timeout.TotalHours;
|
|
return hours == 1 ? "1 hour" : $"{hours} hours";
|
|
}
|
|
|
|
if (timeout.TotalMinutes >= 1 && timeout.TotalSeconds % 60 == 0)
|
|
{
|
|
var minutes = (int)timeout.TotalMinutes;
|
|
return minutes == 1 ? "1 minute" : $"{minutes} minutes";
|
|
}
|
|
|
|
var seconds = (int)timeout.TotalSeconds;
|
|
return seconds == 1 ? "1 second" : $"{seconds} seconds";
|
|
}
|
|
|
|
public static CancellationTokenSource CreateTimeoutTokenSource(CancellationToken cancellationToken)
|
|
{
|
|
var timeoutTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
|
timeoutTokenSource.CancelAfter(GetTimeout());
|
|
return timeoutTokenSource;
|
|
}
|
|
|
|
public static bool IsTimeoutException(Exception exception, CancellationToken userCancellationToken = default)
|
|
{
|
|
if (userCancellationToken.IsCancellationRequested)
|
|
return false;
|
|
|
|
if (exception is TimeoutException)
|
|
return true;
|
|
|
|
if (exception is OperationCanceledException)
|
|
return true;
|
|
|
|
return exception.InnerException is not null && IsTimeoutException(exception.InnerException, userCancellationToken);
|
|
}
|
|
|
|
private static TimeSpan GetTimeout()
|
|
{
|
|
var seconds = SETTINGS_MANAGER.Value.ConfigurationData.App.HttpClientTimeoutSeconds;
|
|
if (seconds <= 0)
|
|
seconds = DEFAULT_HTTP_CLIENT_TIMEOUT_SECONDS;
|
|
|
|
seconds = Math.Clamp(seconds, MIN_HTTP_CLIENT_TIMEOUT_SECONDS, MAX_HTTP_CLIENT_TIMEOUT_SECONDS);
|
|
return TimeSpan.FromSeconds(seconds);
|
|
}
|
|
|
|
private static void Configure(HttpClient httpClient, Uri? baseAddress = null)
|
|
{
|
|
httpClient.Timeout = GetTimeout();
|
|
if (baseAddress is not null)
|
|
httpClient.BaseAddress = baseAddress;
|
|
}
|
|
} |