From 597e26a1e070533e1405f1adfeaf17d46bf6b231 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Sat, 23 Nov 2024 14:48:22 +0000 Subject: [PATCH] Allow to disable EventPriority --- .../Infrastructures/Events/IEventPublisher.cs | 2 +- .../Events/IEventSubscriber.cs | 2 +- .../Infrastructures/Events/RedisPublisher.cs | 7 ++-- .../Infrastructures/Events/RedisSubscriber.cs | 36 +++++++++++++------ 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventPublisher.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventPublisher.cs index a58adf59..f2276c13 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventPublisher.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventPublisher.cs @@ -10,7 +10,7 @@ public interface IEventPublisher /// Task BroadcastAsync(string channel, string message); - Task PublishAsync(string channel, string message, EventPriority priority = EventPriority.Medium); + Task PublishAsync(string channel, string message, EventPriority? priority = null); Task ReDispatchAsync(string channel, int count = 10, string order = "asc"); diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventSubscriber.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventSubscriber.cs index f295e54e..fa96bb35 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventSubscriber.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventSubscriber.cs @@ -4,5 +4,5 @@ public interface IEventSubscriber { Task SubscribeAsync(string channel, Func received); - Task SubscribeAsync(string channel, string group, Func received); + Task SubscribeAsync(string channel, string group, bool priorityEnabled, Func received); } diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisPublisher.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisPublisher.cs index d991a45c..27cbf566 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisPublisher.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisPublisher.cs @@ -21,12 +21,15 @@ public class RedisPublisher : IEventPublisher await _subscriber.PublishAsync(channel, message); } - public async Task PublishAsync(string channel, string message, EventPriority priority = EventPriority.Medium) + public async Task PublishAsync(string channel, string message, EventPriority? priority = null) { var db = _redis.GetDatabase(); // convert to apporiate channel by priority - channel = $"{channel}-{priority}"; + if (priority != null) + { + channel = $"{channel}-{priority}"; + } if (CheckMessageExists(db, channel, "message", message)) { diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs index f35113fe..b93c22a2 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs @@ -25,27 +25,41 @@ public class RedisSubscriber : IEventSubscriber }); } - public async Task SubscribeAsync(string channel, string group, Func received) + public async Task SubscribeAsync(string channel, string group, bool priorityEnabled, Func received) { var db = _redis.GetDatabase(); - await CreateConsumerGroup(db, $"{channel}-{EventPriority.Low}", group); - await CreateConsumerGroup(db, $"{channel}-{EventPriority.Medium}", group); - await CreateConsumerGroup(db, $"{channel}-{EventPriority.High}", group); + if (priorityEnabled) + { + await CreateConsumerGroup(db, $"{channel}-{EventPriority.Low}", group); + await CreateConsumerGroup(db, $"{channel}-{EventPriority.Medium}", group); + await CreateConsumerGroup(db, $"{channel}-{EventPriority.High}", group); + } + else + { + await CreateConsumerGroup(db, channel, group); + } while (true) { - if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, received) > 0) + if (priorityEnabled) { - continue; - } + if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, received) > 0) + { + continue; + } - if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, received) > 0) + if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, received) > 0) + { + continue; + } + + await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, received); + } + else { - continue; + await HandleGroupMessage(db, channel, group, received); } - - await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, received); } }