This repository was archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 190
Streamlining feature cache and object re-initialiation code paths #516
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e3fe74
Streamlining feature cache and object re-initialiation code paths
lodejard c05c203
Adding example of what http context pooling might look like
lodejard 4265855
PR feedback
lodejard 290ff2a
PR feedback
lodejard 35d7106
PR feedback
lodejard c95d816
Updating unit test to probe _features cache field state
lodejard 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 hidden or 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,54 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNet.Http; | ||
using Microsoft.AspNet.Http.Features; | ||
using Microsoft.AspNet.Http.Internal; | ||
|
||
namespace SampleApp | ||
{ | ||
public class PooledHttpContext : DefaultHttpContext | ||
{ | ||
DefaultHttpRequest _pooledHttpRequest; | ||
DefaultHttpResponse _pooledHttpResponse; | ||
|
||
public PooledHttpContext(IFeatureCollection featureCollection) : | ||
base(featureCollection) | ||
{ | ||
} | ||
|
||
protected override HttpRequest InitializeHttpRequest() | ||
{ | ||
if (_pooledHttpRequest != null) | ||
{ | ||
_pooledHttpRequest.Initialize(this, Features); | ||
return _pooledHttpRequest; | ||
} | ||
|
||
return new DefaultHttpRequest(this, Features); | ||
} | ||
|
||
protected override void UninitializeHttpRequest(HttpRequest instance) | ||
{ | ||
_pooledHttpRequest = instance as DefaultHttpRequest; | ||
_pooledHttpRequest?.Uninitialize(); | ||
} | ||
|
||
protected override HttpResponse InitializeHttpResponse() | ||
{ | ||
if (_pooledHttpResponse != null) | ||
{ | ||
_pooledHttpResponse.Initialize(this, Features); | ||
return _pooledHttpResponse; | ||
} | ||
|
||
return new DefaultHttpResponse(this, Features); | ||
} | ||
|
||
protected override void UninitializeHttpResponse(HttpResponse instance) | ||
{ | ||
_pooledHttpResponse = instance as DefaultHttpResponse; | ||
_pooledHttpResponse?.Uninitialize(); | ||
} | ||
} | ||
} |
This file contains hidden or 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,65 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.AspNet.Http; | ||
using Microsoft.AspNet.Http.Features; | ||
|
||
namespace SampleApp | ||
{ | ||
public class PooledHttpContextFactory : IHttpContextFactory | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly Stack<PooledHttpContext> _pool = new Stack<PooledHttpContext>(); | ||
|
||
public PooledHttpContextFactory(IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
public HttpContext Create(IFeatureCollection featureCollection) | ||
{ | ||
PooledHttpContext httpContext = null; | ||
lock (_pool) | ||
{ | ||
if (_pool.Count != 0) | ||
{ | ||
httpContext = _pool.Pop(); | ||
} | ||
} | ||
|
||
if (httpContext == null) | ||
{ | ||
httpContext = new PooledHttpContext(featureCollection); | ||
} | ||
else | ||
{ | ||
httpContext.Initialize(featureCollection); | ||
} | ||
|
||
if (_httpContextAccessor != null) | ||
{ | ||
_httpContextAccessor.HttpContext = httpContext; | ||
} | ||
return httpContext; | ||
} | ||
|
||
public void Dispose(HttpContext httpContext) | ||
{ | ||
if (_httpContextAccessor != null) | ||
{ | ||
_httpContextAccessor.HttpContext = null; | ||
} | ||
|
||
var pooled = httpContext as PooledHttpContext; | ||
if (pooled != null) | ||
{ | ||
pooled.Uninitialize(); | ||
lock (_pool) | ||
{ | ||
_pool.Push(pooled); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,62 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
|
||
namespace Microsoft.AspNet.Http.Features | ||
{ | ||
public struct FeatureReferences<TCache> | ||
{ | ||
public FeatureReferences(IFeatureCollection collection) | ||
{ | ||
Collection = collection; | ||
Cache = default(TCache); | ||
Revision = collection.Revision; | ||
} | ||
|
||
public IFeatureCollection Collection { get; private set; } | ||
public int Revision { get; private set; } | ||
|
||
// cache is a public field because the code calling Fetch must | ||
// be able to pass ref values that "dot through" the TCache struct memory, | ||
// if it was a Property then that getter would return a copy of the memory | ||
// preventing the use of "ref" | ||
public TCache Cache; | ||
|
||
public TFeature Fetch<TFeature, TState>( | ||
ref TFeature cached, | ||
TState state, | ||
Func<TState, TFeature> factory) | ||
{ | ||
var cleared = false; | ||
if (Revision != Collection.Revision) | ||
{ | ||
cleared = true; | ||
Cache = default(TCache); | ||
Revision = Collection.Revision; | ||
} | ||
|
||
var feature = cached; | ||
if (feature == null) | ||
{ | ||
feature = Collection.Get<TFeature>(); | ||
if (feature == null) | ||
{ | ||
feature = factory(state); | ||
|
||
Collection.Set(feature); | ||
if (!cleared) | ||
{ | ||
Cache = default(TCache); | ||
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. Bit late; but not sure about this if statement? What's it doing? 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. Should it be #524 ? |
||
} | ||
Revision = Collection.Revision; | ||
} | ||
cached = feature; | ||
} | ||
return feature; | ||
} | ||
|
||
public TFeature Fetch<TFeature>(ref TFeature cached, Func<IFeatureCollection, TFeature> factory) => | ||
Fetch(ref cached, Collection, factory); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
You need to override all of methods in order to properly pool right?
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's a policy choice, really - the ConnectionInfo/WebSocket objects might be used infrequently enough it could be could be more efficient to let them be garbage collected. But yes, you would override all child object management methods to take full control