From c0409823d2f4f5f10045a81d18698f0d9a49207c Mon Sep 17 00:00:00 2001 From: Ledjon Behluli Date: Sat, 26 Aug 2023 12:39:27 +0200 Subject: [PATCH 1/3] fixed missing stream channel write op for BaseAgent --- src/OrleanSpaces/Agents/BaseAgent.cs | 17 +++++++++++++---- src/OrleanSpaces/Agents/SpaceAgent.cs | 22 ++++++++++++++-------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/OrleanSpaces/Agents/BaseAgent.cs b/src/OrleanSpaces/Agents/BaseAgent.cs index 534763bd..445b2b45 100644 --- a/src/OrleanSpaces/Agents/BaseAgent.cs +++ b/src/OrleanSpaces/Agents/BaseAgent.cs @@ -4,6 +4,7 @@ using OrleanSpaces.Tuples; using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; using System.Threading.Channels; namespace OrleanSpaces.Agents; @@ -50,10 +51,7 @@ async ValueTask ISpaceRouter.RouteAction(TupleAction case TupleActionType.Insert: { tuples = tuples.Add(action.Tuple); - if (streamChannel is not null) - { - await streamChannel.Writer.WriteAsync(action.Tuple); - } + await TryWriteToStream(action.Tuple); } break; case TupleActionType.Remove: @@ -86,6 +84,8 @@ public async Task WriteAsync(TTuple tuple) ThrowHelpers.EmptyTuple(tuple); await tupleStore.Insert(new(agentId, tuple, TupleActionType.Insert)); + await TryWriteToStream(tuple); + tuples = tuples.Add(tuple); } @@ -196,5 +196,14 @@ public async Task ClearAsync() tuples = ImmutableArray.Empty; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private async Task TryWriteToStream(TTuple tuple) + { + if (streamChannel is not null) + { + await streamChannel.Writer.WriteAsync(tuple); + } + } + #endregion } diff --git a/src/OrleanSpaces/Agents/SpaceAgent.cs b/src/OrleanSpaces/Agents/SpaceAgent.cs index c9d2fd05..2e41d7e4 100644 --- a/src/OrleanSpaces/Agents/SpaceAgent.cs +++ b/src/OrleanSpaces/Agents/SpaceAgent.cs @@ -5,6 +5,7 @@ using OrleanSpaces.Channels; using OrleanSpaces.Registries; using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; namespace OrleanSpaces.Agents; @@ -47,10 +48,7 @@ async ValueTask ISpaceRouter.RouteAction(TupleAction< case TupleActionType.Insert: { tuples = tuples.Add(action.Tuple); - if (streamChannel is not null) - { - await streamChannel.Writer.WriteAsync(action.Tuple); - } + await TryWriteToStream(action.Tuple); } break; case TupleActionType.Remove: @@ -81,12 +79,11 @@ public void Unsubscribe(Guid observerId) public async Task WriteAsync(SpaceTuple tuple) { ThrowHelpers.EmptyTuple(tuple); + await tupleStore.Insert(new(agentId, tuple, TupleActionType.Insert)); + await TryWriteToStream(tuple); + tuples = tuples.Add(tuple); - if (streamChannel is not null) - { - await streamChannel.Writer.WriteAsync(tuple); - } } public ValueTask EvaluateAsync(Func> evaluation) @@ -196,5 +193,14 @@ public async Task ClearAsync() tuples = ImmutableArray.Empty; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private async Task TryWriteToStream(SpaceTuple tuple) + { + if (streamChannel is not null) + { + await streamChannel.Writer.WriteAsync(tuple); + } + } + #endregion } \ No newline at end of file From ed3b67d4f2a593280a77bc54c0fef9151e223022 Mon Sep 17 00:00:00 2001 From: Ledjon Behluli Date: Sat, 26 Aug 2023 12:49:09 +0200 Subject: [PATCH 2/3] extracted streamChannel.WriteIfNotNull to helpers class --- src/OrleanSpaces/Agents/BaseAgent.cs | 4 ++-- src/OrleanSpaces/Agents/SpaceAgent.cs | 4 ++-- src/OrleanSpaces/Helpers/Helpers.cs | 11 +++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/OrleanSpaces/Agents/BaseAgent.cs b/src/OrleanSpaces/Agents/BaseAgent.cs index 445b2b45..738a022e 100644 --- a/src/OrleanSpaces/Agents/BaseAgent.cs +++ b/src/OrleanSpaces/Agents/BaseAgent.cs @@ -51,7 +51,7 @@ async ValueTask ISpaceRouter.RouteAction(TupleAction case TupleActionType.Insert: { tuples = tuples.Add(action.Tuple); - await TryWriteToStream(action.Tuple); + await streamChannel.WriteIfNotNull(action.Tuple); } break; case TupleActionType.Remove: @@ -84,7 +84,7 @@ public async Task WriteAsync(TTuple tuple) ThrowHelpers.EmptyTuple(tuple); await tupleStore.Insert(new(agentId, tuple, TupleActionType.Insert)); - await TryWriteToStream(tuple); + await streamChannel.WriteIfNotNull(tuple); tuples = tuples.Add(tuple); } diff --git a/src/OrleanSpaces/Agents/SpaceAgent.cs b/src/OrleanSpaces/Agents/SpaceAgent.cs index 2e41d7e4..f9d29a94 100644 --- a/src/OrleanSpaces/Agents/SpaceAgent.cs +++ b/src/OrleanSpaces/Agents/SpaceAgent.cs @@ -48,7 +48,7 @@ async ValueTask ISpaceRouter.RouteAction(TupleAction< case TupleActionType.Insert: { tuples = tuples.Add(action.Tuple); - await TryWriteToStream(action.Tuple); + await streamChannel.WriteIfNotNull(action.Tuple); } break; case TupleActionType.Remove: @@ -81,7 +81,7 @@ public async Task WriteAsync(SpaceTuple tuple) ThrowHelpers.EmptyTuple(tuple); await tupleStore.Insert(new(agentId, tuple, TupleActionType.Insert)); - await TryWriteToStream(tuple); + await streamChannel.WriteIfNotNull(tuple); tuples = tuples.Add(tuple); } diff --git a/src/OrleanSpaces/Helpers/Helpers.cs b/src/OrleanSpaces/Helpers/Helpers.cs index c7a8d7d0..b8dc4ae2 100644 --- a/src/OrleanSpaces/Helpers/Helpers.cs +++ b/src/OrleanSpaces/Helpers/Helpers.cs @@ -1,5 +1,6 @@ using System.Buffers; using System.Runtime.CompilerServices; +using System.Threading.Channels; using OrleanSpaces.Helpers; using OrleanSpaces.Tuples; @@ -46,4 +47,14 @@ public static bool AllocateAndExecute(this TConsumer consumer, int return result; } } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task WriteIfNotNull(this Channel? channel, T tuple) + where T : ISpaceTuple + { + if (channel is not null) + { + await channel.Writer.WriteAsync(tuple); + } + } } \ No newline at end of file From 69e79e548a5da575a966a42877aff380e9d76e22 Mon Sep 17 00:00:00 2001 From: Ledjon Behluli Date: Sat, 26 Aug 2023 12:52:58 +0200 Subject: [PATCH 3/3] removed private methods as it has been extract to Helpers class --- src/OrleanSpaces/Agents/BaseAgent.cs | 10 ---------- src/OrleanSpaces/Agents/SpaceAgent.cs | 10 ---------- 2 files changed, 20 deletions(-) diff --git a/src/OrleanSpaces/Agents/BaseAgent.cs b/src/OrleanSpaces/Agents/BaseAgent.cs index 738a022e..5231d5b3 100644 --- a/src/OrleanSpaces/Agents/BaseAgent.cs +++ b/src/OrleanSpaces/Agents/BaseAgent.cs @@ -4,7 +4,6 @@ using OrleanSpaces.Tuples; using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using System.Threading.Channels; namespace OrleanSpaces.Agents; @@ -196,14 +195,5 @@ public async Task ClearAsync() tuples = ImmutableArray.Empty; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private async Task TryWriteToStream(TTuple tuple) - { - if (streamChannel is not null) - { - await streamChannel.Writer.WriteAsync(tuple); - } - } - #endregion } diff --git a/src/OrleanSpaces/Agents/SpaceAgent.cs b/src/OrleanSpaces/Agents/SpaceAgent.cs index f9d29a94..d0dfff12 100644 --- a/src/OrleanSpaces/Agents/SpaceAgent.cs +++ b/src/OrleanSpaces/Agents/SpaceAgent.cs @@ -5,7 +5,6 @@ using OrleanSpaces.Channels; using OrleanSpaces.Registries; using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; namespace OrleanSpaces.Agents; @@ -193,14 +192,5 @@ public async Task ClearAsync() tuples = ImmutableArray.Empty; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private async Task TryWriteToStream(SpaceTuple tuple) - { - if (streamChannel is not null) - { - await streamChannel.Writer.WriteAsync(tuple); - } - } - #endregion } \ No newline at end of file