Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from clientfactory to regular httpclient #823

Merged
merged 6 commits into from
Oct 3, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<ItemGroup>
<PackageReference Include="DotNet.Glob" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="morelinq" />
<PackageReference Include="NuGet.ProjectModel" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
// time to wait before retrying a failed call to pypi.org
private static readonly TimeSpan RETRYDELAY = TimeSpan.FromSeconds(1);

private static readonly HttpClientHandler HttpClientHandler = new HttpClientHandler() { CheckCertificateRevocationList = true };

private readonly IEnvironmentVariableService environmentVariableService;
private readonly ILogger<SimplePyPiClient> logger;

// Keep telemetry on how the cache is being used for future refinements
private readonly SimplePypiCacheTelemetryRecord cacheTelemetry = new SimplePypiCacheTelemetryRecord();

private readonly HttpClient httpClient;

/// <summary>
/// A thread safe cache implementation which contains a mapping of URI -> SimpleProject for simplepypi api projects
/// and has a limited number of entries which will expire after the cache fills or a specified interval.
Expand All @@ -58,13 +58,14 @@
// retries used so far for calls to pypi.org
private long retries;

public SimplePyPiClient(IEnvironmentVariableService environmentVariableService, IHttpClientFactory httpClientFactory, ILogger<SimplePyPiClient> logger)
public SimplePyPiClient(IEnvironmentVariableService environmentVariableService, ILogger<SimplePyPiClient> logger)
{
this.environmentVariableService = environmentVariableService;
this.logger = logger;
this.httpClient = httpClientFactory.CreateClient();
}

public static HttpClient HttpClient { get; internal set; } = new HttpClient(HttpClientHandler);

/// <inheritdoc />
public async Task<SimplePypiProject> GetSimplePypiProjectAsync(PipDependencySpecification spec)
{
Expand Down Expand Up @@ -269,7 +270,7 @@
request.Headers.UserAgent.Add(ProductValue);
request.Headers.UserAgent.Add(CommentValue);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.pypi.simple.v1+json"));
var response = await this.httpClient.SendAsync(request);
var response = await HttpClient.SendAsync(request);
return response;
}

Expand All @@ -280,6 +281,6 @@
this.cacheTelemetry.Dispose();
this.cachedProjectWheelFiles.Dispose();
this.cachedSimplePyPiProjects.Dispose();
this.httpClient.Dispose();
HttpClient.Dispose();

Check warning on line 284 in src/Microsoft.ComponentDetection.Detectors/pip/SimplePypiClient.cs

View check run for this annotation

Codecov / codecov/patch

src/Microsoft.ComponentDetection.Detectors/pip/SimplePypiClient.cs#L284

Added line #L284 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ namespace Microsoft.ComponentDetection.Orchestrator.Extensions;
using Microsoft.ComponentDetection.Orchestrator.Services;
using Microsoft.ComponentDetection.Orchestrator.Services.GraphTranslation;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http;

public static class ServiceCollectionExtensions
{
Expand Down Expand Up @@ -141,12 +139,6 @@ public static IServiceCollection AddComponentDetection(this IServiceCollection s
services.AddSingleton<IYarnLockFileFactory, YarnLockFileFactory>();
services.AddSingleton<IComponentDetector, YarnLockComponentDetector>();

// HttpClient
services.AddHttpClient();

// Remove the default logging for http client
services.RemoveAll<IHttpMessageHandlerBuilderFilter>();

return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="CommandLineParser" />
<PackageReference Include="DotNet.Glob" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Polly" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -8,7 +8,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Serilog" />
<PackageReference Include="Serilog.Extensions.Logging" />
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.ComponentDetection/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
.AddComponentDetection()
.AddLogging(l => l.AddSerilog(new LoggerConfiguration()
.MinimumLevel.ControlledBy(Interceptor.LogLevel)
.MinimumLevel.Override("Microsoft.Extensions.Http.DefaultHttpClientFactory", LogEventLevel.Information)
.Enrich.With<LoggingEnricher>()
.Enrich.FromLogContext()
.WriteTo.Map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Microsoft.ComponentDetection.Detectors.Tests;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Orchestrator.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
Expand All @@ -18,7 +20,10 @@ public class ComponentDetectorTests
[TestInitialize]
public void Initialize()
{
var serviceProvider = new ServiceCollection().AddComponentDetection().BuildServiceProvider();
var serviceProvider = new ServiceCollection()
.AddComponentDetection()
.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>))
.BuildServiceProvider();

this.detectors = serviceProvider.GetServices<IComponentDetector>().ToList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup Label="Project References">
<ProjectReference Include="..\..\src\Microsoft.ComponentDetection.Orchestrator\Microsoft.ComponentDetection.Orchestrator.csproj" />
Expand All @@ -7,7 +7,6 @@

<ItemGroup Label="Package References">
<PackageReference Include="coverlet.collector" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NuGet.Versioning" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ namespace Microsoft.ComponentDetection.Detectors.Tests;
[TestClass]
public class SimplePyPiClientTests
{
private readonly Mock<IHttpClientFactory> mockHttpClientFactory = new Mock<IHttpClientFactory>();

private Mock<HttpMessageHandler> MockHttpMessageHandler(string content, HttpStatusCode statusCode)
{
var handlerMock = new Mock<HttpMessageHandler>();
Expand All @@ -41,9 +39,8 @@ private Mock<HttpMessageHandler> MockHttpMessageHandler(string content, HttpStat

private ISimplePyPiClient CreateSimplePypiClient(HttpMessageHandler messageHandler, IEnvironmentVariableService evs, ILogger<SimplePyPiClient> logger)
{
var httpClient = new HttpClient(messageHandler);
this.mockHttpClientFactory.Setup(x => x.CreateClient(It.IsAny<string>())).Returns(httpClient);
return new SimplePyPiClient(evs, this.mockHttpClientFactory.Object, logger);
SimplePyPiClient.HttpClient = new HttpClient(messageHandler);
return new SimplePyPiClient(evs, logger);
}

[TestMethod]
Expand Down