Lazy initialize HttpConnectionContext.Items (#2027)

This commit is contained in:
James Newton-King 2018-04-15 19:06:13 +12:00 committed by GitHub
parent ddc905c219
commit 725bb33949
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View File

@ -26,11 +26,13 @@ namespace Microsoft.AspNetCore.Http.Connections.Internal
IHttpContextFeature,
IHttpTransportFeature
{
private readonly object _itemsLock = new object();
private readonly object _heartbeatLock = new object();
private List<(Action<object> handler, object state)> _heartbeatHandlers;
private readonly ILogger _logger;
private PipeWriterStream _applicationStream;
private IDuplexPipe _application;
private IDictionary<object, object> _items;
// This tcs exists so that multiple calls to DisposeAsync all wait asynchronously
// on the same task
@ -95,7 +97,24 @@ namespace Microsoft.AspNetCore.Http.Connections.Internal
public ClaimsPrincipal User { get; set; }
public override IDictionary<object, object> Items { get; set; } = new ConnectionItems(new ConcurrentDictionary<object, object>());
public override IDictionary<object, object> Items
{
get
{
if (_items == null)
{
lock (_itemsLock)
{
if (_items == null)
{
_items = new ConnectionItems(new ConcurrentDictionary<object, object>());
}
}
}
return _items;
}
set => _items = value ?? throw new ArgumentNullException(nameof(value));
}
public IDuplexPipe Application
{

View File

@ -76,7 +76,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
[MemberData(nameof(TransportTypes))]
public async Task CanStartAndStopConnectionUsingGivenTransport(HttpTransportType transportType)
{
using (StartVerifableLog(out var loggerFactory))
using (StartVerifableLog(out var loggerFactory, testName: $"CanStartAndStopConnectionUsingGivenTransport_{transportType}"))
{
var url = _serverFixture.Url + "/echo";
var connection = new HttpConnection(new Uri(url), transportType, loggerFactory);