From c05c203c289ebc38dfbb7496199b6baad7d1bb75 Mon Sep 17 00:00:00 2001 From: Louis DeJardin Date: Wed, 16 Dec 2015 22:02:05 -0800 Subject: [PATCH] Adding example of what http context pooling might look like --- samples/SampleApp/PooledHttpContext.cs | 54 +++++++++++++++ samples/SampleApp/PooledHttpContextFactory.cs | 65 +++++++++++++++++++ .../DefaultHttpRequest.cs | 2 +- .../DefaultHttpResponse.cs | 2 +- 4 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 samples/SampleApp/PooledHttpContext.cs create mode 100644 samples/SampleApp/PooledHttpContextFactory.cs diff --git a/samples/SampleApp/PooledHttpContext.cs b/samples/SampleApp/PooledHttpContext.cs new file mode 100644 index 0000000000..7736085eac --- /dev/null +++ b/samples/SampleApp/PooledHttpContext.cs @@ -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(); + } + } +} \ No newline at end of file diff --git a/samples/SampleApp/PooledHttpContextFactory.cs b/samples/SampleApp/PooledHttpContextFactory.cs new file mode 100644 index 0000000000..62955d90f8 --- /dev/null +++ b/samples/SampleApp/PooledHttpContextFactory.cs @@ -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 _pool = new Stack(); + + 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); + } + } + } + } +} diff --git a/src/Microsoft.AspNet.Http/DefaultHttpRequest.cs b/src/Microsoft.AspNet.Http/DefaultHttpRequest.cs index e03f93c83c..f8890e7345 100644 --- a/src/Microsoft.AspNet.Http/DefaultHttpRequest.cs +++ b/src/Microsoft.AspNet.Http/DefaultHttpRequest.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNet.Http.Internal private HttpContext _context; private FeatureReferences _features; - public DefaultHttpRequest(DefaultHttpContext context, IFeatureCollection features) + public DefaultHttpRequest(HttpContext context, IFeatureCollection features) { Initialize(context, features); } diff --git a/src/Microsoft.AspNet.Http/DefaultHttpResponse.cs b/src/Microsoft.AspNet.Http/DefaultHttpResponse.cs index 901e17cbfe..a9cb49cbdf 100644 --- a/src/Microsoft.AspNet.Http/DefaultHttpResponse.cs +++ b/src/Microsoft.AspNet.Http/DefaultHttpResponse.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNet.Http.Internal private HttpContext _context; private FeatureReferences _features; - public DefaultHttpResponse(DefaultHttpContext context, IFeatureCollection features) + public DefaultHttpResponse(HttpContext context, IFeatureCollection features) { Initialize(context, features); }