Items is now a first class property on ConnectionContext (#2395)

* Metadata is now a first class property on ConnectionContext
- Make IConnectionMetadata a manatory top level feature on ConnectionContext
- TransportConnection will lazily manifest ConnectionMetadata on first access.
This should avoid allocations since Kestrel isn't using this today.
This commit is contained in:
David Fowler 2018-03-14 20:56:30 -07:00 committed by GitHub
parent 9901f0f3e4
commit d31512528d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 185 additions and 4 deletions

View File

@ -0,0 +1,118 @@
// 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;
using System.Collections.Generic;
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
{
internal class ConnectionItems : IDictionary<object, object>
{
public ConnectionItems()
: this(new Dictionary<object, object>())
{
}
public ConnectionItems(IDictionary<object, object> items)
{
Items = items;
}
public IDictionary<object, object> Items { get; }
// Replace the indexer with one that returns null for missing values
object IDictionary<object, object>.this[object key]
{
get
{
if (Items.TryGetValue(key, out var value))
{
return value;
}
return null;
}
set { Items[key] = value; }
}
void IDictionary<object, object>.Add(object key, object value)
{
Items.Add(key, value);
}
bool IDictionary<object, object>.ContainsKey(object key)
{
return Items.ContainsKey(key);
}
ICollection<object> IDictionary<object, object>.Keys
{
get { return Items.Keys; }
}
bool IDictionary<object, object>.Remove(object key)
{
return Items.Remove(key);
}
bool IDictionary<object, object>.TryGetValue(object key, out object value)
{
return Items.TryGetValue(key, out value);
}
ICollection<object> IDictionary<object, object>.Values
{
get { return Items.Values; }
}
void ICollection<KeyValuePair<object, object>>.Add(KeyValuePair<object, object> item)
{
Items.Add(item);
}
void ICollection<KeyValuePair<object, object>>.Clear()
{
Items.Clear();
}
bool ICollection<KeyValuePair<object, object>>.Contains(KeyValuePair<object, object> item)
{
return Items.Contains(item);
}
void ICollection<KeyValuePair<object, object>>.CopyTo(KeyValuePair<object, object>[] array, int arrayIndex)
{
Items.CopyTo(array, arrayIndex);
}
int ICollection<KeyValuePair<object, object>>.Count
{
get { return Items.Count; }
}
bool ICollection<KeyValuePair<object, object>>.IsReadOnly
{
get { return Items.IsReadOnly; }
}
bool ICollection<KeyValuePair<object, object>>.Remove(KeyValuePair<object, object> item)
{
object value;
if (Items.TryGetValue(item.Key, out value) && Equals(item.Value, value))
{
return Items.Remove(item.Key);
}
return false;
}
IEnumerator<KeyValuePair<object, object>> IEnumerable<KeyValuePair<object, object>>.GetEnumerator()
{
return Items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Items.GetEnumerator();
}
}
}

View File

@ -13,6 +13,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
IHttpConnectionFeature,
IConnectionIdFeature,
IConnectionTransportFeature,
IConnectionItemsFeature,
IMemoryPoolFeature,
IApplicationTransportFeature,
ITransportSchedulerFeature
@ -20,6 +21,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
private static readonly Type IHttpConnectionFeatureType = typeof(IHttpConnectionFeature);
private static readonly Type IConnectionIdFeatureType = typeof(IConnectionIdFeature);
private static readonly Type IConnectionTransportFeatureType = typeof(IConnectionTransportFeature);
private static readonly Type IConnectionItemsFeatureType = typeof(IConnectionItemsFeature);
private static readonly Type IMemoryPoolFeatureType = typeof(IMemoryPoolFeature);
private static readonly Type IApplicationTransportFeatureType = typeof(IApplicationTransportFeature);
private static readonly Type ITransportSchedulerFeatureType = typeof(ITransportSchedulerFeature);
@ -27,6 +29,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
private object _currentIHttpConnectionFeature;
private object _currentIConnectionIdFeature;
private object _currentIConnectionTransportFeature;
private object _currentIConnectionItemsFeature;
private object _currentIMemoryPoolFeature;
private object _currentIApplicationTransportFeature;
private object _currentITransportSchedulerFeature;
@ -118,6 +121,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
set => Application = value;
}
IDictionary<object, object> IConnectionItemsFeature.Items
{
get => Items;
set => Items = value;
}
PipeScheduler ITransportSchedulerFeature.InputWriterScheduler => InputWriterScheduler;
PipeScheduler ITransportSchedulerFeature.OutputReaderScheduler => OutputReaderScheduler;
@ -140,6 +149,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
return _currentIConnectionTransportFeature;
}
if (key == IConnectionItemsFeatureType)
{
return _currentIConnectionItemsFeature;
}
if (key == IMemoryPoolFeatureType)
{
return _currentIMemoryPoolFeature;
@ -178,6 +192,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
{
_currentIConnectionTransportFeature = value;
}
else if (key == IConnectionItemsFeatureType)
{
_currentIConnectionItemsFeature = value;
}
else if (key == IMemoryPoolFeatureType)
{
_currentIMemoryPoolFeature = value;
@ -211,6 +229,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
{
return (TFeature)_currentIConnectionTransportFeature;
}
else if (typeof(TFeature) == typeof(IConnectionItemsFeature))
{
return (TFeature)_currentIConnectionItemsFeature;
}
else if (typeof(TFeature) == typeof(IMemoryPoolFeature))
{
return (TFeature)_currentIMemoryPoolFeature;
@ -247,6 +269,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
{
_currentIConnectionTransportFeature = instance;
}
else if (typeof(TFeature) == typeof(IConnectionItemsFeature))
{
_currentIConnectionItemsFeature = instance;
}
else if (typeof(TFeature) == typeof(IMemoryPoolFeature))
{
_currentIMemoryPoolFeature = instance;
@ -286,6 +312,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
yield return new KeyValuePair<Type, object>(IConnectionTransportFeatureType, _currentIConnectionTransportFeature);
}
if (_currentIConnectionItemsFeature != null)
{
yield return new KeyValuePair<Type, object>(IConnectionItemsFeatureType, _currentIConnectionItemsFeature);
}
if (_currentIMemoryPoolFeature != null)
{
yield return new KeyValuePair<Type, object>(IMemoryPoolFeatureType, _currentIMemoryPoolFeature);

View File

@ -1,4 +1,5 @@
using System.Buffers;
using System.Collections.Generic;
using System.IO.Pipelines;
using System.Net;
using System.Threading;
@ -7,11 +8,14 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
{
public abstract partial class TransportConnection
{
private IDictionary<object, object> _items;
public TransportConnection()
{
_currentIConnectionIdFeature = this;
_currentIConnectionTransportFeature = this;
_currentIHttpConnectionFeature = this;
_currentIConnectionItemsFeature = this;
_currentIApplicationTransportFeature = this;
_currentIMemoryPoolFeature = this;
_currentITransportSchedulerFeature = this;
@ -31,6 +35,19 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal
public IDuplexPipe Transport { get; set; }
public IDuplexPipe Application { get; set; }
public IDictionary<object, object> Items
{
get
{
// Lazily allocate connection metadata
return _items ?? (_items = new ConnectionItems());
}
set
{
_items = value;
}
}
public PipeWriter Input => Application.Output;
public PipeReader Output => Application.Input;
}

View File

@ -1,4 +1,5 @@
using System.IO.Pipelines;
using System.Collections.Generic;
using System.IO.Pipelines;
using Microsoft.AspNetCore.Http.Features;
namespace Microsoft.AspNetCore.Protocols
@ -9,6 +10,8 @@ namespace Microsoft.AspNetCore.Protocols
public abstract IFeatureCollection Features { get; }
public abstract IDictionary<object, object> Items { get; set; }
public abstract IDuplexPipe Transport { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using System.IO.Pipelines;
using System.Collections.Generic;
using System.IO.Pipelines;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Protocols.Features;
@ -19,6 +20,9 @@ namespace Microsoft.AspNetCore.Protocols
private IConnectionTransportFeature ConnectionTransportFeature =>
_features.Fetch(ref _features.Cache.ConnectionTransport, _ => null);
private IConnectionItemsFeature ConnectionItemsFeature =>
_features.Fetch(ref _features.Cache.ConnectionItems, _ => null);
public override string ConnectionId
{
get => ConnectionIdFeature.ConnectionId;
@ -33,11 +37,19 @@ namespace Microsoft.AspNetCore.Protocols
set => ConnectionTransportFeature.Transport = value;
}
public override IDictionary<object, object> Items
{
get => ConnectionItemsFeature.Items;
set => ConnectionItemsFeature.Items = value;
}
struct FeatureInterfaces
{
public IConnectionIdFeature ConnectionId;
public IConnectionTransportFeature ConnectionTransport;
public IConnectionItemsFeature ConnectionItems;
}
}
}

View File

@ -6,8 +6,8 @@ using System.Collections.Generic;
namespace Microsoft.AspNetCore.Protocols.Features
{
public interface IConnectionMetadataFeature
public interface IConnectionItemsFeature
{
IDictionary<object, object> Metadata { get; set; }
IDictionary<object, object> Items { get; set; }
}
}