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 1 commit
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 @@ -33,14 +33,14 @@ public sealed class SimplePyPiClient : ISimplePyPiClient, IDisposable
// 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 @@ public sealed class SimplePyPiClient : ISimplePyPiClient, IDisposable
// 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 @@ private async Task<HttpResponseMessage> GetPypiResponseAsync(Uri uri)
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 @@ public void Dispose()
this.cacheTelemetry.Dispose();
this.cachedProjectWheelFiles.Dispose();
this.cachedSimplePyPiProjects.Dispose();
this.httpClient.Dispose();
HttpClient.Dispose();
}
}
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;
}
}
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 @@ -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
Loading