diff --git a/framework/src/Volo.Abp.EventBus.Azure/Volo/Abp/EventBus/Azure/AzureDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Azure/Volo/Abp/EventBus/Azure/AzureDistributedEventBus.cs index 922ab95412..4344e3c72b 100644 --- a/framework/src/Volo.Abp.EventBus.Azure/Volo/Abp/EventBus/Azure/AzureDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.Azure/Volo/Abp/EventBus/Azure/AzureDistributedEventBus.cs @@ -119,13 +119,22 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen { var handlerFactories = GetOrCreateHandlerFactories(eventType); - if (factory.IsInFactories(handlerFactories)) + var added = handlerFactories.Locking(factories => + { + if (factory.IsInFactories(factories)) + { + return false; + } + + factories.Add(factory); + return true; + }); + + if (!added) { return NullDisposable.Instance; } - handlerFactories.Add(factory); - return new EventHandlerFactoryUnregistrar(this, eventType, factory); } @@ -134,13 +143,22 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen { var handlerFactories = GetOrCreateDynamicHandlerFactories(eventName); - if (handler.IsInFactories(handlerFactories)) + var added = handlerFactories.Locking(factories => + { + if (handler.IsInFactories(factories)) + { + return false; + } + + factories.Add(handler); + return true; + }); + + if (!added) { return NullDisposable.Instance; } - handlerFactories.Add(handler); - return new DynamicEventHandlerFactoryUnregistrar(this, eventName, handler); } @@ -396,12 +414,12 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key))) { - handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value)); + handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value.Locking(factories => factories.ToList()))); } foreach (var handlerFactory in DynamicHandlerFactories.Where(aehf => eventNames.Contains(aehf.Key))) { - handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value)); + handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList()))); } return handlerFactoryList.ToArray(); @@ -452,7 +470,7 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen foreach (var handlerFactory in DynamicHandlerFactories.Where(hf => hf.Key == eventName)) { - result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value)); + result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList()))); } return result; diff --git a/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs index 5e89571554..37968ddd53 100644 --- a/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs @@ -69,13 +69,22 @@ public class DaprDistributedEventBus : DistributedEventBusBase, ISingletonDepend { var handlerFactories = GetOrCreateHandlerFactories(eventType); - if (factory.IsInFactories(handlerFactories)) + var added = handlerFactories.Locking(factories => + { + if (factory.IsInFactories(factories)) + { + return false; + } + + factories.Add(factory); + return true; + }); + + if (!added) { return NullDisposable.Instance; } - handlerFactories.Add(factory); - return new EventHandlerFactoryUnregistrar(this, eventType, factory); } @@ -273,7 +282,7 @@ public class DaprDistributedEventBus : DistributedEventBusBase, ISingletonDepend foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key))) { - handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value)); + handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value.Locking(factories => factories.ToList()))); } return handlerFactoryList.ToArray(); diff --git a/framework/src/Volo.Abp.EventBus.Kafka/Volo/Abp/EventBus/Kafka/KafkaDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Kafka/Volo/Abp/EventBus/Kafka/KafkaDistributedEventBus.cs index 4ac6d4a805..017355a5e5 100644 --- a/framework/src/Volo.Abp.EventBus.Kafka/Volo/Abp/EventBus/Kafka/KafkaDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.Kafka/Volo/Abp/EventBus/Kafka/KafkaDistributedEventBus.cs @@ -118,13 +118,22 @@ public class KafkaDistributedEventBus : DistributedEventBusBase, ISingletonDepen { var handlerFactories = GetOrCreateHandlerFactories(eventType); - if (factory.IsInFactories(handlerFactories)) + var added = handlerFactories.Locking(factories => + { + if (factory.IsInFactories(factories)) + { + return false; + } + + factories.Add(factory); + return true; + }); + + if (!added) { return NullDisposable.Instance; } - handlerFactories.Add(factory); - return new EventHandlerFactoryUnregistrar(this, eventType, factory); } @@ -133,13 +142,22 @@ public class KafkaDistributedEventBus : DistributedEventBusBase, ISingletonDepen { var handlerFactories = GetOrCreateDynamicHandlerFactories(eventName); - if (handler.IsInFactories(handlerFactories)) + var added = handlerFactories.Locking(factories => + { + if (handler.IsInFactories(factories)) + { + return false; + } + + factories.Add(handler); + return true; + }); + + if (!added) { return NullDisposable.Instance; } - handlerFactories.Add(handler); - return new DynamicEventHandlerFactoryUnregistrar(this, eventName, handler); } @@ -427,12 +445,12 @@ public class KafkaDistributedEventBus : DistributedEventBusBase, ISingletonDepen foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key))) { - handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value)); + handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value.Locking(factories => factories.ToList()))); } foreach (var handlerFactory in DynamicHandlerFactories.Where(aehf => eventNames.Contains(aehf.Key))) { - handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value)); + handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList()))); } return handlerFactoryList.ToArray(); @@ -481,7 +499,7 @@ public class KafkaDistributedEventBus : DistributedEventBusBase, ISingletonDepen foreach (var handlerFactory in DynamicHandlerFactories.Where(hf => hf.Key == eventName)) { - result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value)); + result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList()))); } return result; diff --git a/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs index 529a16ddba..29f4d4d416 100644 --- a/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs @@ -138,13 +138,22 @@ public class RabbitMqDistributedEventBus : DistributedEventBusBase, IRabbitMqDis { var handlerFactories = GetOrCreateHandlerFactories(eventType); - if (factory.IsInFactories(handlerFactories)) + var added = handlerFactories.Locking(factories => + { + if (factory.IsInFactories(factories)) + { + return false; + } + + factories.Add(factory); + return true; + }); + + if (!added) { return NullDisposable.Instance; } - handlerFactories.Add(factory); - if (handlerFactories.Count == 1) //TODO: Multi-threading! { Consumer.BindAsync(EventNameAttribute.GetNameOrDefault(eventType)); @@ -157,14 +166,23 @@ public class RabbitMqDistributedEventBus : DistributedEventBusBase, IRabbitMqDis public override IDisposable Subscribe(string eventName, IEventHandlerFactory handler) { var handlerFactories = GetOrCreateDynamicHandlerFactories(eventName); - - if (handler.IsInFactories(handlerFactories)) + + var added = handlerFactories.Locking(factories => + { + if (handler.IsInFactories(factories)) + { + return false; + } + + factories.Add(handler); + return true; + }); + + if (!added) { return NullDisposable.Instance; } - handlerFactories.Add(handler); - if (handlerFactories.Count == 1) //TODO: Multi-threading! { Consumer.BindAsync(eventName); @@ -494,12 +512,12 @@ public class RabbitMqDistributedEventBus : DistributedEventBusBase, IRabbitMqDis foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key))) { - handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value)); + handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value.Locking(factories => factories.ToList()))); } foreach (var handlerFactory in DynamicHandlerFactories.Where(aehf => eventNames.Contains(aehf.Key))) { - handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value)); + handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList()))); } return handlerFactoryList.ToArray(); @@ -548,7 +566,7 @@ public class RabbitMqDistributedEventBus : DistributedEventBusBase, IRabbitMqDis foreach (var handlerFactory in DynamicHandlerFactories.Where(hf => hf.Key == eventName)) { - result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value)); + result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList()))); } return result; diff --git a/framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/RebusDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/RebusDistributedEventBus.cs index b44b65bd4a..2304acaa4a 100644 --- a/framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/RebusDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/RebusDistributedEventBus.cs @@ -109,13 +109,22 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen { var handlerFactories = GetOrCreateHandlerFactories(eventType); - if (factory.IsInFactories(handlerFactories)) + var added = handlerFactories.Locking(factories => + { + if (factory.IsInFactories(factories)) + { + return false; + } + + factories.Add(factory); + return true; + }); + + if (!added) { return NullDisposable.Instance; } - handlerFactories.Add(factory); - if (handlerFactories.Count == 1) //TODO: Multi-threading! { Rebus.Subscribe(eventType); @@ -129,12 +138,21 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen { var handlerFactories = GetOrCreateDynamicHandlerFactories(eventName); - if (handler.IsInFactories(handlerFactories)) + var added = handlerFactories.Locking(factories => + { + if (handler.IsInFactories(factories)) + { + return false; + } + + factories.Add(handler); + return true; + }); + + if (!added) { return NullDisposable.Instance; } - - handlerFactories.Add(handler); if (DynamicHandlerFactories.Count == 1) //TODO: Multi-threading! { @@ -392,12 +410,12 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key))) { - handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value)); + handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value.Locking(factories => factories.ToList()))); } foreach (var handlerFactory in DynamicHandlerFactories.Where(aehf => eventNames.Contains(aehf.Key))) { - handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value)); + handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList()))); } return handlerFactoryList.ToArray(); @@ -446,7 +464,7 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen foreach (var handlerFactory in DynamicHandlerFactories.Where(hf => hf.Key == eventName)) { - result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value)); + result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList()))); } return result; diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs index 729afa2915..8411a50a0d 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs @@ -217,7 +217,7 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key))) { - foreach (var factory in handlerFactory.Value) + foreach (var factory in handlerFactory.Value.Locking(factories => factories.ToArray())) { handlerFactoryList.Add(new Tuple( factory, @@ -228,7 +228,7 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency foreach (var handlerFactory in DynamicEventHandlerFactories.Where(aehf => eventNames.Contains(aehf.Key))) { - foreach (var factory in handlerFactory.Value) + foreach (var factory in handlerFactory.Value.Locking(factories => factories.ToArray())) { handlerFactoryList.Add(new Tuple( factory, @@ -252,7 +252,7 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency foreach (var handlerFactory in DynamicEventHandlerFactories.Where(aehf => aehf.Key == eventName)) { - foreach (var factory in handlerFactory.Value) + foreach (var factory in handlerFactory.Value.Locking(factories => factories.ToArray())) { using var handler = factory.GetHandler(); var handlerType = handler.EventHandler.GetType(); diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Concurrency_Test.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Concurrency_Test.cs new file mode 100644 index 0000000000..0c285390da --- /dev/null +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Concurrency_Test.cs @@ -0,0 +1,103 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Shouldly; +using Xunit; + +namespace Volo.Abp.EventBus.Local; + +public class EventBus_Concurrency_Test : EventBusTestBase +{ + private const int PublishCount = 10_000; + + [Fact] + public async Task Should_Publish_While_Other_Handlers_Subscribe_And_Unsubscribe() + { + var handledCount = 0; + using var subscription = LocalEventBus.Subscribe(_ => + { + Interlocked.Increment(ref handledCount); + return Task.CompletedTask; + }); + + await PublishWhileChurningAsync( + () => LocalEventBus.Subscribe(_ => Task.CompletedTask).Dispose(), + () => LocalEventBus.PublishAsync(new MySimpleEventData(1), onUnitOfWorkComplete: false)); + + handledCount.ShouldBe(PublishCount); + } + + [Fact] + public async Task Should_Publish_While_Dynamic_Handlers_Of_The_Same_Event_Subscribe_And_Unsubscribe() + { + var handledCount = 0; + using var subscription = LocalEventBus.Subscribe(_ => + { + Interlocked.Increment(ref handledCount); + return Task.CompletedTask; + }); + + var eventName = EventNameAttribute.GetNameOrDefault(); + + await PublishWhileChurningAsync( + () => LocalEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory( + new ActionEventHandler(_ => Task.CompletedTask))).Dispose(), + () => LocalEventBus.PublishAsync(new MySimpleEventData(1), onUnitOfWorkComplete: false)); + + handledCount.ShouldBe(PublishCount); + } + + [Fact] + public async Task Should_Publish_Dynamic_Event_While_Other_Handlers_Subscribe_And_Unsubscribe() + { + const string eventName = "MyConcurrencyTestDynamicEvent"; + var handledCount = 0; + using var subscription = LocalEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory( + new ActionEventHandler(_ => + { + Interlocked.Increment(ref handledCount); + return Task.CompletedTask; + }))); + + await PublishWhileChurningAsync( + () => LocalEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory( + new ActionEventHandler(_ => Task.CompletedTask))).Dispose(), + () => LocalEventBus.PublishAsync(eventName, new { Value = 1 }, onUnitOfWorkComplete: false)); + + handledCount.ShouldBe(PublishCount); + } + + private static async Task PublishWhileChurningAsync(Action subscribeAndUnsubscribe, Func publish) + { + var churnStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var churnCount = 0; + + using var cts = new CancellationTokenSource(); + var churn = Task.Run(() => + { + churnStarted.TrySetResult(); + while (!cts.IsCancellationRequested) + { + subscribeAndUnsubscribe(); + Interlocked.Increment(ref churnCount); + } + }); + + try + { + await churnStarted.Task.WaitAsync(TimeSpan.FromSeconds(10)); + + for (var i = 0; i < PublishCount; i++) + { + await publish(); + } + } + finally + { + cts.Cancel(); + await churn; + } + + churnCount.ShouldBeGreaterThan(0); + } +}