diff options
Diffstat (limited to 'src/Http/samples/SampleApp/PooledHttpContextFactory.cs')
-rw-r--r-- | src/Http/samples/SampleApp/PooledHttpContextFactory.cs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/Http/samples/SampleApp/PooledHttpContextFactory.cs b/src/Http/samples/SampleApp/PooledHttpContextFactory.cs new file mode 100644 index 0000000000..c61e139ac3 --- /dev/null +++ b/src/Http/samples/SampleApp/PooledHttpContextFactory.cs @@ -0,0 +1,83 @@ +// 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; +using System.Collections.Generic; +using System.Text; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; +using Microsoft.Extensions.ObjectPool; + +namespace SampleApp +{ + public class PooledHttpContextFactory : IHttpContextFactory + { + private readonly IHttpContextAccessor _httpContextAccessor; + private readonly Stack<PooledHttpContext> _pool = new Stack<PooledHttpContext>(); + + public PooledHttpContextFactory(ObjectPoolProvider poolProvider) + : this(poolProvider, httpContextAccessor: null) + { + } + + public PooledHttpContextFactory(ObjectPoolProvider poolProvider, IHttpContextAccessor httpContextAccessor) + { + if (poolProvider == null) + { + throw new ArgumentNullException(nameof(poolProvider)); + } + + _httpContextAccessor = httpContextAccessor; + } + + public HttpContext Create(IFeatureCollection featureCollection) + { + if (featureCollection == null) + { + throw new ArgumentNullException(nameof(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); + } + } + } + } +} |