Skip to content

Commit 73ddb8e

Browse files
authored
Lazily initalise the HttpClient for the Bitbucket OAuth2ClientRegistry (#1239)
Lazily acquire the `HttpClient` instance from the factory inside of the `OAuth2ClientRegistry`. There is a relatively large cost to creating a new `HttpClient` instance (especially the first; due to config reading) that is being paid each time we just register the entire Bitbucket host provider. Whilst we are here also cleanup some of the styling/field naming in this class to be consistent with the rest of GCM. This saves approximately 120ms of startup time.
2 parents 7667f6a + 4dfedc7 commit 73ddb8e

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/shared/Atlassian.Bitbucket/OAuth2ClientRegistry.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@
33

44
namespace Atlassian.Bitbucket
55
{
6-
public class OAuth2ClientRegistry : IRegistry<BitbucketOAuth2Client>
6+
public class OAuth2ClientRegistry : DisposableObject, IRegistry<BitbucketOAuth2Client>
77
{
8-
private readonly HttpClient http;
9-
private ISettings settings;
10-
private readonly ITrace trace;
11-
private readonly ITrace2 trace2;
12-
private Cloud.BitbucketOAuth2Client cloudClient;
13-
private DataCenter.BitbucketOAuth2Client dataCenterClient;
8+
private readonly ICommandContext _context;
9+
10+
private HttpClient _httpClient;
11+
private Cloud.BitbucketOAuth2Client _cloudClient;
12+
private DataCenter.BitbucketOAuth2Client _dataCenterClient;
1413

1514
public OAuth2ClientRegistry(ICommandContext context)
1615
{
17-
this.http = context.HttpClientFactory.CreateClient();
18-
this.settings = context.Settings;
19-
this.trace = context.Trace;
20-
this.trace2 = context.Trace2;
16+
EnsureArgument.NotNull(context, nameof(context));
17+
_context = context;
2118
}
2219

2320
public BitbucketOAuth2Client Get(InputArguments input)
@@ -30,15 +27,20 @@ public BitbucketOAuth2Client Get(InputArguments input)
3027
return CloudClient;
3128
}
3229

33-
public void Dispose()
30+
protected override void ReleaseManagedResources()
3431
{
35-
http.Dispose();
36-
settings.Dispose();
37-
cloudClient = null;
38-
dataCenterClient = null;
32+
_httpClient?.Dispose();
33+
_cloudClient = null;
34+
_dataCenterClient = null;
35+
base.ReleaseManagedResources();
3936
}
4037

41-
private Cloud.BitbucketOAuth2Client CloudClient => cloudClient ??= new Cloud.BitbucketOAuth2Client(http, settings, trace2);
42-
private DataCenter.BitbucketOAuth2Client DataCenterClient => dataCenterClient ??= new DataCenter.BitbucketOAuth2Client(http, settings, trace2);
38+
private HttpClient HttpClient => _httpClient ??= _context.HttpClientFactory.CreateClient();
39+
40+
private Cloud.BitbucketOAuth2Client CloudClient =>
41+
_cloudClient ??= new Cloud.BitbucketOAuth2Client(HttpClient, _context.Settings, _context.Trace2);
42+
43+
private DataCenter.BitbucketOAuth2Client DataCenterClient =>
44+
_dataCenterClient ??= new DataCenter.BitbucketOAuth2Client(HttpClient, _context.Settings, _context.Trace2);
4345
}
4446
}

0 commit comments

Comments
 (0)