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 const string TypePropertyName = "type";
private static JsonEncodedText TypePropertyNameBytes = JsonEncodedText.Encode(TypePropertyName); 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; var memoryBufferWriter = MemoryBufferWriter.Get();
if (!_messageCache.TryGetValue(protocol, out result)) try
{ {
var memoryBufferWriter = MemoryBufferWriter.Get(); WriteResponseMessage(HandshakeResponseMessage.Empty, memoryBufferWriter);
try _successHandshakeData = memoryBufferWriter.ToArray();
{ }
WriteResponseMessage(HandshakeResponseMessage.Empty, memoryBufferWriter); finally
result = memoryBufferWriter.ToArray(); {
_messageCache.TryAdd(protocol, result); MemoryBufferWriter.Return(memoryBufferWriter);
}
finally
{
MemoryBufferWriter.Return(memoryBufferWriter);
}
} }
return result.Span;
} }
public static ReadOnlySpan<byte> GetSuccessfulHandshake(IHubProtocol protocol) => _successHandshakeData.Span;
/// <summary> /// <summary>
/// Writes the serialized representation of a <see cref="HandshakeRequestMessage"/> to the specified writer. /// Writes the serialized representation of a <see cref="HandshakeRequestMessage"/> to the specified writer.
/// </summary> /// </summary>