Remove ConcurrentDictionary in HandshakeProtocol (#23315)

This commit is contained in:
Stephen Halter 2020-06-25 12:31:38 -07:00 committed by GitHub
parent 10baccefb4
commit 71af57fdc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 17 deletions

View File

@ -26,29 +26,24 @@ namespace Microsoft.AspNetCore.SignalR.Protocol
private const string TypePropertyName = "type";
private static JsonEncodedText TypePropertyNameBytes = JsonEncodedText.Encode(TypePropertyName);
private static ConcurrentDictionary<IHubProtocol, ReadOnlyMemory<byte>> _messageCache = new ConcurrentDictionary<IHubProtocol, ReadOnlyMemory<byte>>();
private static readonly ReadOnlyMemory<byte> _successHandshakeData;
public static ReadOnlySpan<byte> GetSuccessfulHandshake(IHubProtocol protocol)
static HandshakeProtocol()
{
ReadOnlyMemory<byte> result;
if (!_messageCache.TryGetValue(protocol, out result))
var memoryBufferWriter = MemoryBufferWriter.Get();
try
{
var memoryBufferWriter = MemoryBufferWriter.Get();
try
{
WriteResponseMessage(HandshakeResponseMessage.Empty, memoryBufferWriter);
result = memoryBufferWriter.ToArray();
_messageCache.TryAdd(protocol, result);
}
finally
{
MemoryBufferWriter.Return(memoryBufferWriter);
}
WriteResponseMessage(HandshakeResponseMessage.Empty, memoryBufferWriter);
_successHandshakeData = memoryBufferWriter.ToArray();
}
finally
{
MemoryBufferWriter.Return(memoryBufferWriter);
}
return result.Span;
}
public static ReadOnlySpan<byte> GetSuccessfulHandshake(IHubProtocol protocol) => _successHandshakeData.Span;
/// <summary>
/// Writes the serialized representation of a <see cref="HandshakeRequestMessage"/> to the specified writer.
/// </summary>