-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reuse HttpContext object per connection.
- Today in Kestrel, we reuse the IFeatureCollection per connection and per Http2Stream. This PR aims to take advantage of that same technique and affinitize the HttpContext and friends so that they are only allocated per connection. - ReusableHttpContext and friends mimic the functionality of DefaultHttpContext but is sealed and has no overridable methods.
- Loading branch information
Showing
11 changed files
with
703 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,4 +171,4 @@ struct FeatureInterfaces | |
public IRouteValuesFeature RouteValues; | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -136,4 +136,4 @@ struct FeatureInterfaces | |
public IResponseCookiesFeature Cookies; | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http.Features; | ||
|
||
namespace Microsoft.AspNetCore.Http.Internal | ||
{ | ||
public sealed class ReusableConnectionInfo : ConnectionInfo | ||
{ | ||
// Lambdas hoisted to static readonly fields to improve inlining https://github.com/dotnet/roslyn/issues/13624 | ||
private readonly static Func<IFeatureCollection, IHttpConnectionFeature> _newHttpConnectionFeature = f => new HttpConnectionFeature(); | ||
private readonly static Func<IFeatureCollection, ITlsConnectionFeature> _newTlsConnectionFeature = f => new TlsConnectionFeature(); | ||
|
||
private FeatureReferences<FeatureInterfaces> _features; | ||
|
||
public ReusableConnectionInfo(IFeatureCollection features) | ||
{ | ||
Initialize(features); | ||
} | ||
|
||
public void Initialize(IFeatureCollection features) | ||
{ | ||
_features = new FeatureReferences<FeatureInterfaces>(features); | ||
} | ||
|
||
public void Uninitialize() | ||
{ | ||
_features = default(FeatureReferences<FeatureInterfaces>); | ||
} | ||
|
||
private IHttpConnectionFeature HttpConnectionFeature => | ||
_features.Fetch(ref _features.Cache.Connection, _newHttpConnectionFeature); | ||
|
||
private ITlsConnectionFeature TlsConnectionFeature => | ||
_features.Fetch(ref _features.Cache.TlsConnection, _newTlsConnectionFeature); | ||
|
||
/// <inheritdoc /> | ||
public override string Id | ||
{ | ||
get { return HttpConnectionFeature.ConnectionId; } | ||
set { HttpConnectionFeature.ConnectionId = value; } | ||
} | ||
|
||
public override IPAddress RemoteIpAddress | ||
{ | ||
get { return HttpConnectionFeature.RemoteIpAddress; } | ||
set { HttpConnectionFeature.RemoteIpAddress = value; } | ||
} | ||
|
||
public override int RemotePort | ||
{ | ||
get { return HttpConnectionFeature.RemotePort; } | ||
set { HttpConnectionFeature.RemotePort = value; } | ||
} | ||
|
||
public override IPAddress LocalIpAddress | ||
{ | ||
get { return HttpConnectionFeature.LocalIpAddress; } | ||
set { HttpConnectionFeature.LocalIpAddress = value; } | ||
} | ||
|
||
public override int LocalPort | ||
{ | ||
get { return HttpConnectionFeature.LocalPort; } | ||
set { HttpConnectionFeature.LocalPort = value; } | ||
} | ||
|
||
public override X509Certificate2 ClientCertificate | ||
{ | ||
get { return TlsConnectionFeature.ClientCertificate; } | ||
set { TlsConnectionFeature.ClientCertificate = value; } | ||
} | ||
|
||
public override Task<X509Certificate2> GetClientCertificateAsync(CancellationToken cancellationToken = default) | ||
{ | ||
return TlsConnectionFeature.GetClientCertificateAsync(cancellationToken); | ||
} | ||
|
||
struct FeatureInterfaces | ||
{ | ||
public IHttpConnectionFeature Connection; | ||
public ITlsConnectionFeature TlsConnection; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,165 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using System.Threading; | ||
using Microsoft.AspNetCore.Http.Authentication; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.AspNetCore.Http.Features.Authentication; | ||
|
||
namespace Microsoft.AspNetCore.Http.Internal | ||
{ | ||
public sealed class ReusableHttpContext : HttpContext | ||
{ | ||
// Lambdas hoisted to static readonly fields to improve inlining https://github.com/dotnet/roslyn/issues/13624 | ||
private readonly static Func<IFeatureCollection, IItemsFeature> _newItemsFeature = f => new ItemsFeature(); | ||
private readonly static Func<IFeatureCollection, IServiceProvidersFeature> _newServiceProvidersFeature = f => new ServiceProvidersFeature(); | ||
private readonly static Func<IFeatureCollection, IHttpAuthenticationFeature> _newHttpAuthenticationFeature = f => new HttpAuthenticationFeature(); | ||
private readonly static Func<IFeatureCollection, IHttpRequestLifetimeFeature> _newHttpRequestLifetimeFeature = f => new HttpRequestLifetimeFeature(); | ||
private readonly static Func<IFeatureCollection, ISessionFeature> _newSessionFeature = f => new DefaultSessionFeature(); | ||
private readonly static Func<IFeatureCollection, ISessionFeature> _nullSessionFeature = f => null; | ||
private readonly static Func<IFeatureCollection, IHttpRequestIdentifierFeature> _newHttpRequestIdentifierFeature = f => new HttpRequestIdentifierFeature(); | ||
|
||
private FeatureReferences<FeatureInterfaces> _features; | ||
|
||
private ReusableHttpRequest _request; | ||
private ReusableHttpResponse _response; | ||
|
||
private ReusableConnectionInfo _connection; | ||
private ReusableWebSocketManager _websockets; | ||
|
||
public ReusableHttpContext(IFeatureCollection features) | ||
{ | ||
_features = new FeatureReferences<FeatureInterfaces>(features); | ||
_request = new ReusableHttpRequest(this); | ||
_response = new ReusableHttpResponse(this); | ||
} | ||
|
||
public void Initialize(IFeatureCollection features) | ||
{ | ||
_features = new FeatureReferences<FeatureInterfaces>(features); | ||
_request.Initialize(this); | ||
_response.Initialize(this); | ||
_connection?.Initialize(features); | ||
_websockets?.Initialize(features); | ||
} | ||
|
||
public void Uninitialize() | ||
{ | ||
_features = default; | ||
|
||
_request.Uninitialize(); | ||
_response.Uninitialize(); | ||
_connection?.Uninitialize(); | ||
_websockets?.Uninitialize(); | ||
} | ||
|
||
private IItemsFeature ItemsFeature => | ||
_features.Fetch(ref _features.Cache.Items, _newItemsFeature); | ||
|
||
private IServiceProvidersFeature ServiceProvidersFeature => | ||
_features.Fetch(ref _features.Cache.ServiceProviders, _newServiceProvidersFeature); | ||
|
||
private IHttpAuthenticationFeature HttpAuthenticationFeature => | ||
_features.Fetch(ref _features.Cache.Authentication, _newHttpAuthenticationFeature); | ||
|
||
private IHttpRequestLifetimeFeature LifetimeFeature => | ||
_features.Fetch(ref _features.Cache.Lifetime, _newHttpRequestLifetimeFeature); | ||
|
||
private ISessionFeature SessionFeature => | ||
_features.Fetch(ref _features.Cache.Session, _newSessionFeature); | ||
|
||
private ISessionFeature SessionFeatureOrNull => | ||
_features.Fetch(ref _features.Cache.Session, _nullSessionFeature); | ||
|
||
|
||
private IHttpRequestIdentifierFeature RequestIdentifierFeature => | ||
_features.Fetch(ref _features.Cache.RequestIdentifier, _newHttpRequestIdentifierFeature); | ||
|
||
public override IFeatureCollection Features => _features.Collection; | ||
|
||
public override HttpRequest Request => _request; | ||
|
||
public override HttpResponse Response => _response; | ||
|
||
public override ConnectionInfo Connection => _connection ?? (_connection = new ReusableConnectionInfo(_features.Collection)); | ||
|
||
[Obsolete("This is obsolete and will be removed in a future version. The recommended alternative is to use Microsoft.AspNetCore.Authentication.AuthenticationHttpContextExtensions. See https://go.microsoft.com/fwlink/?linkid=845470.")] | ||
public override AuthenticationManager Authentication => throw new NotSupportedException(); | ||
|
||
public override WebSocketManager WebSockets => _websockets ?? (_websockets = new ReusableWebSocketManager(_features.Collection)); | ||
|
||
|
||
public override ClaimsPrincipal User | ||
{ | ||
get | ||
{ | ||
var user = HttpAuthenticationFeature.User; | ||
if (user == null) | ||
{ | ||
user = new ClaimsPrincipal(new ClaimsIdentity()); | ||
HttpAuthenticationFeature.User = user; | ||
} | ||
return user; | ||
} | ||
set { HttpAuthenticationFeature.User = value; } | ||
} | ||
|
||
public override IDictionary<object, object> Items | ||
{ | ||
get { return ItemsFeature.Items; } | ||
set { ItemsFeature.Items = value; } | ||
} | ||
|
||
public override IServiceProvider RequestServices | ||
{ | ||
get { return ServiceProvidersFeature.RequestServices; } | ||
set { ServiceProvidersFeature.RequestServices = value; } | ||
} | ||
|
||
public override CancellationToken RequestAborted | ||
{ | ||
get { return LifetimeFeature.RequestAborted; } | ||
set { LifetimeFeature.RequestAborted = value; } | ||
} | ||
|
||
public override string TraceIdentifier | ||
{ | ||
get { return RequestIdentifierFeature.TraceIdentifier; } | ||
set { RequestIdentifierFeature.TraceIdentifier = value; } | ||
} | ||
|
||
public override ISession Session | ||
{ | ||
get | ||
{ | ||
var feature = SessionFeatureOrNull; | ||
if (feature == null) | ||
{ | ||
throw new InvalidOperationException("Session has not been configured for this application " + | ||
"or request."); | ||
} | ||
return feature.Session; | ||
} | ||
set | ||
{ | ||
SessionFeature.Session = value; | ||
} | ||
} | ||
|
||
public override void Abort() | ||
{ | ||
LifetimeFeature.Abort(); | ||
} | ||
|
||
struct FeatureInterfaces | ||
{ | ||
public IItemsFeature Items; | ||
public IServiceProvidersFeature ServiceProviders; | ||
public IHttpAuthenticationFeature Authentication; | ||
public IHttpRequestLifetimeFeature Lifetime; | ||
public ISessionFeature Session; | ||
public IHttpRequestIdentifierFeature RequestIdentifier; | ||
} | ||
} | ||
} |
Oops, something went wrong.