Skip to content

Commit b9728d4

Browse files
committed
Use PooledContextFactory
Also needs aspnet/HttpAbstractions#501 to be real
1 parent e9f829e commit b9728d4

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/Benchmarks/Startup.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Data.Common;
67
using System.Data.SqlClient;
78
using Benchmarks.Data;
89
using Microsoft.AspNet.Builder;
910
using Microsoft.AspNet.Hosting;
1011
using Microsoft.AspNet.Http;
12+
using Microsoft.AspNet.Http.Features;
13+
using Microsoft.AspNet.Http.Internal;
1114
using Microsoft.Data.Entity;
1215
using Microsoft.Extensions.Configuration;
1316
using Microsoft.Extensions.DependencyInjection;
@@ -44,6 +47,7 @@ public void ConfigureServices(IServiceCollection services)
4447
// No scenarios covered by the benchmarks require the HttpContextAccessor so we're replacing it with a
4548
// no-op version to avoid the cost.
4649
services.AddSingleton(typeof(IHttpContextAccessor), typeof(InertHttpContextAccessor));
50+
services.AddSingleton(typeof(IHttpContextFactory), typeof(PooledContextFactory));
4751

4852
if (StartupOptions.EnableDbTests)
4953
{
@@ -121,5 +125,69 @@ public class Options
121125

122126
public string ConnectionString { get; set; }
123127
}
128+
129+
public class PooledContextFactory : IHttpContextFactory
130+
{
131+
private IHttpContextAccessor _httpContextAccessor;
132+
133+
[ThreadStatic]
134+
static 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+
154+
return _contextPool;
155+
}
156+
}
157+
158+
public HttpContext Create(IFeatureCollection featureCollection)
159+
{
160+
var contextPool = ContextPool;
161+
if (contextPool.Count > 0)
162+
{
163+
var context = contextPool.Dequeue();
164+
// Needs https://github.com/aspnet/HttpAbstractions/pull/501
165+
//context.UpdateFeatures(featureCollection);
166+
return context;
167+
}
168+
169+
return new DefaultHttpContext(featureCollection);
170+
}
171+
172+
public void Dispose(HttpContext httpContext)
173+
{
174+
if (_httpContextAccessor != null)
175+
{
176+
_httpContextAccessor.HttpContext = null;
177+
}
178+
179+
var context = httpContext as DefaultHttpContext;
180+
181+
if (context != null)
182+
{
183+
var contextPool = ContextPool;
184+
185+
if (contextPool.Count < 16)
186+
{
187+
contextPool.Enqueue(context);
188+
}
189+
}
190+
}
191+
}
124192
}
125193
}

0 commit comments

Comments
 (0)