-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Add HttpClient.Shared #84327
Closed
Closed
Add HttpClient.Shared #84327
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,15 @@ public partial class HttpClient : HttpMessageInvoker | |
#region Fields | ||
|
||
private static IWebProxy? s_defaultProxy; | ||
private static HttpClient? s_shared; | ||
private static readonly TimeSpan s_defaultTimeout = TimeSpan.FromSeconds(100); | ||
private static readonly TimeSpan s_maxTimeout = TimeSpan.FromMilliseconds(int.MaxValue); | ||
private static readonly TimeSpan s_infiniteTimeout = Threading.Timeout.InfiniteTimeSpan; | ||
private const HttpCompletionOption DefaultCompletionOption = HttpCompletionOption.ResponseContentRead; | ||
|
||
private volatile bool _operationStarted; | ||
private volatile bool _disposed; | ||
private bool _isSharedInstance; | ||
|
||
private CancellationTokenSource _pendingRequestsCts; | ||
private HttpRequestHeaders? _defaultRequestHeaders; | ||
|
@@ -46,8 +48,41 @@ public static IWebProxy DefaultProxy | |
} | ||
} | ||
|
||
public HttpRequestHeaders DefaultRequestHeaders => | ||
_defaultRequestHeaders ??= new HttpRequestHeaders(); | ||
/// <summary>Gets an <see cref="HttpClient"/> instance configured with all of the default options.</summary> | ||
public static HttpClient Shared | ||
{ | ||
get | ||
{ | ||
return s_shared ?? Initialize(); | ||
|
||
static HttpClient Initialize() | ||
{ | ||
var shared = new HttpClient() { _isSharedInstance = true, _operationStarted = true }; | ||
if (Interlocked.CompareExchange(ref s_shared, shared, null) is not null) | ||
{ | ||
shared._isSharedInstance = false; | ||
shared.Dispose(); | ||
shared = s_shared; | ||
} | ||
return shared; | ||
} | ||
} | ||
} | ||
|
||
public HttpRequestHeaders DefaultRequestHeaders | ||
{ | ||
get | ||
{ | ||
if (_isSharedInstance) | ||
{ | ||
// HttpRequestHeaders is not thread safe. Developers trying to mutate HttpClient.Shared.DefaultRequestHeaders | ||
// could thus corrupt the instance if used from multiple threads. | ||
throw new NotSupportedException(SR.net_http_shared_httpclient_notsupported); | ||
} | ||
|
||
return _defaultRequestHeaders ??= new HttpRequestHeaders(); | ||
} | ||
} | ||
|
||
public Version DefaultRequestVersion | ||
{ | ||
|
@@ -699,15 +734,23 @@ public void CancelPendingRequests() | |
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (disposing && !_disposed) | ||
if (disposing) | ||
{ | ||
_disposed = true; | ||
if (_isSharedInstance) | ||
{ | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we're not disposing the client, we might see more issues with H/3 similar to this: #71927 |
||
} | ||
|
||
// Cancel all pending requests (if any). Note that we don't call CancelPendingRequests() but cancel | ||
// the CTS directly. The reason is that CancelPendingRequests() would cancel the current CTS and create | ||
// a new CTS. We don't want a new CTS in this case. | ||
_pendingRequestsCts.Cancel(); | ||
_pendingRequestsCts.Dispose(); | ||
if (!_disposed) | ||
{ | ||
_disposed = true; | ||
|
||
// Cancel all pending requests (if any). Note that we don't call CancelPendingRequests() but cancel | ||
// the CTS directly. The reason is that CancelPendingRequests() would cancel the current CTS and create | ||
// a new CTS. We don't want a new CTS in this case. | ||
_pendingRequestsCts.Cancel(); | ||
_pendingRequestsCts.Dispose(); | ||
} | ||
} | ||
|
||
base.Dispose(disposing); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes me feel better about this feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of getter's throwing, I'd rather return a read-only object that threw if you tried to modify it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would a) make HttpRequestHeaders slower for everyone and b) not really affect anything because it has getters that mutate (e.g. lazily instantiate) and they would then need to throw instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we want to do the same in
CancelPendingRequests()
, and any other member that has a chance to mess with independent callers?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could. CancelPendingRequests is "safe" to use concurrently, whereas DefaultRequestHeaders is not, hence why I gave the latter this treatment.