|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
3 | 3 |
|
4 | 4 | using System;
|
| 5 | +using System.Collections.Generic; |
5 | 6 | using System.Data.Common;
|
6 | 7 | using System.Data.SqlClient;
|
7 | 8 | using Benchmarks.Data;
|
8 | 9 | using Microsoft.AspNet.Builder;
|
9 | 10 | using Microsoft.AspNet.Hosting;
|
10 | 11 | using Microsoft.AspNet.Http;
|
| 12 | +using Microsoft.AspNet.Http.Features; |
| 13 | +using Microsoft.AspNet.Http.Internal; |
11 | 14 | using Microsoft.Data.Entity;
|
12 | 15 | using Microsoft.Extensions.Configuration;
|
13 | 16 | using Microsoft.Extensions.DependencyInjection;
|
@@ -44,6 +47,7 @@ public void ConfigureServices(IServiceCollection services)
|
44 | 47 | // No scenarios covered by the benchmarks require the HttpContextAccessor so we're replacing it with a
|
45 | 48 | // no-op version to avoid the cost.
|
46 | 49 | services.AddSingleton(typeof(IHttpContextAccessor), typeof(InertHttpContextAccessor));
|
| 50 | + services.AddSingleton(typeof(IHttpContextFactory), typeof(PooledContextFactory)); |
47 | 51 |
|
48 | 52 | if (StartupOptions.EnableDbTests)
|
49 | 53 | {
|
@@ -121,5 +125,75 @@ public class Options
|
121 | 125 |
|
122 | 126 | public string ConnectionString { get; set; }
|
123 | 127 | }
|
| 128 | + |
| 129 | + public class PooledContextFactory : IHttpContextFactory |
| 130 | + { |
| 131 | + private IHttpContextAccessor _httpContextAccessor; |
| 132 | + |
| 133 | + [ThreadStatic] |
| 134 | + Queue<DefaultHttpContext> _contextPool; |
| 135 | + |
| 136 | + public PooledContextFactory() : this(httpContextAccessor: null) |
| 137 | + { |
| 138 | + } |
| 139 | + |
| 140 | + public PooledContextFactory(IHttpContextAccessor httpContextAccessor) |
| 141 | + { |
| 142 | + _httpContextAccessor = httpContextAccessor; |
| 143 | + } |
| 144 | + |
| 145 | + private Queue<DefaultHttpContext> ContextPool |
| 146 | + { |
| 147 | + get |
| 148 | + { |
| 149 | + if (_contextPool == null) |
| 150 | + { |
| 151 | + _contextPool = new Queue<DefaultHttpContext>(16); |
| 152 | + } |
| 153 | + return _contextPool; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public HttpContext Create(IFeatureCollection featureCollection) |
| 158 | + { |
| 159 | + var contextPool = ContextPool; |
| 160 | + // locking as ThreadStatic didn't appear to be behaving thread safe via property on coreclr x64 |
| 161 | + lock (contextPool) |
| 162 | + { |
| 163 | + if (contextPool.Count > 0) |
| 164 | + { |
| 165 | + var context = contextPool.Dequeue(); |
| 166 | + // Needs https://github.com/aspnet/HttpAbstractions/pull/501 |
| 167 | + context.ReplaceFeatures(featureCollection); |
| 168 | + return context; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return new DefaultHttpContext(featureCollection); |
| 173 | + } |
| 174 | + |
| 175 | + public void Dispose(HttpContext httpContext) |
| 176 | + { |
| 177 | + if (_httpContextAccessor != null) |
| 178 | + { |
| 179 | + _httpContextAccessor.HttpContext = null; |
| 180 | + } |
| 181 | + |
| 182 | + var context = httpContext as DefaultHttpContext; |
| 183 | + |
| 184 | + if (context != null) |
| 185 | + { |
| 186 | + var contextPool = ContextPool; |
| 187 | + // locking as ThreadStatic didn't appear to be behaving thread safe via property on coreclr x64 |
| 188 | + lock (contextPool) |
| 189 | + { |
| 190 | + if (contextPool.Count < 16) |
| 191 | + { |
| 192 | + contextPool.Enqueue(context); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + } |
124 | 198 | }
|
125 | 199 | }
|
0 commit comments