From ff65a0bb079cd2668932134eb877be1f994dd10c Mon Sep 17 00:00:00 2001 From: Matthew Dennis Date: Sun, 23 Aug 2020 13:16:42 -0400 Subject: [PATCH 1/6] Added System.IO.Pipelines NuGet so solution will build. --- src/Bedrock.Framework/Bedrock.Framework.csproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Bedrock.Framework/Bedrock.Framework.csproj b/src/Bedrock.Framework/Bedrock.Framework.csproj index 97e8fe37..861ce1e6 100644 --- a/src/Bedrock.Framework/Bedrock.Framework.csproj +++ b/src/Bedrock.Framework/Bedrock.Framework.csproj @@ -10,4 +10,8 @@ + + + + From 0a61803c0ff3343bf6c1bf09747569f84f9a4902 Mon Sep 17 00:00:00 2001 From: Matthew Dennis Date: Sun, 23 Aug 2020 13:24:48 -0400 Subject: [PATCH 2/6] Cleanup ClientApplication --- samples/ClientApplication/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/ClientApplication/Program.cs b/samples/ClientApplication/Program.cs index 90cb4442..7b666b4d 100644 --- a/samples/ClientApplication/Program.cs +++ b/samples/ClientApplication/Program.cs @@ -41,6 +41,7 @@ static async Task Main(string[] args) Console.WriteLine("6. Length prefixed custom binary protocol"); Console.WriteLine("7. Talk to local docker dameon"); Console.WriteLine("8. Memcached protocol"); + Console.WriteLine("9. RebbitMQ protocol"); while (true) { @@ -91,7 +92,6 @@ static async Task Main(string[] args) Console.WriteLine("RabbitMQ test"); await RabbitMQProtocol(serviceProvider); } - await RabbitMQProtocol(serviceProvider); } } From 1dfd2f3c081f3de436a55328d31ce967fa2b4efe Mon Sep 17 00:00:00 2001 From: Matthew Dennis Date: Sun, 23 Aug 2020 14:05:25 -0400 Subject: [PATCH 3/6] Revert "Added System.IO.Pipelines NuGet so solution will build." This reverts commit ff65a0bb079cd2668932134eb877be1f994dd10c. --- src/Bedrock.Framework/Bedrock.Framework.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Bedrock.Framework/Bedrock.Framework.csproj b/src/Bedrock.Framework/Bedrock.Framework.csproj index 861ce1e6..97e8fe37 100644 --- a/src/Bedrock.Framework/Bedrock.Framework.csproj +++ b/src/Bedrock.Framework/Bedrock.Framework.csproj @@ -10,8 +10,4 @@ - - - - From d9937a43ad78e3edae4574dc6c0b191d362a7a15 Mon Sep 17 00:00:00 2001 From: Matthew Dennis Date: Sun, 23 Aug 2020 14:12:30 -0400 Subject: [PATCH 4/6] Re-added System.IO.Pipelines --- src/Bedrock.Framework/Bedrock.Framework.csproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Bedrock.Framework/Bedrock.Framework.csproj b/src/Bedrock.Framework/Bedrock.Framework.csproj index 97e8fe37..861ce1e6 100644 --- a/src/Bedrock.Framework/Bedrock.Framework.csproj +++ b/src/Bedrock.Framework/Bedrock.Framework.csproj @@ -10,4 +10,8 @@ + + + + From ed181a8f11830c02b1beb1f047281737f960497d Mon Sep 17 00:00:00 2001 From: Matthew Dennis Date: Sun, 23 Aug 2020 14:14:38 -0400 Subject: [PATCH 5/6] move semaphor.WaitAsync into CommandWith[No]Result. Remove unnecessary awaits. --- .../Protocols/Memcached/MemcachedProtocol.cs | 47 +++++++++---------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/Bedrock.Framework.Experimental/Protocols/Memcached/MemcachedProtocol.cs b/src/Bedrock.Framework.Experimental/Protocols/Memcached/MemcachedProtocol.cs index 35b9ecf6..2c85da1a 100644 --- a/src/Bedrock.Framework.Experimental/Protocols/Memcached/MemcachedProtocol.cs +++ b/src/Bedrock.Framework.Experimental/Protocols/Memcached/MemcachedProtocol.cs @@ -1,6 +1,8 @@ using Bedrock.Framework.Experimental.Protocols.Memcached; using Bedrock.Framework.Protocols; + using Microsoft.AspNetCore.Connections; + using System; using System.Buffers; using System.Collections.Concurrent; @@ -8,12 +10,13 @@ using System.Text; using System.Threading; using System.Threading.Tasks; + using static Bedrock.Framework.Experimental.Protocols.Memcached.Enums; namespace Bedrock.Framework.Experimental.Protocols.Memcached { public class MemcachedProtocol - { + { private readonly ConnectionContext _connection; private readonly MemcachedMessageWriter _memcachedMessageWriter; private readonly MemcachedMessageReader _memcachedMessageReader; @@ -40,6 +43,8 @@ public MemcachedProtocol(ConnectionContext connection) private async Task CommandWithResult(MemcachedRequest request) { + await _semaphore.WaitAsync(); + try { var result = await ExecuteCommand(request); @@ -53,13 +58,15 @@ private async Task CommandWithResult(MemcachedRequest request) } } finally - { + { _semaphore.Release(); } } private async Task CommandWithNoResult(MemcachedRequest request) { + await _semaphore.WaitAsync(); + try { var result = await ExecuteCommand(request); @@ -73,59 +80,49 @@ private async Task CommandWithNoResult(MemcachedRequest request) _semaphore.Release(); } } - - public async Task Get(string key) + + public Task Get(string key) { - await _semaphore.WaitAsync(); - var keyBytes = Encoding.UTF8.GetBytes(key); var request = new MemcachedRequest(Enums.Opcode.Get, keyBytes, NextOpaque); - return await CommandWithResult(request); + return CommandWithResult(request); } - public async Task Delete(string key) + public Task Delete(string key) { - await _semaphore.WaitAsync(); - var keyBytes = Encoding.UTF8.GetBytes(key); var request = new MemcachedRequest(Enums.Opcode.Delete, keyBytes, NextOpaque); - await CommandWithNoResult(request); + return CommandWithNoResult(request); } - public async Task Set(string key, byte[] value, TimeSpan? expireIn) + public Task Set(string key, byte[] value, TimeSpan? expireIn) { - await _semaphore.WaitAsync(); - - var keyBytes = Encoding.UTF8.GetBytes(key); + var keyBytes = Encoding.UTF8.GetBytes(key); var request = new MemcachedRequest(Enums.Opcode.Set, keyBytes, NextOpaque, value, TypeCode.Object, expireIn); - await CommandWithNoResult(request); + return CommandWithNoResult(request); } - public async Task Add(string key, byte[] value, TimeSpan? expireIn) + public Task Add(string key, byte[] value, TimeSpan? expireIn) { - await _semaphore.WaitAsync(); - var keyBytes = Encoding.UTF8.GetBytes(key); var request = new MemcachedRequest(Enums.Opcode.Add, keyBytes, NextOpaque, value, TypeCode.Object, expireIn); - await CommandWithNoResult(request); + return CommandWithNoResult(request); } - public async Task Replace(string key, byte[] value, TimeSpan? expireIn) + public Task Replace(string key, byte[] value, TimeSpan? expireIn) { - await _semaphore.WaitAsync(); - var keyBytes = Encoding.UTF8.GetBytes(key); var request = new MemcachedRequest(Enums.Opcode.Replace, keyBytes, NextOpaque, value, TypeCode.Object, expireIn); - await CommandWithNoResult(request); + return CommandWithNoResult(request); } private async Task ExecuteCommand(MemcachedRequest request) - { + { await _protocolWriter.WriteAsync(_memcachedMessageWriter, request); var result = await _protocolReader.ReadAsync(_memcachedMessageReader); _protocolReader.Advance(); From f3fa143f1fc198f119d893856491076180c7f28d Mon Sep 17 00:00:00 2001 From: Matthew Dennis Date: Sun, 23 Aug 2020 14:35:17 -0400 Subject: [PATCH 6/6] Added string key to MemcachedRequest --- .../Protocols/Memcached/MemcachedProtocol.cs | 15 +++------ .../Protocols/Memcached/MemcachedRequest.cs | 33 ++++++++++++++----- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/Bedrock.Framework.Experimental/Protocols/Memcached/MemcachedProtocol.cs b/src/Bedrock.Framework.Experimental/Protocols/Memcached/MemcachedProtocol.cs index 2c85da1a..f36d5e29 100644 --- a/src/Bedrock.Framework.Experimental/Protocols/Memcached/MemcachedProtocol.cs +++ b/src/Bedrock.Framework.Experimental/Protocols/Memcached/MemcachedProtocol.cs @@ -83,40 +83,35 @@ private async Task CommandWithNoResult(MemcachedRequest request) public Task Get(string key) { - var keyBytes = Encoding.UTF8.GetBytes(key); - var request = new MemcachedRequest(Enums.Opcode.Get, keyBytes, NextOpaque); + var request = new MemcachedRequest(Enums.Opcode.Get, key, NextOpaque); return CommandWithResult(request); } public Task Delete(string key) { - var keyBytes = Encoding.UTF8.GetBytes(key); - var request = new MemcachedRequest(Enums.Opcode.Delete, keyBytes, NextOpaque); + var request = new MemcachedRequest(Enums.Opcode.Delete, key, NextOpaque); return CommandWithNoResult(request); } public Task Set(string key, byte[] value, TimeSpan? expireIn) { - var keyBytes = Encoding.UTF8.GetBytes(key); - var request = new MemcachedRequest(Enums.Opcode.Set, keyBytes, NextOpaque, value, TypeCode.Object, expireIn); + var request = new MemcachedRequest(Enums.Opcode.Set, key, NextOpaque, value, TypeCode.Object, expireIn); return CommandWithNoResult(request); } public Task Add(string key, byte[] value, TimeSpan? expireIn) { - var keyBytes = Encoding.UTF8.GetBytes(key); - var request = new MemcachedRequest(Enums.Opcode.Add, keyBytes, NextOpaque, value, TypeCode.Object, expireIn); + var request = new MemcachedRequest(Enums.Opcode.Add, key, NextOpaque, value, TypeCode.Object, expireIn); return CommandWithNoResult(request); } public Task Replace(string key, byte[] value, TimeSpan? expireIn) { - var keyBytes = Encoding.UTF8.GetBytes(key); - var request = new MemcachedRequest(Enums.Opcode.Replace, keyBytes, NextOpaque, value, TypeCode.Object, expireIn); + var request = new MemcachedRequest(Enums.Opcode.Replace, key, NextOpaque, value, TypeCode.Object, expireIn); return CommandWithNoResult(request); } diff --git a/src/Bedrock.Framework.Experimental/Protocols/Memcached/MemcachedRequest.cs b/src/Bedrock.Framework.Experimental/Protocols/Memcached/MemcachedRequest.cs index b76b0cfa..ddbbefbb 100644 --- a/src/Bedrock.Framework.Experimental/Protocols/Memcached/MemcachedRequest.cs +++ b/src/Bedrock.Framework.Experimental/Protocols/Memcached/MemcachedRequest.cs @@ -14,21 +14,38 @@ public class MemcachedRequest public TypeCode Flags { get; } public TimeSpan? ExpireIn { get; } - public MemcachedRequest(Opcode opcode, byte[] key, uint opaque,byte[] value, TypeCode flags, TimeSpan ? expireIn=null) + public MemcachedRequest(Opcode opcode, string key, uint opaque, byte[] value, TypeCode flags, TimeSpan? expireIn = null) { - Opcode = opcode; - Key = key; - Opaque = opaque; - Value = value; - Flags = flags; - ExpireIn = expireIn; + Opcode = opcode; + Key = Encoding.UTF8.GetBytes(key); + Opaque = opaque; + Value = value; + Flags = flags; + ExpireIn = expireIn; + } + + public MemcachedRequest(Opcode opcode, byte[] key, uint opaque, byte[] value, TypeCode flags, TimeSpan? expireIn = null) + { + Opcode = opcode; + Key = key; + Opaque = opaque; + Value = value; + Flags = flags; + ExpireIn = expireIn; } public MemcachedRequest(Opcode opcode, byte[] key, uint opaque) { Opcode = opcode; - Key = key; + Key = key; Opaque = opaque; } + + public MemcachedRequest(Opcode opcode, string key, uint opaque) + { + Opcode = opcode; + Key = Encoding.UTF8.GetBytes(key); + Opaque = opaque; + } } }