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

Lazily initalise the HttpClient for the Bitbucket OAuth2ClientRegistry #1239

Merged
merged 2 commits into from
May 3, 2023
Merged
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
38 changes: 20 additions & 18 deletions src/shared/Atlassian.Bitbucket/OAuth2ClientRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@

namespace Atlassian.Bitbucket
{
public class OAuth2ClientRegistry : IRegistry<BitbucketOAuth2Client>
public class OAuth2ClientRegistry : DisposableObject, IRegistry<BitbucketOAuth2Client>
{
private readonly HttpClient http;
private ISettings settings;
private readonly ITrace trace;
private readonly ITrace2 trace2;
private Cloud.BitbucketOAuth2Client cloudClient;
private DataCenter.BitbucketOAuth2Client dataCenterClient;
private readonly ICommandContext _context;

private HttpClient _httpClient;
private Cloud.BitbucketOAuth2Client _cloudClient;
private DataCenter.BitbucketOAuth2Client _dataCenterClient;

public OAuth2ClientRegistry(ICommandContext context)
{
this.http = context.HttpClientFactory.CreateClient();
this.settings = context.Settings;
this.trace = context.Trace;
this.trace2 = context.Trace2;
EnsureArgument.NotNull(context, nameof(context));
_context = context;
}

public BitbucketOAuth2Client Get(InputArguments input)
Expand All @@ -30,15 +27,20 @@ public BitbucketOAuth2Client Get(InputArguments input)
return CloudClient;
}

public void Dispose()
protected override void ReleaseManagedResources()
{
http.Dispose();
settings.Dispose();
cloudClient = null;
dataCenterClient = null;
_httpClient?.Dispose();
_cloudClient = null;
_dataCenterClient = null;
base.ReleaseManagedResources();
}

private Cloud.BitbucketOAuth2Client CloudClient => cloudClient ??= new Cloud.BitbucketOAuth2Client(http, settings, trace2);
private DataCenter.BitbucketOAuth2Client DataCenterClient => dataCenterClient ??= new DataCenter.BitbucketOAuth2Client(http, settings, trace2);
private HttpClient HttpClient => _httpClient ??= _context.HttpClientFactory.CreateClient();

private Cloud.BitbucketOAuth2Client CloudClient =>
_cloudClient ??= new Cloud.BitbucketOAuth2Client(HttpClient, _context.Settings, _context.Trace2);

private DataCenter.BitbucketOAuth2Client DataCenterClient =>
_dataCenterClient ??= new DataCenter.BitbucketOAuth2Client(HttpClient, _context.Settings, _context.Trace2);
}
}