-
Notifications
You must be signed in to change notification settings - Fork 190
Allow feature cache to be updated #501
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,17 +16,18 @@ namespace Microsoft.AspNet.Http.Internal | |
{ | ||
public class DefaultHttpContext : HttpContext, IFeatureCache | ||
{ | ||
private readonly HttpRequest _request; | ||
private readonly HttpResponse _response; | ||
private ConnectionInfo _connection; | ||
private AuthenticationManager _authenticationManager; | ||
private readonly DefaultHttpRequest _request; | ||
private readonly DefaultHttpResponse _response; | ||
|
||
private DefaultAuthenticationManager _authenticationManager; | ||
private DefaultConnectionInfo _connection; | ||
private DefaultWebSocketManager _websockets; | ||
|
||
private IItemsFeature _items; | ||
private IServiceProvidersFeature _serviceProviders; | ||
private IHttpAuthenticationFeature _authentication; | ||
private IHttpRequestLifetimeFeature _lifetime; | ||
private ISessionFeature _session; | ||
private WebSocketManager _websockets; | ||
|
||
private IFeatureCollection _features; | ||
private int _cachedFeaturesRevision = -1; | ||
|
@@ -36,28 +37,54 @@ public DefaultHttpContext() | |
{ | ||
_features.Set<IHttpRequestFeature>(new HttpRequestFeature()); | ||
_features.Set<IHttpResponseFeature>(new HttpResponseFeature()); | ||
((IFeatureCache)this).SetFeaturesRevision(); | ||
} | ||
|
||
public DefaultHttpContext(IFeatureCollection features) | ||
{ | ||
_features = features; | ||
_request = new DefaultHttpRequest(this, features); | ||
_response = new DefaultHttpResponse(this, features); | ||
((IFeatureCache)this).SetFeaturesRevision(); | ||
} | ||
|
||
void IFeatureCache.CheckFeaturesRevision() | ||
{ | ||
if (_cachedFeaturesRevision !=_features.Revision) | ||
{ | ||
_items = null; | ||
_serviceProviders = null; | ||
_authentication = null; | ||
_lifetime = null; | ||
_session = null; | ||
_cachedFeaturesRevision = _features.Revision; | ||
ResetFeatures(); | ||
} | ||
} | ||
|
||
void IFeatureCache.SetFeaturesRevision() | ||
{ | ||
_cachedFeaturesRevision = _features.Revision; | ||
} | ||
|
||
public void UpdateFeatures(IFeatureCollection features) | ||
{ | ||
_features = features; | ||
ResetFeatures(); | ||
|
||
_request.UpdateFeatures(features); | ||
_response.UpdateFeatures(features); | ||
|
||
_authenticationManager?.UpdateFeatures(features); | ||
_connection?.UpdateFeatures(features); | ||
_websockets?.UpdateFeatures(features); | ||
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 these lazy sub-objects be cleared out per request? Only some requests will need them. If they don't get cleared then eventually every pooled HttpContext will have all of them. 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. Paid the price; fairly small, are a defined size (rather than an unknown user code type) - might as well? (Happy either way) 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. shrug It's a probability question. We can figure it out based on profiles later. |
||
} | ||
|
||
private void ResetFeatures() | ||
{ | ||
_items = null; | ||
_serviceProviders = null; | ||
_authentication = null; | ||
_lifetime = null; | ||
_session = null; | ||
|
||
((IFeatureCache)this).SetFeaturesRevision(); | ||
} | ||
|
||
IItemsFeature ItemsFeature | ||
{ | ||
get | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,18 +21,36 @@ public class DefaultWebSocketManager : WebSocketManager, IFeatureCache | |
public DefaultWebSocketManager(IFeatureCollection features) | ||
{ | ||
_features = features; | ||
((IFeatureCache)this).SetFeaturesRevision(); | ||
} | ||
|
||
void IFeatureCache.CheckFeaturesRevision() | ||
{ | ||
if (_cachedFeaturesRevision != _features.Revision) | ||
{ | ||
_request = null; | ||
_webSockets = null; | ||
_cachedFeaturesRevision = _features.Revision; | ||
ResetFeatures(); | ||
} | ||
} | ||
|
||
void IFeatureCache.SetFeaturesRevision() | ||
{ | ||
_cachedFeaturesRevision = _features.Revision; | ||
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. Would it make sense to also set _cachedFeaturesRevision in the constructor? It would save you work on your first call to CheckFeaturesRevision. 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. Yep; done both, just running tests locally. |
||
} | ||
|
||
public void UpdateFeatures(IFeatureCollection features) | ||
{ | ||
_features = features; | ||
ResetFeatures(); | ||
} | ||
|
||
private void ResetFeatures() | ||
{ | ||
_request = null; | ||
_webSockets = null; | ||
|
||
((IFeatureCache)this).SetFeaturesRevision(); | ||
} | ||
|
||
private IHttpRequestFeature HttpRequestFeature | ||
{ | ||
get { return FeatureHelpers.GetAndCache(this, _features, ref _request); } | ||
|
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.
Why the explicit implementation?
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 want someone to call it; as it says "mark cache as up to date; but don't check it" - its only valid use is as part of the Interface contract.