Skip to content

Pool http context #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/Benchmarks/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using Benchmarks.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -43,6 +46,7 @@ public void ConfigureServices(IServiceCollection services)
// No scenarios covered by the benchmarks require the HttpContextAccessor so we're replacing it with a
// no-op version to avoid the cost.
services.AddSingleton(typeof(IHttpContextAccessor), typeof(InertHttpContextAccessor));
services.AddSingleton(typeof(IHttpContextFactory), typeof(PooledContextFactory));

if (Scenarios.Any("Db"))
{
Expand Down Expand Up @@ -161,5 +165,69 @@ public HttpContext HttpContext
set { return; }
}
}

public class PooledContextFactory : IHttpContextFactory
{
private IHttpContextAccessor _httpContextAccessor;

[ThreadStatic]
static Queue<DefaultHttpContext> _contextPool;

public PooledContextFactory() : this(httpContextAccessor: null)
{
}

public PooledContextFactory(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}

private Queue<DefaultHttpContext> ContextPool
{
get
{
if (_contextPool == null)
{
_contextPool = new Queue<DefaultHttpContext>(16);
}

return _contextPool;
}
}

public HttpContext Create(IFeatureCollection featureCollection)
{
var contextPool = ContextPool;
if (contextPool.Count > 0)
{
var context = contextPool.Dequeue();
context.Initialize(featureCollection);
return context;
}

return new DefaultHttpContext(featureCollection);
}

public void Dispose(HttpContext httpContext)
{
if (_httpContextAccessor != null)
{
_httpContextAccessor.HttpContext = null;
}

var context = httpContext as DefaultHttpContext;

if (context != null)
{
context.Uninitialize();

var contextPool = ContextPool;
if (contextPool.Count < 16)
{
contextPool.Enqueue(context);
}
}
}
}
}
}