Files
UnrealEngineUWP/Engine/Source/Programs/AutomationTool/AutomationUtils/HttpClientSingleton.cs
joshua shlemmer 7902c4cd98 reusable singleton httpclient as per MS guidance for .net5+
updates PDB telemetry task to use new client

[REVIEW] [at]Gary.Yuan, [at]Eric.Knapik

#rb Eric.Knapik

[CL 30122489 by joshua shlemmer in ue5-main branch]
2023-12-05 12:18:26 -05:00

33 lines
1.1 KiB
C#

// Copyright Epic Games, Inc. All Rights Reserved.
using Microsoft.Extensions.Http;
using Polly;
using Polly.Extensions.Http;
using System;
using System.Net.Http;
namespace AutomationUtils
{
// This is used for instances where DI cannot be used for the HTTPClient
// More info: https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines
public class HttpClientSingleton<T>
{
private static readonly Lazy<HttpClient> internalInstance =
new Lazy<HttpClient>(() =>
{
var retryPolicy = HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
// per MS documentation, with a lifetime specified we shouldn't have socket exhaustion issues
var socketHandler = new SocketsHttpHandler { PooledConnectionLifetime = TimeSpan.FromMinutes(15) };
var pollyHandler = new PolicyHttpMessageHandler(retryPolicy)
{
InnerHandler = socketHandler,
};
return new HttpClient(pollyHandler);
});
public static HttpClient Client { get => internalInstance.Value; }
}
}